2023-10-11 10:05AM
打开 test/controllers/cars_controller_test.rb 文件
test "should create car" do
Enterprise.create(name: '新疆食品新联市场', region: '乌鲁木齐市', enterprise_category: '重点商超', goods_category: '冻猪肉', person_in_charge: '李华', person_in_charge_tel: '1351358466', email: 'lihua@email.com', status: '待处理', manager_id: 99, monitor_status: true)
MaterialReserf.create(region: '乌鲁木齐市', enterprise_id: 187, enterprise_category: '重点商超', goods_type: '大米', storage_apacity: '200吨', person_in_charge: '李华', tel: '1330008888', warehouse_location: '西村冷库', city: '乌鲁木齐', max_store: '2000吨', longitude: '87.617733', latitude: '43.792818', sorting_and_distribution: true, warehouse_location_two: '东村冷库', latitude_two: '43.792817', longitude_two: '87.617732', sorting_and_distribution_type: 'WLMQ0001')
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
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
错误消息中显示的内容是关于翻译缺失的错误消息,指示缺少了一些模型属性的翻译。具体来说,它提到了Enterprise
模型的car
属性和MaterialReserf
模型的car
属性的翻译缺失。
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
1. 先增加 test/fixtures/enterprises.yml 文件
one:
name: "新疆食品新联市场"
region: "乌鲁木齐市"
enterprise_category: "重点商超"
goods_category: "冻猪肉"
person_in_charge: "李华"
person_in_charge_tel: "1351358466"
email: "lihua@example.com"
status: "待处理"
manager_id: 99
monitor_status: true
2. 增加 test/fixtures/material_reserves.yml 文件
one:
region: '乌鲁木齐市'
enterprise_id: 187
enterprise_category: '重点商超'
goods_type: '大米'
storage_apacity: '200吨'
person_in_charge: '李华'
tel: '1330008888'
warehouse_location: '西村冷库'
city: '乌鲁木齐'
max_store: '2000吨'
longitude: '87.617733'
latitude: '43.792818'
sorting_and_distribution: true
warehouse_location_two: '东村冷库'
latitude_two: '43.792817'
longitude_two: '87.617732'
sorting_and_distribution_type: 'WLMQ0001'
3. 修改 test/controllers/cars_controller_test.rb 文件
test "should create car" do
post cars_url, params: { car: { enterprise_id: enterprises(:one).id, material_reserf_id: material_reserves(:one).id, car_number: "新A325720" , weight: "68吨0", driver_name: "李明0", driver_tel: 18388668 8450,
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
Run options: --seed 64991
# Running:
..{}
.....
Finished in 0.485842s, 14.4080 runs/s, 18.5245 assertions/s.
7 runs, 9 assertions, 0 failures, 0 errors, 0 skips
发现有一个多余的 {}
4. 修改 app/controllers/cars_controller.rb 文件
def create
@car = Car.new(car_params)
@car.save!
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
Run options: --seed 47314
# Running:
.......
Finished in 0.481660s, 14.5331 runs/s, 18.6854 assertions/s.
7 runs, 9 assertions, 0 failures, 0 errors, 0 skips
就可以了
登录
请登录后再发表评论。
评论列表:
目前还没有人发表评论