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

4

8
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 :)

Pert answered 15/3, 2012 at 2:42 Comment(2)
It looks like you're using the assert_difference gem with Rspec, correct? I think you need to wrap it in an it block.Amputee
I try to place it in block it, but still have this errorPert
A
4

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
Amputee answered 15/3, 2012 at 19:55 Comment(2)
Oh, it have error when add "config.include AssertDifference" spec_helper.rb:43:in `block (2 levels) in <top (required)>': uninitialized constant AssertDifference (NameError)Pert
Have you added gem 'assert_difference' to your Gemfile?Amputee
D
10

If you are using RSPEC, definitely 'change' should be the way to go. Here are two examples a negative and a positive one so you can have a sense of the syntax:

RSpec.describe "UsersSignups", type: :request do
  describe "signing up with invalid information" do
    it "should not work and should go back to the signup form" do
      get signup_path
      expect do
        post users_path, user: { 
          first_name:            "",
          last_name:             "miki",
          email:                 "user@triculi",
          password:              "buajaja",
          password_confirmation: "juababa" 
        }
      end.to_not change{ User.count }
      expect(response).to render_template(:new)
      expect(response.body).to include('errors')
    end
  end

  describe "signing up with valid information" do
    it "should work and should redirect to user's show view" do
      get signup_path
      expect do
        post_via_redirect users_path, user: { 
          first_name:            "Julito",
          last_name:             "Triculi",
          email:                 "[email protected]",
          password:              "worldtriculi",
          password_confirmation: "worldtriculi"
        }
      end.to change{ User.count }.from(0).to(1)
      expect(response).to render_template(:show)
      expect(flash[:success]).to_not be(nil)
    end
  end
Dislodge answered 26/3, 2015 at 12:20 Comment(0)
F
6

I'd better rewrite that using change.

That's work for sure in RSpec 3.x, but probably in older versions as well.

context 'with event_type is available create event' do
  let(:event_type) { EventType.where( name: 'visit_site').first }

  it "changes event counter" do
    expect { Event.fire_event(event_type, @sponge,{}) }.to change { Event.count }
  end
end # with event_type is available create event
Fork answered 23/6, 2014 at 13:12 Comment(0)
A
4

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
Amputee answered 15/3, 2012 at 19:55 Comment(2)
Oh, it have error when add "config.include AssertDifference" spec_helper.rb:43:in `block (2 levels) in <top (required)>': uninitialized constant AssertDifference (NameError)Pert
Have you added gem 'assert_difference' to your Gemfile?Amputee
P
2

If you're trying to use assert_difference from ActiveSupport::Testing::Assertions module with Rspec, All you need to do is insert the below code inside the rails_helper.rb file.

RSpec.configure do |config|
  config.include ActiveSupport::Testing::Assertions
end

I've tested this with Rails 5.2.0. But, I'm assuming this should work with other versions as well.

Puck answered 27/11, 2019 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.