2014-06-05 15 views
16

sto cercando di scoprire cosa c'è di sbagliato in questo codice. Vale a dire che non può trovare il metodo 'stub_model'. Ho cercato di cercare una soluzione per questo, ma ovunque guardi i miei file sembrano essere buoni. Per favore, date un'occhiata forse non riesco a vedere un semplice errore. Grazie mille :) Il modello di libro è stato creato in db.Rspec visualizza il metodo non definito stub_model

La mia opinione spec (spec/view/books_spec.rb) si presenta così:

require 'rails_helper' 

describe 'books/new' do 

    it 'displays the book form' do 
    book = stub_model(Book) 
    assign(:book, book) 
    render 
    expect(rendered).to have_selector("form label[for *= 'Title']") 
    expect(rendered).to have_selector("form label[for *= 'Author']") 
    expect(rendered).to have_selector("form label[for *= 'Cover Photo']") 
    expect(rendered).to have_button "Add Book" 
    end 
end 

e l'errore è la seguente:

1) books/new displays the book form 
    Failure/Error: book = stub_model(Book) 
    NoMethodError: 
     undefined method `stub_model' for #<RSpec::ExampleGroups::BooksNew:0x00000103e61870> 
    # ./spec/views/books_spec.rb:6:in `block (2 levels) in <top (required)>' 

mio rails_helper.rb assomiglia:

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require 'spec_helper' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 

# Requires supporting ruby files with custom matchers and macros, etc, in 
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 
# run as spec files by default. This means that files in spec/support that end 
# in _spec.rb will both be required and run as specs, causing the specs to be 
# run twice. It is recommended that you do not name files matching this glob to 
# end with _spec.rb. You can configure this pattern with with the --pattern 
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 

# Checks for pending migrations before tests are run. 
# If you are not using ActiveRecord, you can remove this line. 
ActiveRecord::Migration.maintain_test_schema! 

RSpec.configure do |config| 
    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    # RSpec Rails can automatically mix in different behaviours to your tests 
    # based on their file location, for example enabling you to call `get` and 
    # `post` in specs under `spec/controllers`. 
    # 
    # You can disable this behaviour by removing the line below, and instead 
    # explicitly tag your specs with their type, e.g.: 
    # 
    #  RSpec.describe UsersController, :type => :controller do 
    #  # ... 
    #  end 
    # 
    # The different available types are documented in the features, such as in 
    # https://relishapp.com/rspec/rspec-rails/docs 
    config.infer_spec_type_from_file_location! 
end 

e il mio Gemfile.rb:

source 'https://rubygems.org' 


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '4.1.2.rc1' 
# Use postgresql as the database for Active Record 
gem 'pg' 
# 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 


group :test do 
    gem 'cucumber-rails', :require => false 
    gem 'database_cleaner' 
end 

group :development, :test do 
    gem 'rspec-rails' 
    gem 'factory_girl_rails' 
end 
# Use ActiveModel has_secure_password 
# gem 'bcrypt', '~> 3.1.7' 

# Use unicorn as the app server 
# gem 'unicorn' 

# Use Capistrano for deployment 
# gem 'capistrano-rails', group: :development 

# Use debugger 
# gem 'debugger', group: [:development, :test] 

risposta

29

Qual è la versione di rspec-rails?

Secondo il Changelog e questo Commitmock_model e stub_model vengono rimossi dalla versione 3.0.0 di RSpec-rails.

I mazzi di rspec sono esternalizzati in un'altra gemma rspec-activemodel-mocks. Dovresti includerlo nel tuo Gemfile e provarlo.

Speranza che aiuta

+3

la documentazione per RSpec-activemodel-mock dice se hai seguito le istruzioni di installazione per RSpec-rails, si sta bene ad andare, ma che è una bugia. devi includere esplicitamente quella gemma. – ahnbizcad

+1

Ho appena sollevato il problema n. 1390 per aggiungere un avviso di deprecazione a rspec-rails per assistere le oltre 2000 persone che sono venute qui negli ultimi 12 mesi, per non doverle fare in futuro. Vedremo come va: https://github.com/rspec/rspec-rails/issues/1390 – Evolve

Problemi correlati