Rails测试:自定义路由的“无路由匹配”

时间:2013-01-14 21:37:45

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

我有以下路线:

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)>'

任何人都知道我搞砸了什么(可能是多件事)?

BTW-我在这里使用get请求(而不是put),因为它是从基于文本的电子邮件发起的,所以根据我的理解,我们不能使用{{{ 1}}请求...

1 个答案:

答案 0 :(得分:1)

在你的佣金路线中,Confirmations不应该有大写字母。

您可以在config/routes.rb中定义路线:

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