这是我的测试:
require 'spec_helper'
describe League do
it 'should default weekly to false' do
league = Factory.create(:league, :weekly => nil)
league.weekly.should == false
end
end
end
这是我的模特:
class League < ActiveRecord::Base
validates :weekly, :inclusion => { :in => [true, false] }
before_create :default_values
protected
def default_values
self.weekly ||= false
end
end
当我运行测试时,收到以下错误消息:
Failure/Error: league = Factory.create(:league, :weekly => nil)
ActiveRecord::RecordInvalid:
Validation failed: Weekly is not included in the list
我尝试了几种不同的方法尝试创建联赛记录并触发回调,但我没有运气。我是否缺少使用RSpec测试回调的东西?
答案 0 :(得分:1)
我相信你说的是,在创建之前,将每周设置为false,然后创建实际上每周设置为nil,覆盖错误。
只做
require 'spec_helper'
describe League do
it 'should default weekly to false' do
league = Factory.create(:league) # <= this line changed
league.weekly.should == false
end
end
end
在你的考试中。无需明确设置nil。