【问题标题】:Shoulda_matches error: undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Idea_3::Validations::Title:0x007f3f88fa7548>应该匹配错误:#<RSpec::ExampleGroups::Idea_3::Validations::Title:0x007f3f88fa7548> 的未定义方法 `validate_presence_of'
【发布时间】:2015-02-26 21:00:08
【问题描述】:

我正在学习 TDD 并尝试使用 shoulda_matchers 来帮助我进行测试,但我遇到了一个非常奇怪的错误。这是我的测试:

spec/models/idea_spec.rb

require 'rails_helper'

describe Idea do

  context 'Validations' do
    describe 'title' do
       it { should validate_presence_of(:title) }
    end
  end
end

测试错误说:

Idea Validations title 
 Failure/Error: it { should validate_presence_of(:title) }
 NoMethodError:
   undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Idea_3::Validations::Title:0x007f056f9fcdf8>
 # ./spec/models/idea_spec.rb:7:in `block (4 levels) in <top (required)>'

根据 gem 说明,require 'should/matchers' 位于我的 rails_helper 文件的顶部。

我正在使用的宝石:

group :development, :test do
  gem 'spring'
  gem 'dotenv-rails'
  gem 'rspec-rails', '~> 3.0'
  gem 'factory_girl_rails'
end

group :test do
  gem 'webmock', '~> 1.20.4'
  gem 'shoulda-matchers', require: false
end
  1. 红宝石'2.1.2'
  2. 导轨','4.1.9'

【问题讨论】:

    标签: ruby-on-rails ruby unit-testing rspec shoulda


    【解决方案1】:

    Should-Matchers 不再自动安装到您的测试框架中。您需要将此添加到您的 rails_helper:

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

    请参阅this GitHub issue,阅读note on shoulda-matchers configuration,并阅读此blog post by maintainers

    【讨论】:

      【解决方案2】:

      您的 Gemfile 中的 require: false 表示在您运行测试时没有加载匹配器。在rails_helper.rb 中,您需要在顶部添加行require 'shoulda/matchers'

      【讨论】:

      • 谢谢,我已经添加了。
      • 希望对您有所帮助,是否解决了您的问题?如果可以,请在答案上打勾,谢谢。
      • 对不起,我在发布问题之前已经添加了它,但仍然有问题。谢谢
      【解决方案3】:

      如果平台出于某种原因要求您将 shoulda-matchers 依赖项设为可选,那么您必须在编写的每个测试的顶部添加 require 'shoulda/matchers'

      但是,如果没有授权,则选择从 Gemspec 中删除 require: false

      【讨论】:

      • 谢谢,我已经添加了,因为 require:false 必须在那里。
      【解决方案4】:

      我遇到了同样的问题,并通过以下提示修复了它:

      • 在您的规范中:

        require 'rails_helper'
        
      • /rails_helper

        require 'rspec/rails'
        require 'shoulda/matchers'
        
      • 还在 /rails_helpers 中,不要添加:

        Shoulda::Matchers.configure do |config|
          config.integrate do |with|
            with.test_framework :rspec
            with.library :rails
          end
        end
        
      • /宝石文件

         group :development, :test do
           gem 'shoulda-matchers', '~> 2.5.0', require: false
        
      • 在 spec_helper 中不需要 gem

      我的版本不同(Rails 4.2.3 和 Ruby 2.3.0),但您仍然可以测试并尝试适应

      【讨论】:

        【解决方案5】:

        spec_helper.rb

          config.include(Shoulda::Matchers::ActiveRecord, type: :model)
        

        rails_helper.rb

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

        【讨论】:

          【解决方案6】:

          关注最新更新:http://matchers.shoulda.io/docs/v3.1.1/

          在你的 Gemfile
          group :test do
            gem 'shoulda-matchers', '~> 3.0' 
          end
          
          在 rails_helpers.rb 结尾
          Shoulda::Matchers.configure do |config|
            config.integrate do |with|
              # Choose a test framework:
              #with.test_framework :minitest
              #with.test_framework :minitest_4
              #with.test_framework :test_unit
              with.test_framework :rspec
          
              # Choose one or more libraries:
              with.library :active_record
              with.library :active_model
              with.library :action_controller
              # Or, choose the following (which implies all of the above):
              with.library :rails
            end
          end
          

          【讨论】:

            【解决方案7】:

            之后:

            context 'Validations' do
            

            添加

            before { FactoryGirl.build(:idea) }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-09-24
              • 1970-01-01
              • 1970-01-01
              • 2016-01-18
              • 1970-01-01
              相关资源
              最近更新 更多