2013-11-25 2 views
0

이제 users_controller.rb에 대한 Rspec 테스트를하고 있습니다. 그러나 나는 다음과 같이 오류 NoMethodError: undefined method 'user_url'에 오류가 있습니다. Rspec 사용자 컨트롤러 - NoMethodError : 정의되지 않은 메서드 'user_url'

users_controller.rb

class UsersController < ApplicationController 
    def show 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.xml { render :xml => @user } 
    end 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 

    def update 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     if @user.update_attributes(params[:user]) 

     format.html { redirect_to @user, notice: 'User was successfully updated.' } 
     format.json { head :no_content } 
    else 
     format.html { render action: "user#edit" } 
     format.json { render json: @idea.errors, status: :unprocessable_entity } 
    end 
    end 
end 
end 

FF 

Failures: 

1) UsersController PUT update user update does not succeed 
Failure/Error: put :update, {:id => user.to_param}, valid_session, :user_route => user 
NoMethodError: 
    undefined method `user_url' for #<UsersController:0x52e40e0> 
# ./app/controllers/users_controller.rb:21:in `block (2 levels) in update' 
# ./app/controllers/users_controller.rb:18:in `update' 
# ./spec/controllers/users_controller_spec.rb:64:in `block (3 levels) in <top  (required)>' 

2) UsersController PUT update user update succeeds 
Failure/Error: put :update, {:id => user.to_param}, valid_session, :user_route => user 
NoMethodError: 
    undefined method `user_url' for #<UsersController:0x53bc560> 
# ./app/controllers/users_controller.rb:21:in `block (2 levels) in update' 
# ./app/controllers/users_controller.rb:18:in `update' 
# ./spec/controllers/users_controller_spec.rb:58:in `block (3 levels) in <top (required)>' 

Finished in 0.679 seconds 
2 examples, 2 failures 

Failed examples: 

rspec ./spec/controllers/users_controller_spec.rb:61 # UsersController PUT update user update does not succeed 
rspec ./spec/controllers/users_controller_spec.rb:56 # UsersController PUT update user update succeeds 

Randomized with seed 33412 

또한 여기 내 RSpec에 users_controller_spec.rb입니다. 나는 "POST update"에 대해 두 가지 테스트를했다. 하나는 성공적으로 업데이트됩니다. 다른 것은 업데이트되지 않는 것입니다. (후자에 대해서는, 나는 그 과정을 "다른"진행 있도록 "update_attribute"가 "false"를 반환 것으로 기대 스텁 User.stub(:update_attribute).and_return(false)를 넣습니다.) 내가 이해 할 수 없기 때문에

require 'spec_helper' 

describe UsersController do 

    let(:valid_attributes) { { 
    "email" => "[email protected]", 
    "password" => "12345678" 
    } } 

    def valid_session 
    {} 
    end 

    describe "PUT update" do 
    it "user update succeeds" do 
     user = User.create! valid_attributes 
     put :update, {:id => user.to_param}, valid_session 
     assigns(:user).should eq(user) 
    end 
    it "user update does not succeed" do 
     user = User.create! valid_attributes 
     User.stub(:update_attribute).and_return(false) 
     put :update, {:id => user.to_param}, valid_session 
     assigns(:user).should eq(user) 
     response.should render_template("edit") 
    end 
    end 
end 

내가,이 문제를 해결 할 생각이 없다 어디 user_url 왔어요. 그래서 당신의 도움을 받고 싶습니다.

답변

2

redirect_to @user을 사용하는 경우 레일은 UsersController#show으로 요청을 보냅니다. 그러나 user_url(@user)을 호출하면됩니다. 당신의 routes.rb 파일에

resources :users 

: 내 추측이 있다면, 당신은 아마 user_url을 정의 라인이 없습니다.

get "https://stackoverflow.com/users/show" => "users#show", as: :user 

을하지만은 '레일은 정말 아니다 : 이것은 자동으로 컨트롤러 또는 redirect_to @user

으로 참조하는 이름 경로 user_url을 만들 것입니다, 당신은 다음처럼 routes.rb 파일의 경로를 직접 정의 할 수 있습니다 - 할 '그것을 할 수있는 방법. 터미널에서 rake routes 명령을 실행하여 routes.rb 파일에 정의 된 모든 경로를 볼 수 있습니다. user이 없으면 위에 언급 한 것처럼 정의해야합니다. 여기라는 이름의 노선

상세 정보 : http://guides.rubyonrails.org/routing.html#singular-resources

1

다음과 같은 방법이 아무것도 반환하는 경우 확인 후 고안 사용하는 경우. application_controller.rb

에서

def after_sign_in_path_for(resource) 

방법은 아무것도 오류 받게됩니다 반환하지 않는 경우 : 나는 또한

stored_location_for(resource) 
을 제거 결국 #

에 대한

정의되지 않은 메서드`USER_URL를 '

in after_sign_in_path_for (자원) 원인이 있기 때문에 무한 루프. 자세한 내용은이 답변을 참조하십시오.

rails:3 Devise signup Filter chain halted as :require_no_authentication rendered or redirected

관련 문제