Rails测试失败

时间:2017-03-31 13:18:57

标签: ruby-on-rails

我们正在我的模型special_hour.rb上处理Library Management Tool,我正在检查通过使用start_dateend_date的自定义验证程序输入的日期没有重叠 validate :check_start_date validate :check_end_date def check_start_date #check if start_date overlaps anything currently set check = SpecialHour.where.not(id: id).where("special_id = ?", special_id).where("special_type = ?", special_type).where("start_date <= ?", start_date).where("end_date >= ?", start_date) if check.exists? errors.add(:start_date, "overlaps currently set special hour.") end end def check_end_date #check if end_date overlaps anything currently set check = SpecialHour.where.not(id: id).where("special_id = ?", special_id).where("special_type = ?", special_type).where("start_date <= ?", end_date).where("end_date >= ?", end_date) if check.exists? errors.add(:end_date, "overlaps currently set special hour.") end end

bin/rails test test/models/special_hour_test.rb:9
bin/rails test test/models/special_hour_test.rb:32
bin/rails test test/controllers/admin/special_hours_controller_test.rb:44

在我运行测试之前,一切似乎都能正常工作。如果我只运行validate:check_start_date所有测试都通过但是如果我运行validate:check_end_date我得到3个错误

Failure: SpecialHourTest#test_open_time_should_return_null_for_this_i‌​tem_and_be_allowed_t‌​o_save [/home/libtool/test/models/special_hour_test.rb:35]: the item didn't save there is a problem with the open time saving to the model bin/rails test test/models/special_hour_test.rb:32

两种不同的验证基本相同,只是日期不同。如果您需要任何其他代码,请查看github repo。非常感谢任何帮助。谢谢。

Failure: SpecialHourTest#test_special_hours_are_valid [/home/libtool/test/models/special_hour_test.rb:10]: the special hour is not valid bin/rails test test/models/special_hour_test.rb:9

Failure: Admin::SpecialHoursControllerTest#test_should_update_special‌​_hour [/home/libtool/test/controllers/admin/special_hours_controll‌​er_test.rb:48]: this did not redirect properly bin/rails test test/controllers/admin/special_hours_controller_test.rb:44

result

1 个答案:

答案 0 :(得分:0)

为解决此问题,我的一位同事建议我将验证从模型移到控制器。我已经重新编写了代码并且测试正在通过。