我试图找出这个代码有什么问题.也就是说找不到方法’stub_model’.试图寻找解决方案,但无处不在,我看我的文件似乎是好的.请看看,也许我只是看不到一个简单的错误.非常感谢 :)
图书模型以db形式创建.
图书模型以db形式创建.
我的view spec(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]
解决方法
你的rspec-rails版本是什么?
根据Changelog和Commit,mock_model和stub_model自rspec-rails的3.0.0版本被删除.
rspec嘲笑在另一个宝石rspec-activemodel-mocks中外化.您应该将其包含在您的Gemfile中并尝试.
希望它有帮助