2017-03-31 118 views
0

我们在Library Management Tool在我的模型special_hour.rb工作,我检查到有正在使用自定义的验证两个start_dateend_date进入的日期没有重叠。Rails的测试失败

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 

一切似乎正常工作,直到我运行我的测试。如果我只运行验证:check_start_date通过了所有测试,但如果我运行验证:check_end_date我得到的3个错误

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 

两个不同的验证是除了日期,如果不同的基本一致。如果您需要任何其他代码,请查看github回购。任何帮助非常感谢你。

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

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

+0

什么是错误信息? –

+0

失败: SpecialHourTest#test_open_time_should_return_null_for_this_item_and_be_allowed_to_save [/home/libtool/test/models/special_hour_test.rb:35]: 该项目没有保存有与保存到模型 仓开放时间的问题/轨试验测试/车型/ special_hour_test.rb:32 –

+0

失败: SpecialHourTest#test_special_hours_are_valid [/home/libtool/test/models/special_hour_test.rb:10]: 特殊小时无效 斌/轨试验测试/模型/ special_hour_test.rb :9 –

回答

0

要解决此问题,我的一个同事建议我搬到我的验证,从模型到控制器。我重写了我的代码,测试通过了。