ruby-on-rails – 控制器规范的未定义方法“包含”

ruby-on-rails – 控制器规范的未定义方法“包含”,第1张

概述绝对是最好的划分规格,所以你有关于MVC架构的每个方面的规格,但我认为与控制器规格和视图规格有轻微的交叉. 对于视图规范,你应该只关注视图,但是对于控制器规范,我仍然认为测试正确的视图是否是一个好主意,甚至可能测试视图的内容,尽管更深入内容的测试应该在视图规范中进行. 尽管有一篇明确的文章,https://www.relishapp.com/rspec/rspec-rails/v/2-1/docs 绝对是最好的划分规格,所以你有关于MVC架构的每个方面的规格,但我认为与控制器规格和视图规格有轻微的交叉.

对于视图规范,你应该只关注视图,但是对于控制器规范,我仍然认为测试正确的视图是否是一个好主意,甚至可能测试视图的内容,尽管更深入内容的测试应该在视图规范中进行.

尽管有一篇明确的文章,https://www.relishapp.com/rspec/rspec-rails/v/2-1/docs/controller-specs/render-views,描述了如何做到这一点,但我无法集成我的视图和控制器规范.

我一直得到错误未定义方法’包含’!

这是我的spec_helper:

# This file is copIEd to spec/ when you run 'rails generate rspec:install'ENV["RAILS_ENV"] ||= 'test'require file.expand_path("../../config/environment",__file__)require 'rspec/rails'require 'capybara/rspec'require 'capybara/rails'require 'factory_girl_rails'require 'ap'def set(factory)  @user = FactoryGirl.create(factory) enddef sign_up(first_name,last_name,profile_name,email,password)    visit "/"    click_link "Register"    fill_in('First name',with: first_name)    fill_in('Last name',with: last_name)                fill_in('Profile name',with: profile_name)    fill_in('Email',with: email)    fill_in('Password',with: password)    fill_in('Password confirmation',with: password)    click_button 'Sign up'enddef sign_in(email,password)    visit "/"    click_link "Sign In"    fill_in('Email',with: password)    click_button 'Sign in'enddef sign_out    visit "/"    click_link "Sign Out"end#Webrat.configure do |config|#  config.mode = :rails#end#webratrequire 'capybara/poltergeist'# Capybara.JavaScript_driver = :poltergeistCapybara.JavaScript_driver = :seleniumDir[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.check_pending! if defined?(ActiveRecord::Migration)RSpec.configure do |config|  # true means 'yes,filter these specs'  config.filter_run_excluding stress: true # config.current_driver = :webkit#  config.use_transactional_fixtures = false#  config.include Capybara::DSL  DatabaseCleaner.strategy = :truncation  config.after(:suite) do    DatabaseCleaner.clean_with(:truncation)  end#  config.before(:suite) do  #  DatabaseCleaner.strategy = :transaction #   DatabaseCleaner.clean_with(:truncation)#    DatabaseCleaner.start # end # config.after(:each) do #   DatabaseCleaner.clean # end  #config.after(:suite) do#   DatabaseCleaner.strategy = :transaction#   DatabaseCleaner.clean_with(:truncation)  #  DatabaseCleaner.clean # end  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures  # config.fixture_path = "#{::Rails.root}/spec/fixtures"  # config.include RSpec::Rails::RequestExampleGroup,type: :feature  # 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  I18n.enforce_available_locales = true  # If true,the base class of anonymous controllers will be inferred  # automatically. This will be the default behavior in future versions of  # rspec-rails.  config.infer_base_class_for_anonymous_controllers = false  # Run specs in random order to surface order dependencIEs. If you find an  # order dependency and want to deBUG it,you can fix the order by provIDing  # the seed,which is printed after each run.  #     --seed 1234  config.order = "random"end

这是我的控制器规格:

require "spec_helper"describe UserFrIEndshipsController,type: :controller do    render_vIEws    let (:user_1) { FactoryGirl.create(:user_1)}     before {         sign_in user_1        get :index    }       it "renders the :index vIEw" do        response.should render_template(:index)    end    it "vIEw contains expected HTML" do        # a sanity test more than anything        response.should contain("Welcome to the home page")    endend

运行此规范后,我得到了这个:

.FFailures:  1) UserFrIEndshipsController vIEw contains expected HTML     Failure/Error: response.should contain("Listing Widgets")     NoMethodError:       undefined method `contain' for #<RSpec::Core::ExampleGroup::nested_1:0x00000008632268>     # ./spec/controllers/user_frIEndships_spec.rb:18:in `block (2 levels) in <top (required)>'Finished in 0.1835 seconds2 examples,1 failure

为什么会这样?我怎样才能让它发挥作用?

解决方法 如果你看一下 relish documentation for the current 2.14 version of Rspec,你会发现他们现在正在使用匹配:

expect(response.body).to match /Listing Widgets/m

使用should语法,这应该工作:

response.body.should match(/Welcome to the home page/)
总结

以上是内存溢出为你收集整理的ruby-on-rails – 控制器规范的未定义方法“包含”全部内容,希望文章能够帮你解决ruby-on-rails – 控制器规范的未定义方法“包含”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1281928.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存