How to test puts in rspec

前端 未结 5 1143
遇见更好的自我
遇见更好的自我 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:30

    You're executing your code before entering the test block, so the expectations are not being met. You need to run the code within the test block after setting expectations (e.g. by moving the require_relative statement after the STDOUT.... statement), as follows:

    describe "sayhello.rb" do
      it "should say 'Hello from Rspec' when ran" do        
        STDOUT.should_receive(:puts).with('Hello from RSpec')
        require_relative 'sayhello.rb' #load/run the file 
      end
    end
    

提交回复
热议问题