我从 Rails 和 RSpec 开始,在使用 FactoryGirl 时遇到了麻烦。基本上该对象不接收属性。
模型/用户.rb
class User < ActiveRecord::Base
attr_accessor :name, :email, :show_name, :company, :identity, :password, :phone, :mobile
validates_presence_of :name, :show_name, :email, :company, :identity, :password, :phone
validates_uniqueness_of :email
end
规格/工厂.rb
FactoryGirl.define do
factory :user do
name "John Doe"
email "john@example.org"
show_name "John Doe"
company "Company"
identity "123.456.789-10"
password "abc123"
phone "1234-5678"
mobile "1234-5678"
end
end
Rails 控制台
irb(main):001:0> u = FactoryGirl.create(:user)
(0.1ms) BEGIN
User Exists (0.3ms) SELECT 1 FROM `users` WHERE `users`.`email` = BINARY 'john@example.org' LIMIT 1
SQL (0.4ms) INSERT INTO `users` (`company`, `email`, `identity`, `mobile`, `name`, `password`, `phone`, `show_name`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Mysql2::Error: Column 'company' cannot be null: INSERT INTO `users` (`company`, `email`, `identity`, `mobile`, `name`, `password`, `phone`, `show_name`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
(0.1ms) ROLLBACK
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'company' cannot be null: INSERT INTO `users` (`company`, `email`, `identity`, `mobile`, `name`, `password`, `phone`, `show_name`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
有人知道怎么了?