我正在写控制器规格:RSpec。如何检查对象方法是否被调用?
it 'should call the method that performs the movies search' do
movie = Movie.new
movie.should_receive(:search_similar)
get :find_similar, {:id => '1'}
end
和我的控制器看起来像:
Failures:
1) MoviesController searching by director name should call the method that performs the movies search
Failure/Error: movie.should_receive(:search_similar)
(#<Movie:0xaa2a454>).search_similar(any args)
expected: 1 time
received: 0 times
# ./spec/controllers/movies_controller_spec.rb:33:in `block (3 levels) in <top (required)>'
我看来:运行RSpec的我得到以下后
def find_similar
@movies = Movie.find(params[:id]).search_similar
end
理解和接受,因为在我的控制器代码中,我调用了Class(Movie)方法,并且我没有看到任何方式将“find_similar”与对象创建的连接C。
所以问题是 - >什么方法来检查方法是否在对象上调用,在spec中创建?
同意过度存根,看到太多的测试检查方法存根工作,而不是检查应用程序的工作原理。 – njorden 2012-06-18 20:08:34
不错的一个。这里是关于津津乐道的文档:https://www.relishapp.com/rspec/rspec-mocks/v/2-5/docs/message-expectations我需要这一点'obj.should_receive(:message).with('more_than ','one_argument')' – TomDunning 2013-10-11 10:29:02
有趣的使用“”的标签 – fotanus 2013-10-31 13:11:57