为什么此测试失败 - 未实施错误 Rspec 3.1.x 和 Rails 4.1.9

Why is this test failing - Not Implemented Error Rspec 3.1.x and Rails 4.1.9

我正在测试以下方法:

  def deal_with_response(site, responses, request_url)
    response.each do |response|
      if response.code == 200
        send_request(site.site_api_url, site.site_api_key, response, request_url)
      elsif response.code == 404
        send_request(site.site_api_url, site.site_api_key, response, request_url)
        GeneralMailer.recieved_404(ENV['ADMINISTRATOR_NAME'], ENV['ADMINISTRATOR_EMAIL'], site, response).deliver_now
      elsif response.code == 500
        send_request(site.site_api_url, site.site_api_key, response, request_url)
        GeneralMailer.recieved_500(ENV['ADMINISTRATOR_NAME'], ENV['ADMINISTRATOR_EMAIL'], site, response).deliver_now
      else
        send_request(site.site_api_url, site.site_api_key, response, request_url)
        GeneralMailer.recieved_error(ENV['ADMINISTRATOR_NAME'], ENV['ADMINISTRATOR_EMAIL'], site, response).deliver_now
      end
    end
  end

通过这段代码测试:

it "should send no email (200)" do
  response = double("response", :code => 200)
  site = double("site", :site_api_url => 'http://google.ca', site_api_key: 'sdsadsasada')
  send_request_method = double()
  send_request_method.stub(:send_request).with(site.site_api_url, site.site_api_key, [response], 'http://google.ca').and_return nil

  expect{ BlackBird::PublishToSites::User.deal_with_response(response) }.to_not change{ ActionMailer::Base.deliveries.count }.by(1)
end

结果是:

 Failure/Error: expect{ BlackBird::PublishToSites::User.deal_with_response(response) }.to_not change{ ActionMailer::Base.deliveries.count }.by(1)
 NotImplementedError:
   `expect { }.not_to change { }.by()` is not supported
 # ./spec/blackbird/publish_to_sites/user_spec.rb:129:in `(root)'

我错过了什么吗?我在其他测试中正在做类似的代码,它工作正常。我是不是 expect 块做错了?

如错误消息所示,expect {}.not_to change { }.by() 不受支持。当您将更改匹配器与 not_to 一起使用时,您只能指定 from 值。

尝试以下任一方法:

expect{ BlackBird::PublishToSites::User.deal_with_response(response) }.to_not change{ ActionMailer::Base.deliveries.count }

expect{ BlackBird::PublishToSites::User.deal_with_response(response) }.to_not change{ ActionMailer::Base.deliveries.count }.from(original_delivery_count)

http://www.rubydoc.info/gems/rspec-expectations/RSpec/Matchers#change-instance_method