在控制器规范中重定向期望失败

时间:2011-08-28 22:47:03

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

我正在使用Devise 1.4.2,RSpec 2.6.0和Rails 3.1.0.rc6。我的routes.rb看起来像这样:

scope "(:locale)", :locale => /e(s|n)/ do
  resources :demotivideos, :only => [:index, :show]
  devise_for :users

  namespace "admin" do
    resources :demotivideos, :except => [:index, :show]
  end
end

我的意思是,当未登录的用户访问新的,创建或更新时,他应该被重定向到new_user_session_path。为此,我使用以下代码

context "when not logged in" do
  before(:each) do
    sign_out user
  end

  describe "GET new" do

    it "should redirect to new user session" do
      get :new
      response.should redirect_to(new_user_session_path)
    end
  end

  describe "POST create" do

    it "should redirect to new user session" do
      post :create, :demotivideo => valid_attributes
      response.should redirect_to(new_user_session_path)
    end
  end

  describe "PUT update" do

    it "should redirect to new user session" do
      put :update, :id => 1, :demotivideo => valid_attributes
      response.should redirect_to(new_user_session_path)
    end
  end
end

由于同样的原因,所有都失败了:预期的路由包括语言环境(默认为en),但实际的重定向是没有语言环境的相同路径。我的应用程序控制器已根据Rails Guides中的说明进行了修改:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_locale

  def default_url_options(options={})
    logger.debug "default_url_options is passed options: #{options.inspect}\n"
    { :locale => I18n.locale }
  end

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end
end

我做错了什么?

1 个答案:

答案 0 :(得分:1)

似乎Rails Guides使用Devise中的def default_url_options,但您需要def self.default_url_options。但不知道其中的区别。