我收到以下错误,无法弄清楚如何修复它。 Stack Overflow上有一些其他页面可以解决类似的问题,但这些似乎并不适用,或者至少我不够精明,无法解决这个问题。
当我运行authentication_pages_spec时,问题似乎是使用“patch”,导致下面的失败。请注意,我已经尝试用“put”替换“patch”并获得相同的结果。
Failures:
1) Authentication authorization for non-signed-in users in the Users controller submitting to the update action
Failure/Error: before { patch user_path(user) }
NoMethodError:
undefined method `patch' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3::Nested_1::Nested_1::Nested_2:0x007fa7e4df1e50>
# ./spec/features/authentication_pages_spec.rb:59:in `block (6 levels) in <top (required)>'
Finished in 0.34392 seconds
2 examples, 1 failure
注意:基于对Stack Overflow here和blog it references here的讨论,我选择将规范/请求文件夹更改为规范/功能,因为这似乎是Capybara在2.0版之后所要求的。
这是我的Gemfile: 来源'https://rubygems.org' 红宝石'2.0.0' #红宝石宝石= railstutorial_rails_4_0
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'pg', '0.15.1'
gem 'bcrypt-ruby', '3.0.1'
group :development, :test do
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', github: 'sporkrb/spork-rails'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.9'
end
group :test do
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
gem 'growl', '1.0.3'
gem 'factory_girl_rails', '4.2.1'
gem 'cucumber-rails', '1.3.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'
end
gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '2.2.1'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
我修改了我的Guardfile以使用spec / features文件夹而不是spec / requests文件夹:
require 'active_support/inflector'
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' },
:rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end
guard 'rspec', all_after_pass: false, cli: '--drb' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
# Custom Rails Tutorial specs
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m|
["spec/routing/#{m[1]}_routing_spec.rb",
"spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
"spec/acceptance/#{m[1]}_spec.rb",
(m[1][/_pages/] ? "spec/features/#{m[1]}_spec.rb" :
"spec/features/#{m[1].singularize}_pages_spec.rb")]
end
watch(%r{^app/controllers/sessions_controller\.rb$}) do |m|
"spec/features/authentication_pages_spec.rb"
end
end
用户控制器在这里:
class UsersController < ApplicationController
attr_accessor :name, :email
before_action :signed_in_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
flash[:error] = "Oops!"
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name,
:email, :password, :password_confirmation)
end
# Before filters
def signed_in_user
redirect_to signin_url, notice: "Please sign in." unless signed_in?
end
end
这里的Spec帮助文件:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Capybara::DSL
# Added below myself, troubleshooting an 'undefined method 'patch'' message
config.include Rails.application.routes.url_helpers
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
最后,为了更好地衡量,config / routes.rb文件位于:
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
有关Stack Overflow here的类似讨论,但该帖子的答案是将测试从./spec/requests移至./spec/features-我已经完成了。
另一个相关讨论here,答案是在功能文件的顶部使用require 'spec_helper'
...我已经拥有。
最后一个观察:看起来关于这个主题的大部分讨论表明问题是Capybara需要位于spec / features文件夹中的测试才能访问URL帮助程序 - 但我的authentication_pages_spec.rb文件似乎“访问”没有问题。
欣赏任何见解! Rails 4.0的Rails教程处于测试阶段,一直试图弄清楚这是不是一个bug。
*更新*
在实施彼得的建议后,一切都过去了。以下是authentication_pages_spec文件中的修订代码(部分,只是“授权”部分):
describe "authorization", type: :request do
.
.
.
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) {FactoryGirl.create(:user, email: "wrong@example.com") }
before { sign_in user, no_capybara: true }
describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_title(full_title('Edit user')) }
end
describe "submitting a PATCH request to the Users#update action" do
before { patch user_path(wrong_user) }
# NOTE: Rails Tutorial uses root_url below not root_path
specify { expect(response).to redirect_to(root_path) }
end
end
end
end
答案 0 :(得分:9)
get
,put
,patch
等仅针对:controller
和:request
规范定义,而不是:feature
规范,当您更改其位置时,您的authentication_pages_spec.rb
测试会隐式变为。如果您在最高级别type: :request
中加入describe
,则应该有效。
答案 1 :(得分:0)
这个答案还修复了这个没有错误的rspec测试失败,这是Rails教程第9章(spec / requests / authentication_pages_spec.rb)的一部分
describe "submitting to the update action" do
before { patch user_path(user) }
specify { response.should redirect_to(signin_path) }
end