Search code examples
rspecrspec2rspec-rails

rspec , 2 way is the same?


@sponge = Factory(:user)
let(:event_type) { EventType.where( name: 'visit_site').first 

ONE: => false when run test

subject{ Event.new user: @sponge, event_type: event_type, points_earned: event_type.points_value, description: {}}

context 'call #update_user_points when create a event' do
   it{should_receive(:update_user_points)}
end

TWO: => true when run test

it 'should call update_user_points after creation' do
   event = Event.new user: @sponge, event_type: event_type, points_earned:event_type.points_value, description: {}
   event.should_receive(:update_user_points)
   event.save
end

Give me some advises please :D


Solution

  • Your second example is not the same as your first: they both set up a message expectation for Event#update_user_points, but the second calls save after that; the first doesn't.

    I don't think you can use should_receive with an implicit subject, as you try to do in the first example. rspec doesn't seem to complain, but I don't think it does what you're wanting it to. In order, it does this:

    1. Runs the subject block to instantiate an Event.
    2. Sets up a message expectation saying that the subject (the newly created event) should receive the the update_user_points method.
    3. Then it stops, because the example has nothing beyond that.

    It fails because the message was never received.