【问题标题】:'redirect_to root_path' does not appear as a redirect in RSpec integration test'redirect_to root_path' 在 RSpec 集成测试中不显示为重定向
【发布时间】:2011-08-18 21:18:06
【问题描述】:

我有一个带有以下路线的 Rails 应用程序

root :to => "pages#home"
scope "/:locale" do
  root :to => "pages#home"
  ...
  match "/sign_in" => "sessions#new"
  resources :sessions, :only => [:new, :create]
end

我的 ApplicationController 包含一个 default_url_options(),它会自动设置语言环境选项

我的 SessionsController 包含以下内容

class SessionsController < ApplicationController
  def new
  end

  def create
    redirect_to root_path
  end
end

所以还没有任何逻辑,只是一个重定向。当我在浏览器中运行应用程序时,转到登录页面,提交表单(发布到 /en/sessions),然后它按预期工作:我被重定向到 /en

但是集成测试无法识别重定向

describe "sign-in" do
  before(:each) do
    visit "/en/sign_in"
    @user = Factory.create(:user)
  end

  context "with valid attributes" do
    before(:each) do
      fill_in "email", :with => @user.email
      fill_in "password", :with => @user.password
    end

    it "should redirect to root" do
      click_button "Sign in"
      response.should be_redirect
      response.should redirect_to "/en"
    end
  end
end

测试失败并显示消息

5) Authentication sign-in with valid attributes should redirect to root
   Failure/Error: response.should be_redirect
     expected redirect? to return true, got false

因此,即使应用程序正确重定向,RSpec 也不会将响应视为重定向。

如果我只是为了好玩,把create的实现改成

def create
  redirect_to new_user_path
end

然后我收到错误消息

6) SessionsController POST 'create' with valid user should redirect to root
   Failure/Error: response.should redirect_to root_path
     Expected response to be a redirect to <http://test.host/en> but was a redirect to <http://test.host/en/users/new>

这当然是预期的错误消息,因为函数现在重定向到错误的 url。但是为什么 new_user_path 会导致 RSpec 视为重定向的重定向,而 root_path 会导致 RSpec 无法识别为重定向的重定向?

更新

基于cmets,我修改测试验证状态码

  it "should redirect to root" do
    click_button "Sign in"
    response.status.should == 302
    response.should be_redirect
    response.should redirect_to "/en"
  end

导致错误

5) Authentication sign-in with valid attributes should redirect to root
   Failure/Error: response.status.should == 302
     expected: 302
          got: 200 (using ==)

【问题讨论】:

  • 可能是 rspec 的错误,您是否尝试检查响应 http 代码?
  • 可能与github.com/rspec/rspec-core/pull/410这个问题有关。尝试使用最新版本的 rspec
  • 我尝试使用 github 的 RSpec 版本,但没有帮助

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


【解决方案1】:

我知道这听起来可能很愚蠢,但请尝试将第一个 'root :to => "pages#home"' 放在您的路线文件的底部。你也可以试试:

scope "/:locale", :as => "localized" do
  root :to => "pages#home"
  ...
  match "/sign_in" => "sessions#new"
  resources :sessions, :only => [:new, :create]
end
root :to => "pages#home"

然后在你的测试中你会检查重定向到localized_root_path

我可能疯了,但我认为这可能是与命名路线的名称冲突。如果您检查rake routes,您可能会发现您有两个命名路由,名为root。由于路线是“第一次匹配”,您很可能只是在测试中选择了错误的路线。

【讨论】:

  • 你说得对,我有两条名为“root”的路由。但问题似乎并不在于我选择了错误的路线,因为如果是这样,我会假设错误是我重定向到了错误的 url。但错误是根本没有重定向。我尝试了一堆不同的路由,没有一个帮助。这包括您的建议、显式重命名根路由以及删除默认根路由的事件。没有帮助:(
【解决方案2】:

我想我解决了这个问题。修改验证码为

it "should redirect to root" do
  current_url.should == root_url("en")
end

有效。

我假设我的问题的原因是因为 webrat 实际上遵循重定向,所以在我最初的测试中,我正在测试重定向后第二个响应的响应代码。

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多