做 model 的时候,在数据库层约束了 null 是 false,然后又在 model 本身加上了 validates presence: true。现在写 test,当尝试为其赋空值时,期望得到这个 record 没有添加成功,不想 test 直接给了 error,说 ActiveRecord::RecordInvalid: Validation failed: Assetnum(就是那个不能为空的字段)can't be blank。 请问这样的测试应该怎么写?谢谢! 贴代码: 先建表,
create_table "hardwares", force: :cascade do |t|
t.string "AssetNum", null: false
#此处省略若干其他字段
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
然后做 model
class Hardware < ActiveRecord::Base
validates :AssetNum, presence: true
end
create 动作是酱紫的
class HardwaresController < ApplicationController
def new
@hardware = Hardware.new
end
def create
@hardware = Hardware.new(hardware_params)
@hardware.save!
redirect_to @hardware
end
private
def hardware_params
params.require(:hardware).permit(:AssetNum, :CPU, :Disk1Size, :Disk1Num,
:Disk2Size, :Disk2Num, :Disk3Size,
:Disk3Num, :Mem1Size, :Mem1Num, :Mem2Size,
:Mem2Num, :BuyingTime, :DeployingTime,
:Position, :ServerType, :ServerModel,
:ServiceNum, :QuickServiceNum )
end
end
程序到这里都 OK,可以正常的创建,于是我做了一个 test 来检测提交空值的 AssetNum 会不会创建这个记录,如下
class CreateHardwareTest < ActionDispatch::IntegrationTest
def setup
@h1 = hardwares(:h1)
end
test "create hardware with invalid information" do
assert_no_difference 'Hardware.count' do
post hardwares_path, hardware: {
AssetNum: "" ,
CPU: @h1.CPU ,
Disk1Size: @h1.Disk1Size ,
Disk1Num: @h1.Disk1Num ,
Mem1Size: @h1.Mem1Size ,
Mem1Num: @h1.Mem1Num ,
BuyingTime: @h1.BuyingTime ,
DeployingTime: @h1.DeployingTime ,
Position: @h1.Position ,
ServerType: @h1.ServerType ,
ServerModel: @h1.ServerModel ,
ServiceNum: @h1.ServiceNum ,
QuickServiceNum: @h1.QuickServiceNum
}
end
end
end
理论上来说 OK 的,但是跑测试的结果却是:
ERROR["test_create_hardware_with_invalid_information", CreateHardwareTest, 2015-07-29 08:39:36 +0800]
test_create_hardware_with_invalid_information#CreateHardwareTest (1438130376.94s)
ActiveRecord::RecordInvalid: ActiveRecord::RecordInvalid: Validation failed: Assetnum can't be blank
app/controllers/hardwares_controller.rb:13:in `create'
test/integration/create_hardware_test.rb:39:in `block (2 levels) in <class:CreateHardwareTest>'
test/integration/create_hardware_test.rb:38:in `block in <class:CreateHardwareTest>'
2/2: [=============================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.07189s
求怎么破?谢谢!