完全被这个困扰了。我发誓,这看起来应该有效,但它没有。测试数据库已更新(rake db:migrate - rake:db:test:clone)。我希望我做的事情很傻。
完全披露,我正在运行3.1 RC4。
模型
class ExerciseSet < ActiveRecord::Base
TYPES = %w[time rep]
validates :type,
:presence => true,
:inclusion => { :in => TYPES }
end
规范文件
require 'spec_helper'
describe ExerciseSet do
before(:each) do
@attr = { :value => 12,
:value_max => 15,
:type => "timed",
:target => "range"
}
end
describe "type" do
it "must be either 'time' or 'rep'" do
values = %w[time rep]
values.each do |value|
exercise_instance = ExerciseSet.new(@attr.merge(:type => value))
exercise_instance.should be_valid
end
end
end
end
输出
Failures:
1) ExerciseSet type must be either 'time' or 'rep'
Failure/Error: exercise_instance.should be_valid
expected valid? to return true, got false
# ./spec/models/exercise_set_spec.rb:19:in `block (4 levels) in <top (required)>'
# ./spec/models/exercise_set_spec.rb:17:in `each'
# ./spec/models/exercise_set_spec.rb:17:in `block (3 levels) in <top (required)>'
答案 0 :(得分:0)
首先要做的事情 - 为什么实例不是有效的?我将在规范中添加一些调试:
describe "type" do
it "must be either 'time' or 'rep'" do
values = %w[time rep]
values.each do |value|
exercise_instance = ExerciseSet.new(@attr.merge(:type => value))
exercise_instance.valid? # force errors
puts exercise_instance.errors.full_messages.join("\n")
exercise_instance.should be_valid
end
end
end
是的,这应该是一个评论,但是我无法轻易地提供规范重写。