Search code examples
rspecrspec2rspec-rails

RSpec: Undefined method `assert_difference' for ... (NoMethodError)


context 'with event_type is available create event' do
  let(:event_type) { EventType.where( name: 'visit_site').first }
  assert_difference 'Event.count' do
    Event.fire_event(event_type, @sponge,{})
  end
end

I searched Google for this error but found nothing to fix it. Help me please. Thank you :)


Solution

  • Make sure you include AssertDifference in spec/spec_helper.rb:

    RSpec.configure do |config|
      ...   
      config.include AssertDifference
    end
    

    And put the assertion inside of an it block:

    it 'event count should change' do
      assert_difference 'Event.count' do
        ...
      end
    end