Noa*_*ark 8 ruby-on-rails rspec2 ruby-on-rails-3 factory-bot
我有一个工厂,如:
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
Dan*_*ans 23
它从您的文件名看起来像模型实际上名为LandingPage.工厂正在尝试根据您提供的名称猜测您的班级名称.所以:页面变成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
看起来您的模型名称是复数:Pages.这应该是单数的:Page.您还需要将文件重命名app/models/page.rb为.FactoryGirl假设一个单一的模型名称.
| 归档时间: | 
 | 
| 查看次数: | 10318 次 | 
| 最近记录: |