工厂女孩威金'超越关联(无限循环,也许?)
我现在拥有的:
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.0
、factory_girl_rails 1.0.1
和 rails 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保留该行,但从您的个人工厂中删除
p.association :user
。Keep that line but remove
p.association :user
from your person factory.我后来发现了 Shoulda,它提供了一个很好的 rspec 匹配器,如下所示:
这解决了我的问题。
I've since discovered Shoulda, which provides a nice rspec matchers like these:
Which has solved my issue.