ruby-on-rails RSpec测试失败,未定义方法“validate_unique_of”

bqujaahr  于 2023-05-19  发布在  Ruby
关注(0)|答案(3)|浏览(96)

在Rails 5和RSpec 3.5中,我得到了以下错误。

1) User
     Failure/Error: it { should validate_uniqueness_of(:auth_token)}

     NoMethodError:
       undefined method `validate_uniqueness_of' for #<RSpec::ExampleGroups::User:0x007fab919f8cf
8>
     # ./spec/models/user_spec.rb:14:in `block (2 levels) in <top (required)>'

我已经在谷歌上搜索了正确的语法,但找不到解决方案。有谁知道这里用什么吗?谢谢

vs3odd8k

vs3odd8k1#

你必须将gem添加到Gemfile中:

Gemfile

group :test do
  gem 'shoulda-matchers'
end

并包括它:

spec/rails_helper.rb

RSpec.configure do |config|
  config.include(Shoulda::Matchers::ActiveModel, type: :model)
  config.include(Shoulda::Matchers::ActiveRecord, type: :model)
end
bq3bfh9z

bq3bfh9z2#

我通过添加以下内容使其工作:

Gemfile

group :test do
  gem 'shoulda-matchers'
end

*rails_helper.rb

require 'shoulda/matchers'

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

.rspec

--color
--require spec_helper
--require rails_helper
kdfy810k

kdfy810k3#

将下面的行添加到spec_helper. rb。它将修复上述错误。config. infere_spec_type_from_file_location!

相关问题