Shoulda_matches错误:未定义的方法`validate_presence_of'用于#<rspec :: examplegroups :: idea_3 :: validations :: title:0x007f3f88fa7548> </rspec :: examplegroups :: idea_3 :: validations :: title:0x007f3f88fa7548>

时间:2015-02-26 21:00:09

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

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

规格/模型/ 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)>'

要求&#39; shoulda / matchers&#39;根据gem指令,它位于我的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. ruby​​&#39; 2.1.2&#39;
  2. rails&#39;,&#39; 4.1.9&#39;

7 个答案:

答案 0 :(得分:12)

Shoulda-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

答案 1 :(得分:5)

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

答案 2 :(得分:2)

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

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

答案 3 :(得分:0)

我遇到了同样的问题并使用以下提示进行修复:

    您的规范中的
  • 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
    
  • / Gemfile中

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

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

按照最新更新后的内容: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

答案 6 :(得分:-2)

后:

context 'Validations' do

添加

before { FactoryGirl.build(:idea) }