Ruby on rails 测试在RSpec中使用特定输入调用存根方法

Ruby on rails 测试在RSpec中使用特定输入调用存根方法,ruby-on-rails,rspec,Ruby On Rails,Rspec,我正在RSpec中删除一个控制器方法,并且这种删除工作正常,但是如何确保使用特定的输入调用该方法,即我传递给调用此存根方法的方法的哈希?您可以使用expect\u to\u receive\u和\u参数: expect(double).to receive(:msg).with(no_args) expect(double).to receive(:msg).with(any_args) expect(double).to receive(:msg).with(1, any_args) # an

我正在RSpec中删除一个控制器方法,并且这种删除工作正常,但是如何确保使用特定的输入调用该方法,即我传递给调用此存根方法的方法的哈希?

您可以使用expect\u to\u receive\u和\u参数:

expect(double).to receive(:msg).with(no_args)
expect(double).to receive(:msg).with(any_args)
expect(double).to receive(:msg).with(1, any_args) # any args acts like an arg splat and can go anywhere
expect(double).to receive(:msg).with(1, kind_of(Numeric), "b") #2nd argument can be any kind of Numeric
expect(double).to receive(:msg).with(1, boolean(), "b") #2nd argument can be true or false
expect(double).to receive(:msg).with(1, /abc/, "b") #2nd argument can be any String matching the submitted Regexp
expect(double).to receive(:msg).with(1, anything(), "b") #2nd argument can be anything at all
expect(double).to receive(:msg).with(1, duck_type(:abs, :div), "b") #2nd argument can be object that responds to #abs and #div
expect(double).to receive(:msg).with(hash_including(:a => 5)) # first arg is a hash with a: 5 as one of the key-values
expect(double).to receive(:msg).with(array_including(5)) # first arg is an array with 5 as one of the key-values
expect(double).to receive(:msg).with(hash_excluding(:a => 5)) # first arg is a hash without a: 5 as one of the key-values

您可以使用expect\u to\u receive\u和\u参数:

expect(double).to receive(:msg).with(no_args)
expect(double).to receive(:msg).with(any_args)
expect(double).to receive(:msg).with(1, any_args) # any args acts like an arg splat and can go anywhere
expect(double).to receive(:msg).with(1, kind_of(Numeric), "b") #2nd argument can be any kind of Numeric
expect(double).to receive(:msg).with(1, boolean(), "b") #2nd argument can be true or false
expect(double).to receive(:msg).with(1, /abc/, "b") #2nd argument can be any String matching the submitted Regexp
expect(double).to receive(:msg).with(1, anything(), "b") #2nd argument can be anything at all
expect(double).to receive(:msg).with(1, duck_type(:abs, :div), "b") #2nd argument can be object that responds to #abs and #div
expect(double).to receive(:msg).with(hash_including(:a => 5)) # first arg is a hash with a: 5 as one of the key-values
expect(double).to receive(:msg).with(array_including(5)) # first arg is an array with 5 as one of the key-values
expect(double).to receive(:msg).with(hash_excluding(:a => 5)) # first arg is a hash without a: 5 as one of the key-values
with方法就是您要寻找的。 举例如下: expectdouble.ToReceive:stubbed_方法。withhash_参数

with方法就是您要查找的方法。 举例如下: expectdouble.to receive:stubbed_method.withhash_参数