34

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
shingara
  • 46,608
  • 11
  • 99
  • 105
rtacconi
  • 14,317
  • 20
  • 66
  • 84

2 Answers2

54

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

 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

In RSpec 3 the syntax is

expect(Object).to receive(:method).with(params)
Shyam Habarakada
  • 15,367
  • 3
  • 36
  • 47
shingara
  • 46,608
  • 11
  • 99
  • 105
11

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

Adam Piotrowski
  • 674
  • 1
  • 7
  • 15