开发者

Fail at redirect expectation in a controller spec

开发者 https://www.devze.com 2023-03-31 03:46 出处:网络
I am using Devise 1.4.2, RSpec 2.6.0 and Rails 3.1.0.rc6. My routes.rb looks like this: scope \"(:locale)\", :locale => /e(s|n)/ do

I am using Devise 1.4.2, RSpec 2.6.0 and Rails 3.1.0.rc6. My routes.rb looks like this:

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

I am spec'ing that, when a not logged in user acces new, create or update, he should be redirected to new_user_session_path. For this, I am using the following code

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

All are failing because of the same reason: expected route includes the locale (by default en) but the actual redirect was to the same path without locale. My application controller was modified as told in Rails Guides:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_locale开发者_如何学Python

  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

What am I doing wrong?


Seems like though Rails Guides uses def default_url_options in Devise you need def self.default_url_options. Don't know the difference, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消