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

Tôi đã tìm kiếm lỗi này nhưng không tìm thấy lỗi nào. Giúp tôi. Cảm ơn bạn :)RSpec: Phương thức không xác định `assert_difference 'cho ... (NoMethodError)

+0

Có vẻ như bạn đang sử dụng đá quý assert_difference với Rspec, đúng không? Tôi nghĩ bạn cần bọc nó trong một khối 'nó'. –

+0

Tôi cố gắng để đặt nó trong khối nó, nhưng vẫn có lỗi này –

Trả lời

4

Hãy chắc chắn rằng bạn bao gồm AssertDifference trong spec/spec_helper.rb:

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

Và đặt sự khẳng định bên trong một khối it:

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

Ồ, nó có lỗi khi thêm "config.include AssertDifference" spec_helper.rb: 43: trong 'block (2 levels) trong ': unertialized hằng số AssertDifference (NameError) –

+1

Bạn đã thêm 'gem' assert_difference'' vào Gemfile của bạn chưa? –

+1

Bạn nói đúng, tôi đã quên nó: D –

4

tôi muốn viết lại tốt hơn mà sử dụng change.

Đó là công việc chắc chắn trong RSpec 3.x, nhưng có thể trong các phiên bản cũ hơn.

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

Nếu bạn đang sử dụng RSPEC, chắc chắn 'thay đổi' sẽ là cách để thực hiện. Dưới đây là hai ví dụ tiêu cực và tích cực để bạn có thể hiểu cú pháp:

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 
Các vấn đề liên quan