Ruby-on-rails 如何使用 RSpec 在对象上添加多个 should_receive 期望?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/244009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add multiple should_receive expectations on an object using RSpec?
提问by Micah
In my Rails controller, I'm creating multiple instances of the same model class. I want to add some RSpec expectations so I can test that it is creating the correct number with the correct parameters. So, here's what I have in my spec:
在我的 Rails 控制器中,我创建了同一个模型类的多个实例。我想添加一些 RSpec 期望值,以便我可以测试它是否使用正确的参数创建了正确的数字。所以,这是我在规范中的内容:
Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => @user.id, :position_id => 1, :is_leader => true) Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "2222", :position_id => 2) Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "3333", :position_id => 3) Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "4444", :position_id => 4)
This is causing problems because it seems that the Bandmate class can only have 1 "should_receive" expectation set on it. So, when I run the example, I get the following error:
这会导致问题,因为似乎 Bandmate 类只能设置 1 个“should_receive”期望。因此,当我运行该示例时,出现以下错误:
Spec::Mocks::MockExpectationError in 'BandsController should create all the bandmates when created'
Mock 'Class' expected :create with ({:band_id=>1014, :user_id=>999, :position_id=>1, :is_leader=>true}) but received it with ({:band_id=>1014, :user_id=>"2222", :position_id=>"2"})
Those are the correct parameters for the second call to create, but RSpec is testing against the wrong parameters.
这些是第二次创建调用的正确参数,但 RSpec 正在针对错误的参数进行测试。
Does anyone know how I can set up my should_receive expectations to allow multiple different calls?
有谁知道我如何设置我的 should_receive 期望以允许多个不同的调用?
回答by James Baker
Multiple expectations are not a problem at all. What you're running into are ordering problems, given your specific args on unordered expectations. Check this pagefor details on ordering expectations.
多重期望根本不是问题。考虑到您对无序期望的特定参数,您遇到的是排序问题。查看此页面以了解有关订购期望的详细信息。
The short story is that you should add .orderedto the end of each of your expectations.
简短的故事是,您应该添加.ordered到每个期望的末尾。
回答by bonyiii
my_mock.should_receive(:sym).once
my_mock.should_receive(:sym).twice
my_mock.should_receive(:sym).exactly(n).times
my_mock.should_receive(:sym).at_least(:once)
my_mock.should_receive(:sym).at_least(:twice)
my_mock.should_receive(:sym).at_least(n).times
my_mock.should_receive(:sym).at_most(:once)
my_mock.should_receive(:sym).at_most(:twice)
my_mock.should_receive(:sym).at_most(n).times
my_mock.should_receive(:sym).any_number_of_times
my_mock.should_receive(:sym).一次
my_mock.should_receive(:sym).twice
my_mock.should_receive(:sym).exactly(n)
.times my_mock.should_receive(:sym).at_least(:once)
my_mock.should_receive(: sym).exactly(n) .times my_mock.should_receive(:sym).at_least(:once) sym).at_least(:twice)
my_mock.should_receive(:sym).at_least(n)
.times my_mock.should_receive(:sym).at_most(:once)
my_mock.should_receive(:sym).at_most(:twice)
my_mock。 should_receive(:sym).at_most(n)
.times my_mock.should_receive(:sym).any_number_of_times
Works for rspec 2.5 too.
也适用于 rspec 2.5。

