【问题标题】:Same Issue in Two Tests: Something Wrong With Events两个测试中的相同问题:事件有问题
【发布时间】:2012-08-29 16:15:30
【问题描述】:

我认为在我的测试环境中创建事件有问题。

当我在浏览器中导航时,一切都很好。

我得到的两个错误是:

 1) Error:
test_should_post_save_period(PeriodRegistrationsControllerTest):
NoMethodError: undefined method `event' for nil:NilClass

 2) Error:
test_should_get_index(PeriodsControllerTest):
ActionView::Template::Error: undefined method `name' for nil:NilClass

错误 1 ​​测试:

  def setup
    @period_registration= FactoryGirl.create(:period_registration)
  end
  test "should post save_period" do
    sign_in(FactoryGirl.create(:user))
     assert_difference('PeriodRegistration.count') do
      post :save_period, period_registration: FactoryGirl.attributes_for(:period_registration)
    end
    assert_not_nil assigns(:period_registration)

    # assert_response :success
  end

错误2测试:

 test "should get index" do
        sign_in(FactoryGirl.create(:user, admin: true))
        get :index
        assert_not_nil assigns(:periods)
        assert_response :success
      end

第一个错误对应控制器中的这个动作:

 def save_period
    @period_registration = PeriodRegistration.new(params[:registration])
    @period_registration.save
    flash[:success] = "Successfully Registered for Session."
    redirect_to event_url(@period_registration.period.event) #problem line
  end

第二个错误在我看来与这一行相对应:

<h6><%= period.event.name %> in <%= period.event.city %>, <%= period.event.state%></h6>

这是我的事件工厂:

  factory :event do
    name 'First Event'
    street '123 street'
    city 'Chicago'
    state 'Iowa'
    date Date.today
  end

 factory :period do
    name 'First Period'
    description 'This is a description'
    start_time Time.now + 10.days
    end_time Time.now + 10.days + 2.hours
    event
    product
  end

factory :period_registration do
    user
    period
  end

我的事件模型如下所示:

# == Schema Information
#
# Table name: events
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  date       :date
#  street     :string(255)
#  city       :string(255)
#  state      :string(255)
#  created_at :datetime        not null
#  updated_at :datetime        not null
#

class Event < ActiveRecord::Base
  attr_accessible :city, :date, :name, :state, :street
  has_many :periods

  validates :name, presence: true
  validates :street, presence: true
  validates :city, presence: true
  validates :state, presence: true
end

这是我的经期模型:

# == Schema Information
#
# Table name: periods
#
#  id          :integer         not null, primary key
#  name        :string(255)
#  event_id    :integer
#  created_at  :datetime        not null
#  updated_at  :datetime        not null
#  start_time  :time
#  end_time    :time
#  description :text
#  product_id  :integer
#

    class Period < ActiveRecord::Base
      attr_accessible :event_id, :name, :time, :start_time, :end_time, :description, :product_id
      belongs_to :event
      belongs_to :product
      has_many :period_registrations

      validates_time :end_time
      validates_time :start_time
      validates_presence_of :name
      validates_presence_of :start_time
      validates_presence_of :end_time
      validates_presence_of :description
    end

关于什么可能导致这种情况的任何想法?

【问题讨论】:

  • 向我们展示您的时期模型和工厂以及您正在运行的测试。

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 factory-bot testunit


【解决方案1】:

我认为这是因为 FactoryGirl.attributes_for(:period_registration) 返回 {}(空哈希)。您可以在 Rails 控制台中检查它。而且你的代码中有错字:在测试中你发送period_registration: FactoryGirl.attributes_for(:period_registration),但在控制器中你期望params[:registration]。这导致在 db 中创建了空的 PeriodRegistration 模型。此模型不包含 event_id,当您从模型请求事件时,它返回 nil。

为什么不使用 mock 进行此类测试?

【讨论】:

  • 我对测试比较陌生,但仍在试图弄清楚这一切是如何结合在一起的。模拟会是更好的解决方案吗?
  • 我认为对于这类测试,模拟比真实模型更好。顺便说一句,我更喜欢 rspec 而不是 test::unit。
  • 你用过minitest吗?我想我会尝试在我的下一个项目中使用它。
  • @NoahClark 是的。我用过小测试。我认为它很好,我计划在我的下一个项目中使用它。
【解决方案2】:

我认为这是因为您缺少用户模型的工厂

【讨论】:

  • 我有,只是没发。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 2010-11-03
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多