Rspec NoMethodError: undefined method `errors_on'

Ich bin Tests für die Validierung der Eingabe einer Stadt-Attribut in einem Adressen-Modell. Mein Modell-Spezifikation ist wie folgt

require 'rails_helper'

RSpec.describe Address, :type => :model do
  before(:each) do
    @valid_attributes = {
      street: 'rock avenue',
      city: 'MSA',
      zip: '00100',
      person_id: 1
    } # runs before each it block
  end

  context "Validations" do
    it 'must have a city' do
      a = Address.new
      expect(a.errors_on(:city)).not_to be_empty
    end
  end
end

Wenn ich die spec, bekomme ich die folgende Fehlermeldung

  1) Address Validations must have a city
     Failure/Error: expect(a.errors_on(:city)).not_to be_empty
     NoMethodError:
       undefined method `errors_on' for #<Address:0xd844538>
     # ./spec/models/address_spec.rb:19:in `block (3 levels) in <top (required)>'

Meinem test-Edelsteine werden eingerichtet wie folgt in meinem Gemfile

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails', :require => false
end

group :test do
  gem 'capybara'
  gem 'guard'
  gem 'guard-rspec'
  gem 'libnotify'
  gem 'rspec-collection_matchers'
end

Ich habe die rspec-Sammlung Matcher in meinem spec_helper, so verstehe ich nicht, warum ich immer diese Fehlermeldung

Meine spec_helper.rb

require 'capybara/rspec' 
require 'factory_girl_rails'
require 'rspec/collection_matchers'

Kann ich jedoch sehen, dass die Methode, errors_on definiert wurde, in rspec-collection_matchers Edelstein-wie dargestellt hier

Meine .rspec

--color
--require spec_helper

Habe ich auch geändert die position der

require 'rspec/collection_matchers'

zu rails_helper.rb, aber das ausführen der test-suite wieder den gleichen Fehler

rails_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require 'rspec/collection_matchers'
require 'capybara/rspec'
require 'factory_girl_rails'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.include Capybara::DSL
  config.include FactoryGirl::Syntax::Methods
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
end

Meine spec_helper.rb ist leer, nur der block unten:

RSpec.configure do |config|
end

Ich bin mit Rails 4.1.0, rspec 3.0.0-und rspec-collection_matchers 1.0.0

  • Deine Skillung erfordert rails_helper, aber Sie setzen die require 'rspec/collection_matchers' Linie in spec_helper - Ist Ihren rails_helper - Datei require 'spec_helper'?
  • ja, meine rails_helper.rb Datei erfordert spec_helper
InformationsquelleAutor Mutuma | 2014-08-04
Schreibe einen Kommentar