[英]How can I setup and use factory_girl_rails properly?
I think I have a problem with configuring FactoryGirl with rails. 我认为使用Rails配置FactoryGirl时遇到问题。 I initially followed ASCIIcasts #275: how i test , but rake is giving me NameError: uninitialized constant ...
我最初遵循ASCIIcasts#275:如何进行测试 ,但瑞克给了我NameError: uninitialized constant ...
Am I missing something? 我想念什么吗? Is it possible that some config file are wrong? 某些配置文件是否可能出错? I'm pretty new to RSpec and Rails. 我对RSpec和Rails很陌生。
I'm using Rails 3.2.2 + Mongoid + RSpec + factory_girl_rails . 我使用Rails 3.2.2 + Mongoid + 的RSpec + factory_girl_rails。
Error: 错误:
Failures:
1) User should save user with valid required fields
Failure/Error: let(:user) { FactoryGirl.build(:valid_user) }
NameError:
uninitialized constant ValidUser
# ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
# ./spec/models/user_spec.rb:7:in `block (2 levels) in <top (required)>'
spec/factories.rb spec / factories.rb
FactoryGirl.define do
factory :valid_user do
name 'somename'
email 'a@b.com'
password 'somepassword'
end
end
spec/models/user_spec.rb 规格/型号/user_spec.rb
require 'spec_helper'
describe User do
let(:user) { FactoryGirl.build(:valid_user) }
it "should save user with valid required fields" do
user.should be_valid
end
end
spec/spec_helper.rb spec / spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = false
config.include FactoryGirl::Syntax::Methods
end
It's usually helpful to output the whole error, or at least the whole first sentence -- you haven't even told us what the missing constant is! 输出整个错误或至少整个第一句话通常会很有帮助-您甚至都没有告诉我们缺失的常量是什么!
UPDATE: Thanks for the whole error. 更新:感谢您的整个错误。 When you define the factory :valid_user, Factory Girl will automatically assume it is for a model named ValidUser
. 当您定义factory:valid_user时,Factory Girl将自动假定它适用于名为ValidUser
的模型。 To get around this, you can either name your factory :user
(assuming you have a User
model), or you can try: 要解决此问题,您可以命名factory :user
(假设您有一个User
模型),也可以尝试:
FactoryGirl.define do
factory :valid_user, :class => User do
name 'somename'
email 'a@b.com'
password 'somepassword'
end
end
Alternatively, if you want to have a couple different types of User factories, you can use: 另外,如果您要使用几种不同类型的User工厂,则可以使用:
FactoryGirl.define do
factory :user do
# set some attrs
end
factory :valid_user, :parent => :user do
name 'somename'
email 'a@b.com'
password 'somepassword'
end
factory :invalid_user, :parent => :user do
# some other attrs
end
end
You can declare factory like this........ 您可以这样声明工厂.....
Factory.define :organization do |g|
g.name 'Test Organization'
g.phone_number '5345234561'
g.website_url 'www.testorg.com'
g.city 'chichago '
g.association :state
end
And use it in organization_spec like this..... 并像这样在organization_spec中使用它。
require 'spec_helper'
describe Organization do
before :each do
@state = Factory :state
@organization = Factory :organization ,:state => @state
end
it "should be invalid without a name" do
@organization.name = nil
@organization.should_not be_valid
end
end
And enjoy!!!!!!!!!!!!!!!! 享受!!!!!!!!!!!!!!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.