(´-`) .. oO (I want to easily find the standard output "Hello".

I'm new to Ruby.

Today I made a rake task that just outputs "Hello World".

# rake hoge:hello 
"Hello World"

I wish I had written a test to make sure that the result contained "Hello" and could find it simply by specifying a string. .. ..

code

hoge_spec.rb


require 'rails_helper'
require 'rake'

RSpec.describe 'Hoge', type: :task do

  #Omission

  describe 'rake hoge:hello' do
    let(:task) { 'hoge:hello' }

    context 'Find the standard output Hello' do

      it 'Hello on standard output#1 Character string as an argument of output matcher' do
        #* Ideal type. However, it fails because it does not match exactly. .. ..
        expect{ @rake[task].invoke() }.to output('Hello').to_stdout
      end

      it 'Hello on standard output#2 Regular expression in the argument of output matcher' do
        #* It works, but regular expression matches are difficult to use. .. ..
        expect{ @rake[task].invoke() }.to output(/Hello/).to_stdout
      end

      it 'Hello on standard output#Partial match with 3 include' do
        #* It works, but the code is long. .. ..
        $stdout = StringIO.new
        @rake[task].invoke()
        output_text = $stdout.string
        $stdout = STDOUT
        expect(output_text).to include 'Hello'
      end
    end
  end

end

Execution result

image.png

image.png

... I thought it would be nice if I could specify a character string (# 1), but an error. The only way to achieve the purpose is to use a regular expression (# 2) or assign the standard output to a variable (# 3). .. .. I'm wondering if there is any good way.

Recommended Posts

(´-`) .. oO (I want to easily find the standard output "Hello".
I want to output the day of the week
I want to simplify the log output on Android
I want to change the log output settings of UtilLoggingJdbcLogger
[Java] I want to test standard input & standard output with JUnit
[Java] Color the standard output to the terminal
I want to write JSP in Emacs more easily than the default.
I want to find out what character the character string appears from the left
I want to var_dump the contents of the intent
I want to truncate after the decimal point
I want to get the value in Ruby
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I want to judge the range using the monthly degree
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
I want to call the main method using reflection
[Rough commentary] I want to marry the pluck method
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to add a delete function to the comment function
I want to output the character number from the left where an arbitrary character string appears.
I want to find out which version of java the jar file I have is available
[Beginner] I want to modify the migration file-How to use rollback-
I want to find a relative path in a situation using Path
I want to set the conditions to be displayed in collection_check_boxes
[Rails] [bootstrap] I want to change the font size responsively
I want to use screen sharing on the login screen on Ubuntu 18
I want to convert characters ...
I want to use Java Applet easily on the command line without using an IDE
I want to bring Tomcat to the server and start the application
I want to expand the clickable part of the link_to method
I want to call a method and count the number
I want to use the Java 8 DateTime API slowly (now)
I want to create a form to select the [Rails] category
I want to use the sanitize method other than View.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to put the JDK on my Mac PC
I want to easily back up files used at work
I want to give a class name to the select attribute
I want to recursively search the class list under the package
I want to distinct the duplicated data with has_many through
I want to transition to the same screen in the saved state
I want to narrow down the display of docker ps
I want to return multiple return values for the input argument
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to pass the startup command to postgres with docker-compose.
I want to find out if the specified character string is supported by the target character code
I want to simplify the conditional if-else statement in Java
I want to give edit and delete permissions only to the poster
I want to create a chat screen for the Swift chat app!
I want to return to the previous screen with kotlin and java!
The story of Collectors.groupingBy that I want to keep for posterity
[Eclipse] I want to open the same file twice [Split editor]
I want to limit the input by narrowing the range of numbers
I want to add the disabled option to f.radio_button depending on the condition
I want to display the images under assets/images in the production environment
I want to remove the top margin in Grouped UITableView (swift)
[Java] I want to perform distinct with the key in the object