【问题标题】:Why i am getting these errors while using rspec to test models in ruby and rails?为什么我在使用 rspec 在 ruby​​ 和 rails 中测试模型时遇到这些错误?
【发布时间】: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


【解决方案1】:

to 检查匹配器(后面的东西)是否匹配,例如

expect(obj).to be_valid

检查obj.valid? 是否真实。如果不是,该示例将被标记为失败。

to_not 则相反:它检查匹配器是否匹配。在上面的示例中,如果obj.valid? 为真,它将将该示例标记为失败。

除非您的代码引发异常,否则to 或to_not 之一将通过,因为它们彼此相反。

您的 rspec 输出告诉您,您的验证没有强制执行您希望它们执行的约束。

【讨论】:

  • 我应该提到这个模型的 .rb 文件吗?
  • 我应该怎么做才能让它们运行?我应该如何通过所有测试?
猜你喜欢
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
相关资源
最近更新 更多