RSpec:具有关系的资源的controller_specs
我知道这是一个基本问题,但有太多令人困惑、过时的问题。那里的信息相互矛盾...我已经设法扰乱我的大脑...如何最好地从孩子的控制器规范访问父模型...使用 FactoryGirl?
它可能与在子模型 (to_params) 中使用父模型 (user.name) 的值部分相关。这对我来说效果很好。我只是很难测试它:
伪模型:
User
:name - string - unique
:email - string - unique
has_many :ballots
Ballot
belongs_to :user
has_many :items
def to_param #
"#{id} #{user.name}".parameterize
end
factorys.rb:
FactoryGirl.define do
sequence :email do |n|
"person#{n}@fake_mail.com"
end
sequence :name do |n|
"Person#{n}"
end
factory :user do
name "Joe Schmo"
email
end
factory :sequenced_user, :parent => :user do
name
email
end
factory :ballot do
sequenced_user
end
ballots_controller_spec.rb:
describe BallotsController do
def valid_attributes
FactoryGirl.attributes_for(:ballot)
end
describe "GET show" do
it "assigns the requested ballot as @ballot" do
ballot = Ballot.create! valid_attributes
get :show, {:id => ballot.to_param}, valid_session
assigns(:ballot).should eq(ballot)
end
end
...
end
规范失败:
1) BallotsController GET show assigns the requested ballot as @ballot
Failure/Error: get :show, {:id => ballot.to_param}, valid_session
NoMethodError:
undefined method `name' for nil:NilClass
# ./app/models/ballot.rb:5:in `to_param'
# ./spec/controllers/ballots_controller_spec.rb:53:in `block (3 levels) in <top (required)>'
...
问题似乎是,正在运行的规范无法访问的名称
与选票关联的用户。但是,选票工厂里有一个关联用户!?!那么,使用 FactoryGirl 生成这种关系的正确方法是什么?
I know this is a basic question but there's so much confusing, outdated & contradicting information out there...I've managed to scramble my brain... How best to access parent model from a child's controller spec...using FactoryGirl?
It may be partially related to using the value from parent model (user.name) in the (to_params) of the child. This is working fine for me. I'm just having a hard time testing it:
Pseudo models:
User
:name - string - unique
:email - string - unique
has_many :ballots
Ballot
belongs_to :user
has_many :items
def to_param #
"#{id} #{user.name}".parameterize
end
factories.rb:
FactoryGirl.define do
sequence :email do |n|
"person#{n}@fake_mail.com"
end
sequence :name do |n|
"Person#{n}"
end
factory :user do
name "Joe Schmo"
email
end
factory :sequenced_user, :parent => :user do
name
email
end
factory :ballot do
sequenced_user
end
ballots_controller_spec.rb:
describe BallotsController do
def valid_attributes
FactoryGirl.attributes_for(:ballot)
end
describe "GET show" do
it "assigns the requested ballot as @ballot" do
ballot = Ballot.create! valid_attributes
get :show, {:id => ballot.to_param}, valid_session
assigns(:ballot).should eq(ballot)
end
end
...
end
spec failure:
1) BallotsController GET show assigns the requested ballot as @ballot
Failure/Error: get :show, {:id => ballot.to_param}, valid_session
NoMethodError:
undefined method `name' for nil:NilClass
# ./app/models/ballot.rb:5:in `to_param'
# ./spec/controllers/ballots_controller_spec.rb:53:in `block (3 levels) in <top (required)>'
...
Problem, appears to be, that the running spec doesn't access to the name
of the User that the ballot is associated with. But, the Ballot factory has an associated user in it!?! So, what's the correct way to generate this relationship using FactoryGirl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试吗:
FactoryGirl 似乎正在尝试制作
ballot.sequenced_user
,而你只需要ballot.user
Can you try:
It seems like FactoryGirl was trying to make
ballot.sequenced_user
, when you want justballot.user