R规格+工厂女孩第一个示例作品,第二个+不要
我有一个像这样的规范:
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
谢谢
I have an spec like so:
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
The first case 1, works fine but then the 2nd fails with:
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
Does the before each run fresh for each it block?
factories.rb looks like:
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
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Factory.rb 的第 23 行,您可以
通过硬编码来查找
fname1
。但是,您的工厂使用序列来定义 fname:这意味着在第二个规范中,fname 的值将是 fname2,而不是 fname1。这是因为不同规格之间的序列不会重置。
所有这些意味着当你调用 User.find_by_fname 时,它找不到 fname1 的用户(只有 fname2),因此它返回 nil,这就是为什么你会得到被调用的 id for nil 错误。
-- 编辑 --
您是否使用传入权限工厂的用户属性执行任何操作(例如使用
Factory.create(:permission, :user => @user)
)?如果没有,我会将其更改为类似的内容注意:如果您没有将 Permission#creator 设置为与 User 的关联,您将需要这样做
类似地,您可能希望更多地定义您的工厂如果
您的关联设置正确,则无需担心 id,该关联会为您做正确的事情。
On line 23 of factories.rb you have
This is hardcoded to find
fname1
. However, your factory uses a sequence to define fname:This means that in the second spec, the value for fname will be fname2, not fname1. This is because sequences aren't reset between different specs.
All of this means that when you call User.find_by_fname, it can't find a user with fname1 (only fname2), so it returns nil, which is why you get the called id for nil error.
-- Edit --
Are you doing anything with the user attribute you pass in to the permission factory (for eg with
Factory.create(:permission, :user => @user)
)? If not, I'd change it to something likeNote: if you don't have Permission#creator setup as an association with User you'll need to rather do
On a similar vein, you might want to define your factory more along the lines of
If your associations are setup correctly, there's no need to worry about the ids, the association does the Right Thing for you.