【发布时间】:2016-06-13 16:55:28
【问题描述】:
1) RfidT is not valid without device_mac
Failure/Error: expect(obj2.errors[:device_mac]).to include ("not valid without device_mac!")
expected [] to include "not valid without device_mac!"
# ./spec/models/rfid_t_spec.rb:20:in `block (2 levels) in <top (required)>'
2) RfidT duplicate values of epc are not allowed
Failure/Error: expect(obj3.errors[:epc]).to include("duplicate values of epc are not allowed!")
expected [] to include "duplicate values of epc are not allowed!"
# ./spec/models/rfid_t_spec.rb:27:in `block (2 levels) in <top (required)>'
3) RfidT is not valid without first_seen
Failure/Error: expect(obj4.errors[:first_seen]).to include("cannot be Blank!")
expected ["can't be blank"] to include "cannot be Blank!"
# ./spec/models/rfid_t_spec.rb:33:in `block (2 levels) in <top (required)>'
4) RfidT is not valid without last_seen
Failure/Error: expect(obj5.errors[:last_seen]).to include("cannot be Blank!")
expected ["can't be blank"] to include "cannot be Blank!"
# ./spec/models/rfid_t_spec.rb:39:in `block (2 levels) in <top (required)>'
Finished in 0.16685 seconds (files took 6.45 seconds to load)
6 examples, 4 failures
但是当我在规范文件中使用 to_not 代替 to 时,我没有收到任何错误。请告诉我这是什么意思以及 ruby 和 rails 中 to 和 to_not 之间的区别。
我的规范文件没有产生错误是:
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
【问题讨论】:
-
任何以 ? 结尾的方法由 rSpec 支持。您应该能够更改为包含以阅读“to_include”,它应该可以正常工作。
-
当我将包含更改为 to_include 时,它给了我 NoMethodError。
-
我用to和to_not有什么区别?
-
我很困惑。
expected ["can't be blank"] to include "cannot be Blank!"的哪一部分你不清楚? -
实际上我是 ROR 的初学者,我不明白为什么 expect(obj).to include(msg) 会失败,而 expect (obj) to_not include(msg) 不会?
标签: ruby-on-rails ruby ruby-on-rails-3 validation