RSpec:具有关系的资源的controller_specs

发布于 2025-01-06 17:27:07 字数 1693 浏览 0 评论 0原文

我知道这是一个基本问题,但有太多令人困惑、过时的问题。那里的信息相互矛盾...我已经设法扰乱我的大脑...如何最好地从孩子的控制器规范访问父模型...使用 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 技术交流群。

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

发布评论

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

评论(1

桃酥萝莉 2025-01-13 17:27:07

你可以尝试吗:

factory :ballot do
  association :user, :factory => :sequenced_user
end

FactoryGirl 似乎正在尝试制作 ballot.sequenced_user,而你只需要 ballot.user

Can you try:

factory :ballot do
  association :user, :factory => :sequenced_user
end

It seems like FactoryGirl was trying to make ballot.sequenced_user, when you want just ballot.user

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