7

我收到以下錯誤,並不能找出如何解決它。有堆棧溢出解決類似問題的其他一些網頁,但這些似乎並不適用,或者至少我沒有足夠的悟性去解決它。未定義的方法「補丁」 <Rspec的核心:: - Rails的教程第9章

當我運行authentication_pages_spec,這個問題似乎是與使用的「補丁」,導致下面的故障。請注意,我已經嘗試用「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 hereblog it references here我當選爲改變我的規格/請求文件夾的投機/功能,因爲這似乎是什麼水豚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使用規格/功能文件夾,而不是規範的/請求文件夾:

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 

規格的輔助文件的位置:

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 

最後,好措施,在配置/ 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' 

有堆棧溢出here類似的討論,但在該職位的答案是,從移動到./spec/requests的./spec/features測試 - 我已經完成了。

另一個相關的討論here,當是在特徵文件的頂部...我已經使用require 'spec_helper'

最後一個觀察:就這個主題的討論似乎很多表明問題是水豚需要位於spec/features文件夾中的測試才能訪問URL助手 - 但我的authentication_pages_spec.rb文件似乎對'訪問'沒有任何問題。

欣賞任何見解! Rails 4.0的Rails教程處於測試階段,一直在試圖弄清楚這是否是一個錯誤。

* UPDATE *

得到的一切傳遞綠色執行彼得的建議後。以下是從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: "[email protected]") } 
     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

你使用特定的寶石和代碼運行到一個問題的一部分邁克爾包括在他的教程中?他對這些事情通常非常小心。 –

+0

我最初一直忠於教程,但後來遇到了一些使用規範文件中的'訪問'的問題 - 它返回'未定義方法'錯誤。問題似乎是Capybara 2.0處理鏈接的方式 - 需要在spec/features文件夾中進行集成測試。見[這裏](http://stackoverflow.com/questions/13496196/why-arent-my-spec-support-files-being-picked-up-with-rspec/18231153#18231153),也[這裏](http ://www.andylindeman.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html)。 – Scro

回答

9

getputpatch等僅用於:controller:request規格定義,而不是一個:feature屬,當你改變它的位置時,你的authentication_pages_spec.rb測試隱含地變成了。如果在頂級describe中包含type: :request,它應該可以工作。

+0

感謝彼得,工作。另一件奇怪的事情 - 我不斷收到一個「失蹤的主機鏈接!」錯誤,這似乎與在PATCH測試中使用'redirect_to(root_url)'有關。經過幾個小時的嘗試修復路由,設置主機在環境配置文件等,我放棄了,並交換到'redirect_to(root_path)',所有的測試現在傳遞綠色。 – Scro

0

這個問題的答案也修復了這個RSpec的測試沒有錯誤失敗這是Rails教程第9章(規格/請求/ authentication_pages_spec.rb)

describe "submitting to the update action" do 

     before { patch user_path(user) } 

     specify { response.should redirect_to(signin_path) } 

    end 
相關問題