How to test puts in rspec

前端 未结 5 1145
遇见更好的自我
遇见更好的自我 2021-02-02 08:15

I want to do is run ruby sayhello.rb on the command line, then receive Hello from Rspec.

I\'ve got that with this:

class Hello
         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 08:41

    You can solve this using Rails' active_support library, which adds a capture method:

    require 'active_support/core_ext/kernel/reporting'
    require_relative 'sayhello'
    
    describe Hello do
      it "says 'Hello from RSpec' when ran" do
        output = capture(:stdout) do
          hi = Hello.new
          hi.speak
        end
        expect(output).to include 'Hello from RSpec'
      end
    end
    

提交回复
热议问题