2014-06-16 20 views
6

Ben ben RSpec ile test etmek istiyorum modeli aşağıdaki:RSpec tanımsız yöntemi "errors_on"

class Language < ActiveRecord::Base 
    has_and_belongs_to_many :dvds 
    validates :title, presence: true, uniqueness: { case_sensitive: false } 
end 

language_spec.rb

describe Language do 
    describe 'title validation' do 
    context 'title is present' do 
     before(:each) do 
     @lang = Language.new(title: 'English') 
     end 

     it 'is valid with present title' do 
     expect(@lang).to have_exactly(0).errors_on(:title) 
     end 
    end 

    context 'title is not present' do 
     before(:each) do 
     @lang = Language.create 
     end 

     it 'has an error on title attribute' do 
     expect(@lang).to have_exactly(1).errors_on(:title) 
     end 

    end 
    end 
end 

Ne yazık ki alıyorum testi arızaları:

Failures:

1) Language title validation title is present is valid with present title Failure/Error: expect(@lang).to have_exactly(0).errors_on(:title) NoMethodError: undefined method errors_on' for #<Language:0xaa40e98> # ./spec/models/language_spec.rb:9:in block (4 levels) in '

2) Language title validation title is not present has an error on title attribute Failure/Error: expect(@lang).to have_exactly(1).errors_on(:title) NoMethodError: undefined method errors_on' for #<Language id: nil, title: nil, created_at: nil, updated_at: nil> # ./spec/models/language_spec.rb:19:in block (4 levels) in '

Gemfile

source 'https://rubygems.org' 

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '4.1.1' 
# Use sqlite3 as the database for Active Record 
gem 'sqlite3' 
# Use SCSS for stylesheets 
gem 'sass-rails', '~> 4.0.3' 
# Use Uglifier as compressor for JavaScript assets 
gem 'uglifier', '>= 1.3.0' 
# Use CoffeeScript for .js.coffee assets and views 
gem 'coffee-rails', '~> 4.0.0' 
# See https://github.com/sstephenson/execjs#readme for more supported runtimes 
# gem 'therubyracer', platforms: :ruby 

# Use jquery as the JavaScript library 
gem 'jquery-rails' 
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks 
gem 'turbolinks' 
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 
gem 'jbuilder', '~> 2.0' 
# bundle exec rake doc:rails generates the API under doc/api. 
gem 'sdoc', '~> 0.4.0',   group: :doc 

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 
gem 'spring',  group: :development 

# Pagination plugin 
gem 'will_paginate', '~> 3.0' 

# Testing 
group :development, :test do 
    gem "rspec-rails", "~> 3.0.1" 
    gem "factory_girl_rails", "~> 4.4.1" 
end 

group :test do 
    gem "faker", "~> 1.3.0" 
    gem "capybara", "~> 2.3.0" 
    gem "database_cleaner", "~> 1.3.0" 
    gem "launchy", "~> 2.4.2" 
    gem "selenium-webdriver", "~> 2.42.0" 
end 

Neden undefined method "errors_on" alıyorum?

describe Language, type: :model do 

Veya kullanmak infer_spec_type_from_file_location!errors_on eşleştirici the rspec-collection_matchers gem taşındı

RSpec.configure do |config| 
    config.infer_spec_type_from_file_location! 
end 
+1

Belki [bu] (https: // github.com/rspec/rspec-rails/issues/948#issuecomment-44097307) yardımcı olacaktır. – zishe

+0

Evet, tam olarak. Ancak hata mesajım biraz farklıydı, bu yüzden başka bir şey olabileceğini düşündüm. Benim durumumda Gemfile'a eklemek zorundayım: 'gem' rspec-collection_matchers ' – Bounce

cevap

-1

tipi seçeneği ayarlamak için deneyin.

uzağa çekirdek olmayan sözdizimi hareket etmek istese, yapabileceği

describe Language do 
    describe 'title validation' do 
    context 'title is present' do 
     before(:each) do 
     @lang = Language.new(title: 'English') 
     end 

     it 'is valid with present title' do 
     expect(@lang.errors[:title]).to be_empty 
     end 
    end 

    context 'title is not present' do 
     before(:each) do 
     @lang = Language.create 
     end 

     it 'has an error on title attribute' do 
     expect(@lang.errors.added?(:title, :blank)).to be_true 
     end 
    end 

    end 
end 

(have_exactly and related matchers have also moved to rspec-collection_matchers.) RSpec 3'te böyle bir eşleştirici yoktur

4

(here bakınız) böylece Gemfile hatayı düzeltmek istiyorum için ifade ederek,: