2013-11-25 50 views
0

我现在正在进行Rspec测试users_controller.rb。不过,我遇到了如下错误NoMethodError: undefined method 'user_url'Rspec用户控制器 - NoMethodError:undefined方法'user_url'

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 

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 

而且,这里是我的Rspec的users_controller_spec.rb。我做了两次关于“POST更新”的测试。一个是成功更新。另一个是不更新。 (关于后者,我把存根User.stub(:update_attribute).and_return(false)这是我想到的是“update_attribute”返回“假”,使处理进入到“其他”。)

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时,ra​​ils将该请求发送到UsersController#show,但它通过调用user_url(@user)来完成。如果我猜的话,你可能没有定义user_url行:在你的routes.rb文件

resources :users 

。这会自动创建命名路线user_url您的控制器与redirect_to @user

或者引用,你可以定义自己的路线在你的routes.rb文件像这样:

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

但是,这并不是真正的“Rails的 - '的方式来做到这一点。在任何时候,您都可以在终端中运行命令rake routes以查看您在routes.rb文件中定义的所有命名路线。如果user不存在,那么您需要像上面提到的那样定义它。在这里命名路线

更多信息:http://guides.rubyonrails.org/routing.html#singular-resources

1

如果您使用的设计然后检查以下方法返回任何东西。

def after_sign_in_path_for(resource) 
在application_controller.rb

如果该方法返回什么你将收到错误:

未定义的方法`user_url”为#

我也结束了消除

stored_location_for(resource) 

in after_sign_in_path_for(resource)因为它是causi无尽的循环。有关详细信息,请参阅此答案。

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

相关问题