2013-06-04 10 views
5

私は2つの工場で一連のテストを作成していますに初期化されていない定数、以下を参照: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

マウント可能なエンジンのテスト中に同じ問題が発生しました。これは便利です。 –

関連する問題