【问题标题】:Rails testing: "No route matches" for custom routeRails 测试:自定义路线的“无路线匹配”
【发布时间】:2013-01-14 21:37:45
【问题描述】:

我有以下路线:

GET    /confirm/:token(.:format)  Confirmations#confirm

控制器:

class ConfirmationsController < ApplicationController
  # GET /confirm/<token>
  def confirm
    @user = User.find_by_email_token(params[:token])
    if @user
      @user.confirmed = true
      @user.email_token = nil
      @user.save!
      sign_in @user
      redirect_to root_url, flash: { success: "Welcome <#{@user.email}>, your address has been verified." }
    elsif
      redirect_to root_url, flash: { error: "Error: could not find matching user record." }
    end
  end
end

还有这个简单的confirmations_controller_spec.rb

require 'spec_helper'

describe ConfirmationsController do

  let(:user) { FactoryGirl.create(:user, email_token: "some_token") }

  describe "Get confirm" do
    it "confirms user with valid email_token" do
      get :confirm, token: "some_token"
      assigns(:user).should eq(user)
      user.reload.email_token.should be_nil
    end

    it "does not confirm user with invalid email_token"
  end
end

但它失败了:

  1) ConfirmationsController Get confirm confirms user with valid email_token
     Failure/Error: get :confirm, token: "some_token"
     ActionController::RoutingError:
       No route matches {:token=>"some_token", :controller=>"confirmations", :action=>"confirm"}
     # ./spec/controllers/confirmations_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

有人知道我搞砸了什么(可能是多件事情)吗?

顺便说一句-我在这里使用get 请求(而不是put),因为它是从基于文本的电子邮件发起的,所以据我了解,我们不能使用put 请求。 .

【问题讨论】:

  • 如果您将get :confirm, token: "some_token" 更改为get '/confirm/some_token' 是否有效?
  • @mccannf - 不。 Failure/Error: get 'confirm/some_token'No route matches {:controller=&gt;"confirmations", :action=&gt;"confirm/some_token"}
  • @mccannf - 与'/confirm/some_token' 相同的问题:No route matches {:controller=&gt;"confirmations", :action=&gt;"/confirm/some_token"}

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


【解决方案1】:

在您的 rake 路由中,Confirmations 不应包含大写字母。

你能在config/routes.rb中这样定义路由吗:

match '/confirm/:token' => 'confirmations#confirm'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多