我正在关注this tutorial,开始与工厂女孩一起使用TDD on rails,rspec和我遇到了这个问题我想不通。
这是我的“工厂”.rb (events.rb)
require 'faker'
FactoryGirl.define do
factory :event do
name "HIGH"
genre "house, techno, idb"
venue_name "Westbourne Studios"
venue_address "4-6 Chamberlayne Road"
venue_postcode "NW103JD"
begin_time "10pm"
end_time "2am"
user_id 2
description "A Super massive party with loads of everything you would want around."
status true
venue_id nil
end
end
下面是event_spec.rb:
require 'spec_helper'
require 'factory_girl_rails'
describe Event do
it "has a valid factory" do
Factory.create(:event).should be_valid
end
it "is invalid without a name"
it "is invalid without a genre"
it "is invalid without a venue_name"
it "is invalid without a venue_address"
it "is invalid without a venue_postcode"
...
end
我已经建立了模型,迁移了等。当我运行"rspec spec/model/event_spec.rb“时,我得到以下错误:
Failures:
1) Event has a valid factory
Failure/Error: Factory.create(:event).should be_valid
NameError:
uninitialized constant Factory
# ./spec/models/event_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.1682 seconds
13 examples, 1 failure, 12 pending
Failed examples:
rspec ./spec/models/event_spec.rb:6 # Event has a valid factory
Randomized with seed 64582
发布于 2012-09-12 06:07:18
试着这样使用它:
FactoryGirl.create(:event).should be_valid
我想,我记得,这只是老版本的宝石中的“工厂”。如果你看看最近的“工厂女孩”的"Getting started" guide,里面只有"FactoryGirl“的电话。
发布于 2012-11-23 22:58:29
如果创建包含以下内容的文件spec/support/factory_girl.rb:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
然后,您可以简单地使用:
create(:event)
build(:book)
而不是:
FactogyGirl.create(:event)
FactogyGirl.build(:book)
发布于 2017-11-08 14:22:03
我在使用factory bot时也犯了同样的错误,为了补充Stefan的答案,我偶然发现了这个小抄。
https://stackoverflow.com/questions/12378542
复制相似问题