Rspec 视图未定义的方法 stub_model [英] Rspec view undefined method stub_model

查看:36
本文介绍了Rspec 视图未定义的方法 stub_model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出这段代码有什么问题.即它找不到方法'stub_model'.试图为此寻找解决方案,但我到处寻找我的文件似乎很好.请看一看,也许我只是看不出一个简单的错误.非常感谢 :)Book 模型在 db 中创建.

我的视图规范 (spec/view/books_spec.rb) 如下所示:

需要'rails_helper'描述书籍/新书"做它显示书籍形式"做书 = stub_model(书)分配(:书,书)使成为expect(rendered).to have_selector("form label[for *= 'Title']")expect(rendered).to have_selector("表单标签[for *= 'Author']")expect(rendered).to have_selector("form label[for *= 'Cover Photo']")期望(渲染).to have_button添加书籍"结尾结尾

错误如下:

1) books/new 显示书籍形式失败/错误:book = stub_model(Book)无方法错误:#<RSpec::ExampleGroups::BooksNew:0x00000103e61870>的未定义方法`stub_model'# ./spec/views/books_spec.rb:6:in `block (2 levels) in <top (required)>'

我的 rails_helper.rb 看起来像:

# 当你运行 'rails generate rspec:install' 时,这个文件被复制到 spec/ENV["RAILS_ENV"] ||= '测试'需要'spec_helper'需要 File.expand_path("../../config/environment", __FILE__)需要'rspec/rails'# 需要支持带有自定义匹配器和宏等的 ruby​​ 文件,在# spec/support/及其子目录.与`spec/**/*_spec.rb` 匹配的文件是# 默认情况下作为规范文件运行.这意味着 spec/support 中的文件结束# 在 _spec.rb 中将需要并作为规范运行,导致规范# 运行两次.建议您不要将与此 glob 匹配的文件命名为# 以_spec.rb 结尾.您可以使用 --pattern 配置此模式# 命令行或 ~/.rspec、.rspec 或 `.rspec-local` 中的选项.目录[Rails.root.join("spec/support/**/*.rb")].each { |f|需要 f }# 在运行测试之前检查挂起的迁移.# 如果你没有使用 ActiveRecord,你可以去掉这一行.ActiveRecord::Migration.maintain_test_schema!RSpec.configure 做 |config|# 如果您不使用 ActiveRecord 或 ActiveRecord 固定装置,请删除此行config.fixture_path = "#{::Rails.root}/spec/fixtures"# 如果你没有使用 ActiveRecord,或者你不想运行你的每一个# 交易中的示例,删除以下行或分配 false# 而不是真的.config.use_transactional_fixtures = true# RSpec Rails 可以自动将不同的行为混合到您的测试中# 基于它们的文件位置,例如使您能够调用 `get` 和# `post` 在 `spec/controllers` 下的规范中.## 您可以通过删除下面的行来禁用此行为,而是# 用它们的类型明确标记你的规格,例如:## RSpec.describe UsersController, :type =>:控制器做# # ...#     结尾## 不同的可用类型记录在特性中,例如# https://relishapp.com/rspec/rspec-rails/docsconfig.infer_spec_type_from_file_location!结尾

和我的 Gemfile.rb:

source 'https://rubygems.org'# Bundle edge Rails 代替: gem 'rails', github: 'rails/rails'宝石导轨",4.1.2.rc1"# 使用 postgresql 作为 Active Record 的数据库宝石'pg'# 对样式表使用 SCSSgem 'sass-rails', '~>4.0.3'# 使用 Uglifier 作为 JavaScript 资源的压缩器gem 'uglifier', '>= 1.3.0'# 将 CoffeeScript 用于 .js.coffee 资产和视图gem '咖啡轨','~>4.0.0'# 有关更多支持的运行时,请参阅 https://github.com/sstephenson/execjs#readme# gem 'therubyracer', 平台: :ruby# 使用 jquery 作为 JavaScript 库宝石'jquery-rails'# Turbolinks 使您的 Web 应用程序中的链接速度更快.阅读更多:https://github.com/rails/turbolinks宝石涡轮链接"# 轻松构建 JSON API.阅读更多:https://github.com/rails/jbuildergem 'jbuilder', '~>2.0'# bundle exec rake doc:rails 在 doc/api 下生成 API.gem 'sdoc', '~>0.4.0', 组: :doc# Spring 通过让您的应用程序在后台运行来加快开发速度.阅读更多:https://github.com/rails/springgem 'spring',组::开发组:测试做gem 'cucumber-rails', :require =>错误的宝石'database_cleaner'结尾组:开发,:测试做gem 'rspec-rails'宝石 'factory_girl_rails'结尾# 使用 ActiveModel has_secure_password# gem 'bcrypt', '~>3.1.7'# 使用独角兽作为应用服务器# gem '独角兽'# 使用 Capistrano 进行部署# gem 'capistrano-rails', group::development# 使用调试器# gem 'debugger', group: [:development, :test]

解决方案

你的 rspec-rails 版本是什么?

根据变更日志和这个Commit mock_modelstub_model 自 3.0.0 版

rspec mocks 在另一个 gem rspec-activemodel-mocks 中外化.您应该将其包含在您的 Gemfile 中并尝试使用.

希望能帮到你

i'm trying to find out what is wrong with this code. Namely it cannot find method 'stub_model'. Tried to look for solution for this but everywhere i look my files seems to be good. Please take a look on it maybe i just can't see a simple mistake. Thanks a lot :) Book model is created in db.

My view spec (spec/view/books_spec.rb) looks like this:

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

and the error is following:

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

My rails_helper.rb looks like:

# 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

and my 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]

解决方案

What is your rspec-rails version?

According to the Changelog and this Commit mock_model and stub_model are removed since version 3.0.0 of rspec-rails.

rspec mocks are externalized in an another gem rspec-activemodel-mocks . You should include it in your Gemfile and try it.

Hope it helps

这篇关于Rspec 视图未定义的方法 stub_model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆