Rspec:如何检查是否调用了另一个类的方法?
我可以检查 FeedItem::populate_from_friend_to_user 是否在用户类内部被调用?
it "should auto populate feed after user.add_friend" do
@user.add_friend(@friend1)
@user.should_receive('FeedItem::populate_from_friend_to_user').with(@friend1, @user)
end
通过上面的代码我得到:
undefined method `populate_from_friend_to_user' for :FeedItem:Symbol
I can I check if FeedItem::populate_from_friend_to_user is called inside the user class?
it "should auto populate feed after user.add_friend" do
@user.add_friend(@friend1)
@user.should_receive('FeedItem::populate_from_friend_to_user').with(@friend1, @user)
end
With the above code I get:
undefined method `populate_from_friend_to_user' for :FeedItem:Symbol
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该知道哪里调用该方法,而只是如果调用该方法。您只知道该方法是否被调用:
在 RSpec 3 之前 >
在 RSpec 3 中,语法是
You should not know where the method is called, just if the method is called.. You just know if the method is call:
Before RSpec 3
In RSpec 3 the syntax is
请记住,这仅适用于 rspec2 。对于 rspec3,您调用
expect(@user).to receive(:your_method)
https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations
Remember that is works only in rspec2 . For rspec3 u call
expect(@user).to receive(:your_method)
https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations