Rspec:如何检查是否调用了另一个类的方法?

时间:2021-09-06 16:05:03

I can I check if FeedItem::populate_from_friend_to_user is called inside the user class?

我可以检查是否在用户类中调用了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

With the above code I get:

通过上面的代码我得到:

undefined method `populate_from_friend_to_user' for :FeedItem:Symbol

2 个解决方案

#1


36  

You should not know where the method is called, just if the method is called.. You just know if the method is call:

您不应该知道调用该方法的位置,只是调用该方法。您只需要知道该方法是否被调用:

 it "should auto populate feed after user.add_friend" do
    FeedItem.should_receive(:populate_from_friend_to_user).with(@friend1, @user)
    @user.add_friend(@friend1)
 end

#2


9  

Remember that is works only in rspec2 . For rspec3 u call

请记住,这仅适用于rspec2。对于rspec3你打电话

expect(@user).to receive(:your_method)

期待(@user)。收到(:your_method)

https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations

https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations

#1


36  

You should not know where the method is called, just if the method is called.. You just know if the method is call:

您不应该知道调用该方法的位置,只是调用该方法。您只需要知道该方法是否被调用:

 it "should auto populate feed after user.add_friend" do
    FeedItem.should_receive(:populate_from_friend_to_user).with(@friend1, @user)
    @user.add_friend(@friend1)
 end

#2


9  

Remember that is works only in rspec2 . For rspec3 u call

请记住,这仅适用于rspec2。对于rspec3你打电话

expect(@user).to receive(:your_method)

期待(@user)。收到(:your_method)

https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations

https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs/message-expectations