R规格+工厂女孩第一个示例作品,第二个+不要

发布于 2024-10-25 07:18:45 字数 1446 浏览 0 评论 0原文

我有一个像这样的规范:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-11-01 07:18:45

在 Factory.rb 的第 23 行,您可以

(User.find_by_fname('fname1')).id

通过硬编码来查找 fname1。但是,您的工厂使用序列来定义 fname:

f.sequence(:fname) { |n| "fname#{n}" }

这意味着在第二个规范中,fname 的值将是 fname2,而不是 fname1。这是因为不同规格之间的序列不会重置。

所有这些意味着当你调用 User.find_by_fname 时,它​​找不到 fname1 的用户(只有 fname2),因此它返回 nil,这就是为什么你会得到被调用的 id for nil 错误。

-- 编辑 --

您是否使用传入权限工厂的用户属性执行任何操作(例如使用 Factory.create(:permission, :user => @user))?如果没有,我会将其更改为类似的内容

Factory.create(:permission, :creator => @user)

注意:如果您没有将 Permission#creator 设置为与 User 的关联,您将需要这样做

Factory.create(:permission, :creator_id => @user.id)

类似地,您可能希望更多地定义您的工厂如果

Factory.define :permission do |f|
  f.role_id         1
  f.association :group, :factory => :group
  f.association :creator, :factory => :user
end

您的关联设置正确,则无需担心 id,该关联会为您做正确的事情。

On line 23 of factories.rb you have

(User.find_by_fname('fname1')).id

This is hardcoded to find fname1. However, your factory uses a sequence to define fname:

f.sequence(:fname) { |n| "fname#{n}" }

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 like

Factory.create(:permission, :creator => @user)

Note: if you don't have Permission#creator setup as an association with User you'll need to rather do

Factory.create(:permission, :creator_id => @user.id)

On a similar vein, you might want to define your factory more along the lines of

Factory.define :permission do |f|
  f.role_id         1
  f.association :group, :factory => :group
  f.association :creator, :factory => :user
end

If your associations are setup correctly, there's no need to worry about the ids, the association does the Right Thing for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文