我试图找出这段代码的错误。即它无法找到方法' stub_model'。试图寻找解决方案,但在任何地方我看起来我的文件似乎是好的。请看一下,也许我不能看到一个简单的错误。非常感谢 :) 书籍模型在db。
中创建我的观点规范(spec / view / books_spec.rb)如下所示:
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
,错误如下:
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)>'
我的rails_helper.rb看起来像:
# 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
和我的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]
答案 0 :(得分:31)
你的rspec-rails版本是什么?
根据Changelog,此Commit mock_model
和stub_model
自版本3.0.0的rspec-rails后删除。
rspec模拟在另一个gem rspec-activemodel-mocks中被外部化。你应该把它包含在Gemfile中并试一试。
希望有所帮助
答案 1 :(得分:0)
这应该是对ahnbizcad的评论,但我缺乏正确执行此操作的声誉。
应通过添加进行修复
require 'rspec/active_model/mocks'
到spec_helper.rb