登录 主页

单元测试示例

2023-10-13 10:04AM

test/fixtures/regions.yml 文件:

one:
  2   name: "乌鲁木齐市"

 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

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' 

测试文件:test/controllers/warns_controller_test.rb 

require "test_helper"
 
class WarnsControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers
    
  setup do
     @manager = managers(:manager3)
     sign_in @manager
     @warn = warns(:one)
  end
   
  test "should get index" do
     get warns_url
     assert_response :success
  end
  
  test "should get index with paramater" do
     get warns_url, params: {goods_category: "面粉"}
     assert_equal 1, assigns(:warns).size
     assert_response :success
  end
  
  test "should get new" do
    get new_warn_url
    assert_response :success
  end
  
  test "should create warns" do
     assert_difference("Warn.count", 1) do
       post warns_url, params: { warn: { region_id: regions(:one).id, enterprise_id: enterprises(:one).id, material_reserf_id: material_reserves(:one).id, enterprise_category: '重点商超', goods_categor
    y: '牛肉', material_reserf_number: '0005', unit: '1500吨', max_warn: '2500吨', min_warn: '501吨', warn_result: '不足' } }
     end
     assert_equal "501吨", Warn.last.min_warn
     assert_redirected_to warn_url(Warn.last)
  end
   
  test "should show warns" do
     get warns_url(@warn)
     assert_response :success
  end
 
  test "should get edit" do
     get edit_warn_url(@warn)
     assert_response :success
   end
  test "should update warns" do
    patch warn_url(@warn), params: { warn: { region_id: regions(:one).id, enterprise_id: enterprises(:one).id, material_reserf_id: material_reserves(:one).id, enterprise_category: '生产批发市场', good    s_category: '羊肉', material_reserf_number: '0006', unit: '1500吨', max_warn: '2500吨', min_warn: '500吨', warn_result: '充足' } }
    assert_equal "生产批发市场", assigns(:warn).enterprise_category
   end
 
  test "should destroy warns" do
     assert_difference("Warn.count", -1) do
       delete warn_url(@warn)
     end
      assert_redirected_to warns_url
    end
  end

 运行测试文件:

$ bundle exec rails test test/controllers/warns_controller_test.rb 
Run options: --seed 34106

# Running:

........

Finished in 0.913969s, 8.7530 runs/s, 13.1295 assertions/s.
8 runs, 12 assertions, 0 failures, 0 errors, 0 skips

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论