两个测试中的相同问题:事件出现问题



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

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

我得到的两个错误是:

 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

错误编号1与控制器中的此操作相对应:

 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

有什么想法可以引起这种情况吗?

我认为这是因为FactoryGirl.attributes_for(:period_registration)返回{}(空散列)。您可以在rails控制台中查看它。此外,您在代码中也有拼写错误:在测试中,您发送period_registration: FactoryGirl.attributes_for(:period_registration),但在控制器中,您期望params[:registration]。这导致在数据库中创建了空的PeriodRegistration模型。此模型不包含event_id,当您向模型请求事件时,它将返回nil。

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

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

最新更新