【问题标题】:Rspec 'it { should have_db_column(:x).of_type(:y) }' not workingRspec 'it { should have_db_column(:x).of_type(:y) }' 不工作
【发布时间】:2019-07-17 05:40:24
【问题描述】:

我的attendance_rspec.rb 文件包含:

require "rails_helper"

RSpec.describe Attendance, type: :model do
    it { should have_db_column(:date).of_type(:date) }
end

当我使用rspec spec/models/attendance_spec.rb从终端运行它时

显示错误:

Failures:

  1) Attendance 
     Failure/Error: it { should have_db_column(:date).of_type(:date) }

     NoMethodError:
       undefined method `of_type' for #<RSpec::Matchers::BuiltIn::Has:0x000055e5c65dd3e0>
     # ./spec/models/attendance_spec.rb:4:in `block (2 levels) in <main>'

我已经配置了 FactoryBot 以及 database_cleanercapybara gems。我在rails_helper.rb 中缺少什么宝石或缺少任何配置?

我关注 this link 设置 Rspec

【问题讨论】:

标签: ruby-on-rails rspec rubygems


【解决方案1】:

正如NickM 指出的那样,have_db_column matchershoulda-matchers gem 提供。将其添加到您的 Gemfile 和 follow the integration steps 以使用 have_db_column

【讨论】:

  • 我认为他确实拥有宝石,因为他在 .of_type 上而不是在 shouldhave_db_column 上收到错误。我认为它与支持 .of_type 的版本不同
  • 安装 gem 并使用 teamtreehouse.com/community/nomethoderror-undefined-methodrails_helper.rb 中配置它。现在完美运行!
  • @NickM 我认为(由于回溯中的RSpec::Matchers::BuiltIn::Has)RSpec 的私有API 中的一个类被实例化,它没有of_type 方法。 shoulda_matchers 提供了一种更具体的方法来覆盖此行为。
  • @abdulwasey20 如果它解决了您的问题,请考虑将答案标记为“已接受”。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 2013-04-05
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多