【发布时间】:2014-02-08 04:30:06
【问题描述】:
我希望有人能够帮助我解决我在尝试使用 factory_girl 时收到的未初始化的持续错误的问题。我知道堆栈溢出上已经发布了类似的问题,我试图在这些帖子中找到解决方案,但无济于事。
我有一个非 Rails 项目,我正在使用 rspec 进行测试。我希望使用 factory_girl 创建测试数据。
所以我在 spec/factories 文件夹中名为 users.rb 的文件中定义了我的工厂。工厂如下:
FactoryGirl.define do
factory :user do
email 'root@test.com'
password 'TestPass'
end
结束
然后我有一个测试:
require 'spec_helper'
describe "login tests" do
it "should allow me to login" do
visit('http://mytestapp.com')
user = FactoryGirl.build(:user)
login(user.email, user.password)
should have_content('Welcome!')
end
end
在我的规范助手中,我有
require 'factory_girl'
require_rel('factories')
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
但是,当我运行测试时,我收到错误消息“未初始化的常量用户”
请注意,在 spec_helper 文件中,我使用 gem reqiure_rel 来查找 factory 文件夹。我尝试使用 FactoryGirl.find_definitions 但是当我使用它时,我收到错误消息“工厂未注册:用户”。因此,使用 require_rel 似乎至少找到了工厂。在我的测试文件中,我也尝试过使用这个:
user = FactoryGirl.create(:user)
但我收到相同的错误消息。
有人知道我做错了什么吗?非常感谢您的帮助!
谢谢!
【问题讨论】:
标签: ruby rspec capybara factory-bot rspec2