2012-03-15 5 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에서이 오류를 검색했지만 해결 방법이 없습니다. 도와주세요. 당신이 사양/spec_helper.rb에 AssertDifference을 포함해야합니다 :RSpec : 정의되지 않은 메소드 'assert_difference'... (NoMethodError)

+0

를? 나는 그것을'it' 블록으로 감쌀 필요가 있다고 생각한다. –

+0

나는 그것을 블록에 넣으려고하지만 여전히이 오류가있다. –

답변

4

감사 :

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 : 블록 (2 레벨) ': 초기화되지 않은 상수 AssertDifference (NameError) –

+1

gemfile에'gem'assert_difference''을 추가 했습니까? –

+1

맞아, 잊어 버렸어. D –

4

내가 더 나은 재 작성을 거라고 change을 사용하십시오.

RSpec 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을 사용하는 경우 분명히 '변경'이 있어야합니다. 여기에 두 가지 예는 부정적이고 긍정적 인 사람은 그래서 당신은 구문의 감각 할 수 있습니다 : 당신이 올바른 RSpec에와 assert_difference 보석을 사용하고있는 것 같습니다

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 
관련 문제