主页

运行单元测试的使用报错说 obj.errors.messages rails validation

2023-10-10 04:08PM

参考:https://stackoverflow.com/questions/56237998/how-to-get-validations-error-messages-in-ruby-on-rails

 单元测试文件的 test create 如下:

test "should create car" do
   Enterprise.create(name: '新疆食品新联市场', region: '乌鲁木齐市', enterprise_category: '重点商超', goods_category: '冻猪肉', person_in_charge: '李华', person_in_charge_tel: '1351358466', email: 'lihua@
   MaterialReserf.create(region: '乌鲁木齐市', enterprise_id: 187, enterprise_category: '重点商超', goods_type: '大米', storage_apacity: '200吨', person_in_charge: '李华', tel: '1330008888', warehouse_loc
   # puts Car.all.inspect
   post cars_url, params: { car: { enterprise_id: 1, material_reserf_id: 1,  car_number: "新A325720" , weight: "68吨0", driver_name: "李明0", driver_tel: 183886688450,
                                        driver_id_card_number: 411020155466374560, driver_address: "乌鲁木齐市天山区中山路6号0"} }
   assert_redirected_to "/goods/notice"
end

在运行单元测试的时候报错说:

$ bundle exec rails test test/controllers/cars_controller_test.rb:23
Run options: --seed 17842

# Running:

#<ActiveRecord::Relation [#<Car id: 298486374, enterprise_id: 1, car_number: "新A12573", weight: "20吨", driver_name: "李大渔", driver_tel: "13265425888", driver_id_card_number: "4113226585475145597", driver_address: "乌鲁木齐市天山区中山路8号", created_at: "2023-10-10 02:02:47.633068000 +0000", updated_at: "2023-10-10 02:02:47.633068000 +0000", material_reserf_id: nil>, #<Car id: 980190962, enterprise_id: nil, car_number: "MyString", weight: "MyString", driver_name: "MyString", driver_tel: "MyString", driver_id_card_number: "MyString", driver_address: "MyString", created_at: "2023-10-10 02:02:47.633068000 +0000", updated_at: "2023-10-10 02:02:47.633068000 +0000", material_reserf_id: nil>]>
E

Error:
CarsControllerTest#test_should_create_car:
ActiveRecord::RecordInvalid: translation missing: zh-CN.activerecord.errors.messages.record_invalid
    app/controllers/cars_controller.rb:58:in `create'
    test/controllers/cars_controller_test.rb:30:in `block in <class:CarsControllerTest>'


rails test test/controllers/cars_controller_test.rb:23

Finished in 0.421395s, 2.3731 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

则抛出异常:ActiveRecord::RecordInvalid: translation missing: zh-CN.activerecord.errors.messages.record_invalid

发现是数据无效导致数据没法保存,然后把错误打印出看一下:obj.errors.messages

1. 打开 app/controllers/cars_controller.rb 文件

  @car = Car.new(car_params)
   if @car.save
      case params[:name]
      when '创建并继续新建车辆'
         flash[:success] = '操作成功!'
         redirect_to new_car_path
     else
         flash[:success] = '操作成功!'
         redirect_to notice_goods_path
     end

    else
       render :new

    end
  end

2. 然后修改这个文件

   def create
     @car = Car.new(car_params)

     @car.save
     puts @car.errors.to_hash
     case params[:name]
     when '创建并继续新建车辆'
       flash[:success] = '操作成功!'
       redirect_to new_car_path
     else
       flash[:success] = '操作成功!'
       redirect_to notice_goods_path
     end
   end

修改完之后,在重新运行单元测试:

$ bundle exec rails test test/controllers/cars_controller_test.rb:23
Run options: --seed 23866

# Running:

......{:enterprise=>["translation missing: zh-CN.activerecord.errors.models.car.attributes.enterprise.required"], :material_reserf=>["translation missing: zh-CN.activerecord.errors.models.car.attributes.material_reserf.required"]}
.

Finished in 0.527401s, 13.2726 runs/s, 17.0648 assertions/s.
7 runs, 9 assertions, 0 failures, 0 errors, 0 skips

 就可以了

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论