2012-03-15 15 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 

मैंने इस त्रुटि के लिए Google की खोज की लेकिन इसे ठीक करने के लिए कुछ भी नहीं मिला। कृपया मेरी सहायता करें। धन्यवाद :)आरएसपीसी: के लिए अपरिभाषित विधि 'assert_difference' ... (NoMethodError)

+0

ऐसा लगता है कि आप Rspec के साथ assert_difference मणि, सही उपयोग कर रहे हैं? मुझे लगता है कि आपको इसे 'इसे' ब्लॉक में लपेटने की ज़रूरत है। –

+0

मैं इसे ब्लॉक में रखने की कोशिश करता हूं, लेकिन अभी भी यह त्रुटि –

उत्तर

4

सुनिश्चित करें कि आप कल्पना/spec_helper.rb में AssertDifference को शामिल करें:

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

और एक it ब्लॉक के अंदर जोर डाल:

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

है, ओह, "config.include AssertDifference" जोड़ने पर त्रुटि होती है spec_helper.rb: 43: ': uninitialized निरंतर AssertDifference (NameError) –

+1

क्या आपने अपने Gemfile में' मणि 'assert_difference'' जोड़ा है? –

+1

आप सही, मैं इसे भूल गया: डी –

4

मैं बेहतर पुनर्लेखन चाहते कि change का उपयोग कर।

यह आरएसपीसी 3.x में निश्चित रूप से काम करता है, लेकिन शायद पुराने संस्करणों में भी।

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

यदि आप आरएसपीईसी का उपयोग कर रहे हैं, तो निश्चित रूप से 'परिवर्तन' जाने का रास्ता होना चाहिए। यहां दो उदाहरण एक नकारात्मक कर रहे हैं और एक सकारात्मक एक तो तुम वाक्य रचना का अनुभव हो सकता है:

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 
संबंधित मुद्दे