我有一個規範,像這樣:Rspec的+工廠女孩第一個例子工程,第2 +不
require 'spec_helper'
describe IncomingMailsController do
include Devise::TestHelpers
before(:each) do
@user = Factory.create(:user)
@user1 = Factory.create(:user)
@group = Factory(:group)
@perm1 = Factory.create(:permission, :user => @user)
@perm2 = Factory.create(:permission, :user => @user1)
end
it "xxxxx case 1" do
....
end
it "xxxxx case 2" do
....
end
第一種情況1,工作正常,但隨後的第二個失敗:
Failure/Error: @perm1 = Factory.create(:permission, :user => @user)
RuntimeError:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
# ./spec/factories.rb:23
不每次運行之前每次運行新鮮它阻止?
factories.rb樣子:
require 'factory_girl'
Factory.define :user do |f|
f.sequence(:fname) { |n| "fname#{n}" }
f.sequence(:lname) { |n| "lname#{n}" }
f.sequence(:email) { |n| "email#{n}@google.com" }
f.password "password"
f.password_confirmation { |u| u.password }
f.invitation_code "dmbsrv82811"
end
Factory.define :group do |f|
f.name "myGroup"
f.sequence(:private_email) { |n| "myGroup#{n}" }
end
Factory.define :permission do |f|
f.role_id 1
f.group_id do
(Group.find_by_name('myGroup')).id
end
f.creator_id do
(User.find_by_fname('fname1')).id
end
end
感謝
FactoryGirl的版本是什麼?自從這篇文章以來,FactoryGirl API是否發生了巨大變化? – 2014-06-28 23:11:57