rake测试中的Rails NoMethodError

时间:2013-09-12 16:01:32

标签: ruby-on-rails unit-testing activerecord

我有以下课程:

class CachedObject < ActiveRecord::Base
  attr_accessible :key, :value
  validates_presence_of :key
  validates_uniqueness_of :key
  serialize :value
end

我正在进行以下单元测试:

class CachedObjectTest < ActiveSupport::TestCase
  setup do
    @cached_object = CachedObject.new
  end

  test "key should set" do
    @cached_object.key = 'test'
    assert @cached_object.save
  end

  test "value should set" do
    offer = new Offer

    @cached_object.value = offer
    assert @cached_object.save
  end
end

我收到此错误:

NoMethodError: undefined method `new' for #<CachedObjectTest:0x007fb1f1c82f08>

这对我没有意义。 CachedObject是ActiveRecord的子类,因此它应该定义新方法。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

错误在这里

  test "value should set" do
    offer = new Offer

应该是

  test "value should set" do
    offer = Offer.new