我有我试图在我的模型中测试的方法,但它们运行不正常,它似乎没有在应该返回 false 的时候返回 - 有什么建议吗?
class Registration < ActiveRecord::Base
validate :check_duplicate_section
def check_duplicate_section
all_registrations = Registration.all
all_registrations.each do |reg|
puts reg.section_id
if reg.section_id == self.section_id && reg.student_id == self.student_id
errors.add(:registration, "Already exists")
return false
end
return true
end
end
测试文件:(@bruce 在前面定义)
class RegistrationTest < ActiveSupport::TestCase
should "not allow invalid student registrations" do
@mike = FactoryGirl.create(:student, :first_name => "Mike")
good_reg = FactoryGirl.build(:registration, :section => @section2, :student => @mike)
bad_reg = FactoryGirl.build(:registration, :section => @section1, :student => @bruce)
bad_reg2 = FactoryGirl.build(:registration, :section => @section2, :student => @mike)
assert_equal true, good_reg.valid?
assert_equal false, bad_reg.valid?
assert_equal false, bad_reg2.valid?