首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何测试after_sign_in_path_for(资源)?

如何测试after_sign_in_path_for(资源)?
EN

Stack Overflow用户
提问于 2011-07-12 08:26:13
回答 5查看 4.8K关注 0票数 19

我已经在我的Rails应用上设置了身份验证和注册。我使用after_sign_in_path_for()根据不同的场景定制用户登录时的重定向。

我想问的是如何测试这种方法?这似乎很难隔离,因为当用户登录时,Devise会自动调用它。我想做这样的事情:

代码语言:javascript
复制
describe ApplicationController do
  describe "after_sign_in_path_for" do
    before :each do
      @user = Factory :user
      @listing = Factory :listing
      sign_in @user
    end

    describe "with listing_id on the session" do
      before :each do
        session[:listing_id] = @listing.id
      end

      describe "and a user in one team" do
        it "should save the listing from the session" do
          expect {
            ApplicationController.new.after_sign_in_path_for(@user)
          }.to change(ListingStore, :count).by(1)
        end

        it "should return the path to the users team page" do
          ApplicationController.new.after_sign_in_path_for(@user).should eq team_path(@user.team)
        end
      end
    end
  end
end

但这显然不是这样做的方法,因为我只是得到了一个错误:

代码语言:javascript
复制
 Failure/Error: ApplicationController.new.after_sign_in_path_for(@user)
 RuntimeError:
   ActionController::Metal#session delegated to @_request.session, but @_request is nil: #<ApplicationController:0x00000104581c68 @_routes=nil, @_action_has_layout=true, @_view_context_class=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil>

那么,我如何测试这个方法呢?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-07-12 08:35:43

奇怪的是,我今天也在想这件事。这是我想出来的。我创建了ApplicationController的匿名子类。在这个匿名子类中,我公开了要作为公共方法测试的受保护方法。然后我直接对它们进行了测试。

代码语言:javascript
复制
describe ApplicationController do
  controller do
    def after_sign_in_path_for(resource)
        super resource
    end
  end

  before (:each) do
    @user = FactoryGirl.create(:user)
  end

  describe "After sigin-in" do
    it "redirects to the /jobs page" do
        controller.after_sign_in_path_for(@user).should == jobs_path
    end
  end

end
票数 33
EN

Stack Overflow用户

发布于 2011-07-12 11:35:06

同样,如果你想在注册后测试重定向,你有两个选择。

首先,您可以遵循类似于上面的模式,并在RegistrationsController中非常直接地测试该方法:

代码语言:javascript
复制
require 'spec_helper'

describe RegistrationsController do

  controller(RegistrationsController) do
    def after_sign_up_path_for(resource)
        super resource
    end
  end

  describe "After sign-up" do
    it "redirects to the /organizations/new page" do
        @user = FactoryGirl.build(:user)
        controller.after_sign_up_path_for(@user).should == new_organization_path
    end
  end
end

或者,您可以采用一种更具集成测试性质的方法,并执行以下操作:

代码语言:javascript
复制
require 'spec_helper'

describe RegistrationsController do

  describe "After successfully completing the sign-up form" do

    before do
        @request.env["devise.mapping"] = Devise.mappings[:user]
    end

    it "redirects to the new organization page" do
        post :create, :user => {"name" => "Test User", "email" => "test@example.com", "password" => "please"}
        response.should redirect_to(new_organization_path)
    end
  end
end
票数 4
EN

Stack Overflow用户

发布于 2017-09-20 07:36:54

对于新手,我建议这样做:

代码语言:javascript
复制
RSpec.describe ApplicationController, type: :controller do
  let(:user) { create :user }

  describe "After sing-in" do
    it "redirects to the /yourpath/ home page" do
      expect(subject.after_sign_in_path_for(user)).to eq(yourpath_root_path)
    end
  end
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6658251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档