【问题标题】:should_not_receive doesn't fail for Class methodshould_not_receive 对于 Class 方法不会失败
【发布时间】:2012-12-23 00:53:19
【问题描述】:

我不太清楚为什么,但我的 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

但它并没有失败。但是,如果我将 should_not_receive 行切换为:

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

那么为什么第一个测试被调用一次就不会失败呢?

【问题讨论】:

    标签: rspec sinatra


    【解决方案1】:

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

    进一步调试后出现这种情况的原因是 Rack 环境设置为开发而不是测试。 Sinatra 的文档说你应该可以把

    set :environment, :test 
    

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

    ENV['RACK_ENV'] = 'test'
    

    就是这样!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2014-11-12
      • 2020-02-13
      • 2011-05-17
      • 2014-12-16
      • 2016-06-23
      • 1970-01-01
      相关资源
      最近更新 更多