8

私は次のような工場を持っています:

FactoryGirl.define do
  factory :page do
    title 'Fake Title For Page'
  end
end

そしてテスト:

describe "LandingPages" do
    it "should load the landing page with the correct data" do
        page = FactoryGirl.create(:page)
        visit page_path(page)
    end
end

私のspec_helper.rbには次のものが含まれています。

require 'factory_girl_rails' 

それでも私は取得し続けます:

LandingPages should load the landing page with the correct data
     Failure/Error: page = FactoryGirl.create(:page)
     NameError:
       uninitialized constant Page
     # ./spec/features/landing_pages_spec.rb:5:in `block (2 levels) in <top (required)>'

これは新しいプロジェクトなので、テストが実際に問題になるとは思いません。正しく設定されていない可能性があると思います。試すべきことや、これを解決するためにどこを見ればよいかについてのアイデアはありますか?

私の問題のないpages.rbファイル:

class Pages < ActiveRecord::Base
  # attr_accessible :title, :body
end
4

2 に答える 2

23

ファイル名からは、モデルの名前が実際にはLandingPageのように見えます。工場はあなたが付けた名前に基づいてあなたのクラス名を推測しようとしています。したがって、:pageはPageになります。

ファクトリの名前を変更するか、明示的なクラスオプションを追加できます。

FactoryGirl.define do
  factory :landing_page do
    title 'Fake Title For Page'
  end
end

また

FactoryGirl.define do
  factory :page, :class => LandingPage do
    title 'Fake Title For Page'
  end
end
于 2013-01-18T00:14:40.557 に答える
6

モデルの名前が複数形のようです:Pages。これは本当に特異なはずです:Page。ファイルの名前も変更する必要がありますapp/models/page.rb。FactoryGirlは、単一のモデル名を想定しています。

于 2013-01-18T01:01:07.017 に答える