2012-12-22 31 views
1

我不太确定为什么,但是我的Sinatra rspec测试不会在应该的时候正确地失败。这是我的Rspec的一部分:should_not_receive不会失败Class方法

context "invalid params" do 
     before do 
      @params = {} 
     end 
     it "does not call the Count model" do 
      Count.should_not_receive(:increment) 
      post '/counts' , @params 
     end 
end 

虽然没有失败。但是如果我切换should_not_receive行:

Failure/Error: Count.should_receive(:increment).exactly(2).times 
    (<Count (class)>).increment(any args) 
     expected: 2 times 
     received: 1 time 

那么,为什么第一次测试没有,如果它被称为一次失败:

Count.should_receive(:increment).exactly(2).times 

它与下面的错误响应?

回答

0

在做last_response.body和last_response.status之后,事实证明Sinatra正在引发异常并返回500状态。

进一步调试之后,原因是Rack环境设置为开发而未测试。 Sinatra的文档说你应该可以把

set :environment, :test 

在您的规格文件的顶部,但实际上不起作用。解决方法是在开始需要任何文件之前,在spec文件的开头添加以下行:

ENV['RACK_ENV'] = 'test' 

就是这样!