2014-06-05 4 views
16

Próbuję dowiedzieć się, co jest nie tak z tym kodem. Mianowicie nie może znaleźć metody "stub_model". Próbowałem znaleźć rozwiązanie tego problemu, ale wszędzie, gdzie patrzę, moje pliki wydają się dobre. Proszę, spójrz na to, może po prostu nie widzę prostego błędu. Wielkie dzięki :) Model książki jest tworzony w db.Rspec zobacz niezdefiniowaną metodę stub_model

moim zdaniem Spec (Spec/view/books_spec.rb) wygląda następująco:

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 

a błąd jest następujący:

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)>' 

Moja rails_helper.rb wygląda następująco:

# 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 

i moja 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] 

Odpowiedz

29

Jaka jest twoja wersja rspec-rails?

Zgodnie z Changelog i tymi Commitmock_model i stub_model są usuwane od wersji 3.0.0 z rspec-rails.

makiety rspec są uziemione w innym klejnocie rspec-activemodel-mocks. Powinieneś umieścić go w swoim Gemfile i spróbować.

Nadzieja pomaga

+3

dokumentację rspec-activemodel-mocks mówi, że jeśli przestrzegać instrukcji instalacji dla rspec szyn, jesteś dobry, aby przejść, ale to kłamstwo. musisz wyraźnie uwzględnić ten klejnot. – ahnbizcad

+1

Właśnie podniosłem numer # 1390, aby dodać ostrzeżenie o zwolnieniu z pracy na rspec-rails w celu udzielenia pomocy 2000+ osobom, które przybyły tu w ciągu ostatnich 12 miesięcy, aby nie musiały w przyszłości. Zobaczymy, jak idzie: https://github.com/rspec/rspec-rails/issues/1390 – Evolve