1

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

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

我得到的两个错误是:

 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

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

4

2 回答 2

2

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

为什么你不使用模拟进行这些测试?

于 2012-09-01T06:09:03.003 回答
0

我认为这是因为您缺少 User 模型的 Factory

于 2012-09-01T05:31:42.063 回答