2016-09-07 4 views
0

이 레일에서 테스트하는 방법을 배우려는 tutorial. 튜토리얼의 한 부분에rspec 및 factorygirl을 사용하여 invalid_attribute를 테스트하는 방법

, 그것은 invalid_attribute 테스트를 작성하는 방법을 보여줍니다

require 'rails_helper' 

RSpec.describe ContactsController, type: :controller do 

    describe "POST #create" do 
    context "with valid attributes" do 
     it "create new contact" do 
     post :create, contact: attributes_for(:contact) 
     expect(Contact.count).to eq(1) 
     end 
    end 

    context "with invalid attributes" do 
     it "does not create new contact" do 
     post :create, contact: attributes_for(:invalid_contact) 
     expect(Contact.count).to eq(0) 
     end 
    end 
    end 
end 

가 이해가 안 어디 :contact:invalid_contact 점이다.

:contactContact 클래스를 가리 킵니까? 그것은 FactoryGirl's gh.에서 그런 것 같아요 :invalid_contact 클래스가 없으므로 그렇다면 어떻게 :invalid_contact을 만들 수 있습니까?

나는 post :create, contact: attributes_for(:contact, :full_name => nil)을 시도했지만 여전히 실패합니다.

spec/factories/contacts.rb :

FactoryGirl.define do 
    factory :contact do 
    full_name  { Faker::Name.name } 
    email   { Faker::Internet.email } 
    phone_number { Faker::PhoneNumber.phone_number } 
    address  { Faker::Address.street_address } 
    end 
end 

먼저 테스트 with valid attributes 통과. 모델에는 존재 확인 validates_presence_of :full_name, :email, :phone_number, :address이 있습니다. "with invalid attributes" 시험에 합격하기 위해서는 무엇을 추가해야합니까?

+1

': contact'는 Factory Girl로 만든 팩토리이며': invalid_contact' , https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md를 보시고 그것을 만드는 방법을보실 수 있습니다. – fanta

+0

할거야. 포인터 @ 주셔서 고마워! – Iggy

답변

2

동일한 이름의 클래스가 공장에서 사용됩니다. 따라서 :contact 공장에서는 Contact 클래스를 사용하게됩니다. 사용할 클래스를 지정하여 유효하지 않은 문의처에 대한 새 팩토리를 작성할 수 있습니다.

factory :invalid_contact, class: Contact do 
    full_name nil 
end 

두 개의 다른 공장을 피하기 위해 형질을 사용할 수도 있습니다.

FactoryGirl.define do 
    factory :contact do 
    full_name  { Faker::Name.name } 
    email   { Faker::Internet.email } 
    phone_number { Faker::PhoneNumber.phone_number } 
    address  { Faker::Address.street_address } 

    trait :invalid do 
     full_name nil 
    end 
    end 
end 

그런 attributes_for(:contact, :invalid)

+0

첫 번째 코드는 작동하지만'trait'은 작동하지 않습니다.오류가 발생했습니다 : 오류/오류 : 게시 : 생성 : 연락처 : attributes_for (: invalid_contact) ArgumentError : 공장 등록되지 않았습니다 : invalid_contact' – Iggy

+1

'trait'을 사용하려면'attributes_for (: invalid_contact) ':'attributes_for (: contact, : invalid)' –

1

당신이 연결 자습서와 함께 사용 말한다 :

Following the spec above, write a spec that uses invalid attributes to create a new contact. This spec should check that the contact is not created.

그래서 당신이 :contact에 대한 예를 사용 :invalid_contact을 테스트하는 방법을 알아낼 필요가있다.

당신은 당신의 사양에 let을 추가 할 수 있습니다

Use let to define a memoized helper method. The value will be cached across multiple calls in the same example but not across examples.

Source: https://www.relishapp.com/rspec/rspec-core/v/3-5/docs/helper-methods/let-and-let

그런 다음 컨트롤러의 사양은 다음과 같이 보일 것이다 :

... 
let(:invalid_contact) { create(:contact, name: nil) } 

context "with invalid attributes" do 
    it "does not create new contact" do 
    post :create, contact: attributes_for(invalid_contact) 
    expect(Contact.count).to eq(0) 
    end 
end 
... 

행동 paramsinvalid_contact

에서 선택됩니다 #post이 방법을

또는 @ fanta 님이 의견에 제안한대로 trait을 fac에 추가 할 수 있습니다. 노상 강도. 내 방법을 선호하는 이유는 다른 사람들이 코드를보고 을 보지 않고 무효화해야하는 이유를 알기 때문입니다.

+0

다음 오류가 발생했습니다 :'ArgumentError : 공장 등록되지 않았습니다 : invalid_contact' http : //에 제안 된'config.before (: all) : FactoryGirl.reload ...' //stackoverflow.com/questions/24078768/argumenterror-factory-not-registered하지만 여전히 실패하고 있습니다 : ( – Iggy

+0

oops,'attributes_for :'에서 arg를 편집하는 것을 잊었습니다. –