有没有办法设置RSpec期望接收方法调用作为参数?

时间:2014-09-05 17:51:23

标签: ruby rspec boolean-expression

  

expect(my_class).to receive(:method_b).with(:calling_method_b_here)

(它不是我想要的返回值,但专门测试method_b以这种方式调用)

1 个答案:

答案 0 :(得分:1)

如果我理解正确,这对你有用:

it "should call #method_b when I call #method_that_calls_b" do 
  expect(my_class).to receive(:method_b).with(anything) #anything means I don't care explictly what is sent in just that it was called
  my_class.method_that_calls_b
end

这将测试method_that_calls_b实际上是否使用任何参数调用method_b。如果你知道它应该接收什么参数,那么用你希望它调用的东西替换任何东西。 e.g。

it "should call #method_b with 'hello'" do 
   expect(my_class).to receive(:method_b).with('hello')
   my_class.method_b('goodbye') #this will fail
   my_class.method_b('hello') #this will pass without the above line
end