工厂女孩威金'超越关联(无限循环,也许?)

发布于 2024-10-22 05:22:30 字数 2312 浏览 1 评论 0原文

我现在拥有的:

class User < ActiveRecord::Base
   has_many :people
end

...和...

class Person < ActiveRecord::Base
   belongs_to :user
end

spec/factories.rb 中:

Factory.define :user do |u|
  u.email "[email protected]"
  u.password "testpassword"
  u.password_confirmation "testpassword"
  u.display_name "neezer"
  # u.people { |i| [i.association(:person)] }
end

Factory.define :person do |p|
  p.first_name "p_firstname"
  p.last_name "p_lastname"
  p.gender "male"
  p.association :user
end

我想设置用户工厂以创建 1 人关联,但是如果我取消注释该行,当我运行时我的测试,我的系统挂起相当长一段时间,然后输出此失败:

1) User can be created from a factory
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /Users/test/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.5/lib/active_record/persistence.rb:285

我在这里做错了什么?我想要进行需要这两个模型之间关联的测试,例如 (1) User 必须至少有 1 个人,并且 (2) Person 必须属于用户

这是首要问题吗?我承认我在这里有点迷失...


我正在使用 rspec 2.5.0factory_girl_rails 1.0.1rails 3.0.5


我的规格:

user_spec.rb

require 'spec_helper'

describe User do

  subject { Factory :user }

  # ...

  context "has associations, " do

    it "can have people" do
      subject.should respond_to :people
    end

    it "must have at least 1 person" do
      subject.send "people=", nil
      subject.should_not be_valid
      subject.errors[:people].should_not be_empty
    end

  end

end

person_spec.rb

require 'spec_helper'

describe Person do

  subject { Factory :person }

  # ...

  context "has validation, " do

    [:gender, :user].each do |attr|

      it "must have a #{ attr }" do
        subject.send "#{attr}=", nil
        subject.should_not be_valid
        subject.errors[attr].should_not be_empty
      end

    end

  end

  context "has associations, " do

    it "can have a User" do
      subject.should respond_to :user
    end

  end

end

What I have now:

class User < ActiveRecord::Base
   has_many :people
end

... and...

class Person < ActiveRecord::Base
   belongs_to :user
end

In spec/factories.rb:

Factory.define :user do |u|
  u.email "[email protected]"
  u.password "testpassword"
  u.password_confirmation "testpassword"
  u.display_name "neezer"
  # u.people { |i| [i.association(:person)] }
end

Factory.define :person do |p|
  p.first_name "p_firstname"
  p.last_name "p_lastname"
  p.gender "male"
  p.association :user
end

I want to setup the user factory to create with 1 person association, but if I uncomment that line, when I run my tests, my system hangs for quite some time, before outputting this failure:

1) User can be created from a factory
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /Users/test/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.5/lib/active_record/persistence.rb:285

What am I doing wrong here? I would like to have tests that require an association between these two models, such that (1) a User must have at least 1 person, and (2) a Person must belong to a User.

Is this a first-priority issue? I'll admit I'm a bit lost here...


I'm using rspec 2.5.0, factory_girl_rails 1.0.1, and rails 3.0.5.


My specs:

user_spec.rb:

require 'spec_helper'

describe User do

  subject { Factory :user }

  # ...

  context "has associations, " do

    it "can have people" do
      subject.should respond_to :people
    end

    it "must have at least 1 person" do
      subject.send "people=", nil
      subject.should_not be_valid
      subject.errors[:people].should_not be_empty
    end

  end

end

person_spec.rb:

require 'spec_helper'

describe Person do

  subject { Factory :person }

  # ...

  context "has validation, " do

    [:gender, :user].each do |attr|

      it "must have a #{ attr }" do
        subject.send "#{attr}=", nil
        subject.should_not be_valid
        subject.errors[attr].should_not be_empty
      end

    end

  end

  context "has associations, " do

    it "can have a User" do
      subject.should respond_to :user
    end

  end

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

昔日梦未散 2024-10-29 05:22:30

保留该行,但从您的个人工厂中删除 p.association :user

Keep that line but remove p.association :user from your person factory.

花开浅夏 2024-10-29 05:22:30

我后来发现了 Shoulda,它提供了一个很好的 rspec 匹配器,如下所示:

subject.should belong_to :user
subject.should have_many :people

这解决了我的问题。

I've since discovered Shoulda, which provides a nice rspec matchers like these:

subject.should belong_to :user
subject.should have_many :people

Which has solved my issue.

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