Ruby on rails 为什么在使用rspec测试ruby和rails中的模型时会出现这些错误?
Ruby on rails 为什么在使用rspec测试ruby和rails中的模型时会出现这些错误?,ruby-on-rails,ruby,ruby-on-rails-3,validation,Ruby On Rails,Ruby,Ruby On Rails 3,Validation,to检查匹配器(后面的东西)是否匹配,例如 require 'rails_helper' RSpec.describe RfidT, type: :model do it "is valid without epc,device_mac,first_seen,last_seen,count" do obj=RfidT.new(epc:"abc",device_mac:"pqrs",first_seen:10,last_seen:20,count:200)
to
检查匹配器(后面的东西)是否匹配,例如
require 'rails_helper'
RSpec.describe RfidT, type: :model do
it "is valid without epc,device_mac,first_seen,last_seen,count" do
obj=RfidT.new(epc:"abc",device_mac:"pqrs",first_seen:10,last_seen:20,count:200)
expect(obj).to be_valid
end
it "is not valid without epc" do
obj1=RfidT.new(epc:nil)
obj1.valid?
#expect(obj1.errors[:epc]).to include("cannot be Blank!")
expect(obj1).to_not be_valid
end
it"is not valid without device_mac " do
obj2=RfidT.new(device_mac:nil)
obj2.valid?
expect(obj2.errors[:device_mac]).to_not include ("not valid without device_mac!")
end
it" duplicate values of epc are not allowed" do
RfidT.create(epc:"abc",device_mac:"pqrs",first_seen:10,last_seen:20,count:200)
obj3=RfidT.new(epc:"abc",device_mac:"pqrs",first_seen:10,last_seen:20,count:200)
obj3.valid?
expect(obj3.errors[:epc]).to_not include("duplicate values of epc are not allowed!")
end
it "is not valid without first_seen " do
obj4=RfidT.new(first_seen:nil)
obj4.valid?
expect(obj4.errors[:first_seen]).not_to include("cannot be Blank!")
end
it "is not valid without last_seen " do
obj5=RfidT.new(last_seen:nil)
obj5.valid?
expect(obj5.errors[:last_seen]).to_not include("cannot be Blank!")
end
end
检查obj.valid?
是否真实。如果不是,示例将被标记为失败
to_not
则相反:它检查匹配器是否匹配。在上面的示例中,如果obj.valid?
是真的,它会将示例标记为失败
除非您的代码引发异常,否则to
或to_not
中的一个将通过,因为它们是对立的
您的rspec输出告诉您,您的验证没有强制执行您希望它们执行的约束 有没有一种方法以失败告终?由rSpec支持。你应该可以将include改为“to_include”,它应该可以工作。当我将include改为to_include时,它会给我命名错误。我使用to和to_有什么区别?我很困惑。你不清楚
的哪个部分希望[“不能为空”]包含“不能为空!”
的确切内容?实际上,我是ROR的初学者,我不明白为什么expect(obj)。include(msg)表示失败,expect(obj)表示不包含(msg)?我应该提到这个模型的.rb文件吗?我应该如何让它们运行?我应该如何通过所有测试?
require 'rails_helper'
RSpec.describe RfidT, type: :model do
it "is valid without epc,device_mac,first_seen,last_seen,count" do
obj=RfidT.new(epc:"abc",device_mac:"pqrs",first_seen:10,last_seen:20,count:200)
expect(obj).to be_valid
end
it "is not valid without epc" do
obj1=RfidT.new(epc:nil)
obj1.valid?
#expect(obj1.errors[:epc]).to include("cannot be Blank!")
expect(obj1).to_not be_valid
end
it"is not valid without device_mac " do
obj2=RfidT.new(device_mac:nil)
obj2.valid?
expect(obj2.errors[:device_mac]).to_not include ("not valid without device_mac!")
end
it" duplicate values of epc are not allowed" do
RfidT.create(epc:"abc",device_mac:"pqrs",first_seen:10,last_seen:20,count:200)
obj3=RfidT.new(epc:"abc",device_mac:"pqrs",first_seen:10,last_seen:20,count:200)
obj3.valid?
expect(obj3.errors[:epc]).to_not include("duplicate values of epc are not allowed!")
end
it "is not valid without first_seen " do
obj4=RfidT.new(first_seen:nil)
obj4.valid?
expect(obj4.errors[:first_seen]).not_to include("cannot be Blank!")
end
it "is not valid without last_seen " do
obj5=RfidT.new(last_seen:nil)
obj5.valid?
expect(obj5.errors[:last_seen]).to_not include("cannot be Blank!")
end
end
expect(obj).to be_valid