2012-03-15 19 views
5
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 

Ho cercato Google per questo errore ma non ho trovato nulla per risolvere il problema. Aiutatemi, per favore. Grazie :)RSpec: metodo non definito `assert_difference 'per ... (NoMethodError)

+0

Sembra che tu stia utilizzando la gemma assert_difference con Rspec, corretto? Penso che sia necessario avvolgerlo in un blocco 'it'. –

+0

Cerco di inserirlo nel blocco, ma ho ancora questo errore –

risposta

4

Assicurati di includere AssertDifference in spec/spec_helper.rb:

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

E mettere l'affermazione all'interno di un blocco di it:

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

Oh, ha un errore quando si aggiunge "config.include AssertDifference" spec_helper.rb: 43: in 'block (2 livelli) in ': costante non inizializzata AssertDifference (NameError) –

+1

Hai aggiunto 'gem' assert_difference'' al tuo Gemfile? –

+1

Hai ragione, l'ho dimenticato: D –

4

farei meglio riscrittura che utilizzando change.

Questo funziona sicuramente in RSpec 3.x, ma probabilmente anche nelle versioni precedenti.

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 
5

Se si utilizza RSPEC, sicuramente "cambiare" dovrebbe essere la strada da percorrere. Ecco due esempi, uno negativo e uno positivo, in modo da avere un senso della sintassi:

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:     "[email protected]", 
      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 
Problemi correlati