【问题标题】:Rspec user controller - NoMethodError: undefined method 'user_url'Rspec用户控制器 - NoMethodError:未定义的方法'user_url'
【发布时间】:2013-12-09 19:37:43
【问题描述】:

我现在正在为users_controller.rb 进行 Rspec 测试。但是我遇到了错误NoMethodError: undefined method '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 update”的测试。一是为了更新成功。另一个是不更新。 (关于后者,我将存根 User.stub(:update_attribute).and_return(false) 放入我希望“update_attribute”返回“false”以便进程继续进行到“else”。)

require 'spec_helper'

describe UsersController do

  let(:valid_attributes) { {
    "email" => "hoge@hogehoge.com",
    "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 到底是从哪里来的。所以我想得到你的帮助。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 rspec


    【解决方案1】:

    当您使用redirect_to @user 时,rails 将该请求发送到UsersController#show,但它通过调用user_url(@user) 来实现。如果我不得不猜测,您可能没有定义user_url 的行:

    resources :users
    

    在您的 routes.rb 文件中。这将自动创建您的控制器使用 redirect_to @user 引用的命名路由 user_url

    或者,您可以在您的 routes.rb 文件中自己定义路由,如下所示:

    get "/users/show" => "users#show", as: :user
    

    但这并不是真正的“Rails-y”方式。您可以随时在终端中运行命令rake routes 来查看您在routes.rb 文件中定义的所有命名路由。如果user 不存在,那么您需要像我上面提到的那样定义它。

    更多关于命名路线的信息:http://guides.rubyonrails.org/routing.html#singular-resources

    【讨论】:

      【解决方案2】:

      如果您使用的是 devise,请检查以下方法是否返回任何内容。

      def after_sign_in_path_for(resource)
      

      在 application_controller.rb 中

      如果该方法没有返回任何内容,您将收到错误:

      用于#的未定义方法`user_url'

      我也最终删除了

      stored_location_for(resource)
      

      在 after_sign_in_path_for(resource) 中,因为它导致了无限循环。有关详细信息,请参阅此答案。

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

      【讨论】:

        猜你喜欢
        • 2022-11-05
        • 2014-09-26
        • 2017-12-09
        • 1970-01-01
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多