2013-06-04 2 views
5

나는 두 개의 공장과 테스트 세트를 만드는 오전에 초기화되지 않은 상수, 아래 참조 :RSpec에 오류 나가서 설명하자면 NameError : 팩토리

사양/공장/page.rb

FactoryGirl.define do 
    factory :page do 
     title "Example Title" 
     content "Here is some sample content" 
     published_on "2013-06-02 02:28:12" 
    end 
    factory :page_invalid do 
     title "" 
     content "" 
     published_on "2013-06-02 02:28:12" 
    end 
end 

그러나, 사양/컨트롤러에

describe "with invalid params" do 
    it "does not save the new page in the database" do 
    expect { 
     post :create, {page: attributes_for(:page_invalid)}, valid_session 
     }.to_not change(Page, :count).by(1) 
    end 

오류 :

1) Api::PagesController POST create with invalid params does not save the new page in the database 
    Failure/Error: post :create, {page: attributes_for(:page_invalid)}, valid_session 
    NameError: 
     uninitialized constant PageInvalid 
    # ./spec/controllers/pages_controller_spec.rb:78:in `block (5 levels) in <top (required)>' 
    # ./spec/controllers/pages_controller_spec.rb:77:in `block (4 levels) in <top (required)>' 
0 /page_controller_spec.rb 다음 테스트에서 오류가 발생

이 코드는 Everyday Rails Rspec의 코드와 유사하므로 page_invalid 팩토리가 인식되지 않는 이유를 모르겠습니다.

답변

10

이 시도 : page_invalid 공장 :

FactoryGirl.define do 
    factory :page do 
    title "Example Title" 
    content "Here is some sample content" 
    published_on "2013-06-02 02:28:12" 
    end 
    factory :page_invalid, :class => "Page" do 
    title "" 
    content "" 
    published_on "2013-06-02 02:28:12" 
    end 
end 

주목하라 : 당신의 클래스 => 옵션을 선택합니다.

+0

감사합니다. –

+0

마운트 가능한 엔진을 테스트하는 동안 동일한 문제가있었습니다. 이것은 유용합니다. –

관련 문제