RSpec: Недефиниран метод „assert_difference“ за (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

Търсих в Google тази грешка, но не намерих нищо, което да я поправи. Помогнете ми моля. Благодаря ти :)


person Peter89    schedule 15.03.2012    source източник
comment
Изглежда, че използвате скъпоценния камък assert_difference с Rspec, нали? Мисля, че трябва да го увиете в it блок.   -  person Tom L    schedule 15.03.2012
comment
Опитвам се да го поставя в блок, но все още имам тази грешка   -  person Peter89    schedule 15.03.2012


Отговори (4)


Уверете се, че сте включили AssertDifference в spec/spec_helper.rb:

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

И поставете твърдението вътре в блок it:

it 'event count should change' do
  assert_difference 'Event.count' do
    ...
  end
end
person Tom L    schedule 15.03.2012
comment
О, има грешка при добавяне на config.include AssertDifference spec_helper.rb:43:в `блок (2 нива) в ‹top (задължително)›': неинициализирана константа AssertDifference (NameError) - person Peter89; 16.03.2012
comment
Добавихте ли gem 'assert_difference' към вашия Gemfile? - person Tom L; 16.03.2012

Ако използвате RSPEC, определено „промяната“ трябва да е правилният начин. Ето два примера, отрицателен и положителен, за да имате представа за синтаксиса:

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
person drjorgepolanco    schedule 26.03.2015


Ако се опитвате да използвате assert_difference от ActiveSupport::Testing::Assertions модул с Rspec, всичко, което трябва да направите, е да вмъкнете кода по-долу във файла rails_helper.rb.

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

Тествах това с Rails 5.2.0. Но предполагам, че това трябва да работи и с други версии.

person Abhilash Reddy    schedule 27.11.2019