Rspec 不应收到不';类方法不能失败

Rspec 不应收到不';类方法不能失败,rspec,sinatra,Rspec,Sinatra,我不太清楚为什么,但我的Sinatra rspec测试并没有在应该失败的时候失败。以下是我的Rspec的一部分: context "invalid params" do before do @params = {} end it "does not call the Count model" do Count.should_not_receive(:increment) pos

我不太清楚为什么,但我的Sinatra rspec测试并没有在应该失败的时候失败。以下是我的Rspec的一部分:

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
但它并没有失败。但是,如果我将“不应接收”线路切换为:

Count.should_receive(:increment).exactly(2).times
它会响应以下错误:

Failure/Error: Count.should_receive(:increment).exactly(2).times
   (<Count (class)>).increment(any args)
       expected: 2 times
       received: 1 time
失败/错误:计数。应接收(:增量)。精确(2)。次
()增量(任何参数)
预期:2次
收到:1次

那么,如果第一个测试被调用一次,为什么它没有失败呢?

在执行puts last_response.body和last_response.status之后,结果表明Sinatra引发了一个异常并返回了一个500状态

进一步调试后出现这种情况的原因是机架环境设置为“开发”,而不是“测试”。Sinatra的文件说你应该能够

set :environment, :test 
在等级库文件的顶部,但这实际上不起作用。解决方案是在开始需要任何文件之前,在等级库文件的最开头添加以下行:

ENV['RACK_ENV'] = 'test'
就这样