【问题标题】:Redirecting the page in RSpec to some other page将 RSpec 中的页面重定向到其他页面
【发布时间】:2023-04-09 00:05:01
【问题描述】:

我正在使用 Devise gem 进行身份验证,我也指定了

before_filter :authenticate_contact! 

在我的ApplicationController。所以用户在访问应用程序之前需要login。如果他在没有登录的情况下错误输入url,它将要求用户进行身份验证,一旦他登录,它将重定向到他给定的url而不是仪表板,我使用了以下代码

def after_sign_in_path_for(resource)
    flash[:notice] = "Welcome #{current_contact.first_name} #{current_contact.last_name}, #{current_contact.company.genre}."
    session[:announcements] = true
    sign_in_url = new_contact_session_url
    if request.referer == sign_in_url
      super
    else
      stored_location_for(resource) || request.referer || root_path
    end
  end

这工作正常。我的下一步是编写 RSpec 规范:

在我的 spec/controller/application_controller_spec.rb 中

require 'spec_helper'

describe ApplicationController do
  controller do
    def after_sign_in_path_for(resource)
      sign_in_url = new_contact_session_url
      if request.referer == sign_in_url
        super
      else
        stored_location_for(resource) || request.referer || root_path
      end
    end
  end

  before (:each) do
    @user = FactoryGirl.create(:contact_owner, email: 'owner@test.com')
  end

  describe "After sigin-in" do
    it "redirects to the root path" do
      controller.after_sign_in_path_for(@user).should == root_path
    end
  end

end

此规范正在通过,它是第一个场景,即用户进入应用程序登录并获取root_path。但是我在测试第二个条件时遇到了困难,即如果用户在没有身份验证的情况下键入 url,它将要求身份验证,登录后,它应该重定向到他的路径而不是 root_path。

describe CompaniesController do
    before do 
      @user = FactoryGirl.create(:contact_owner, email: "test@test.com")
    end
    context "when logged in redirect to path" do
      before { sign_out @user }
      after { sign_in @user } #after this how can i proceed, here user is trying companies/index without login. once he logs in it should redirect_to(companies_path)
      it { get :index }
    end
  end

【问题讨论】:

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


    【解决方案1】:

    RSpec 中的 before 和 after 块用于设置和拆除示例:

    describe FooController do
      before { do_something } # runs before the nested describe
      describe "GET #index" do
        before { get :index } # runs after do_something
        after { clean_up } # runs after each example
        it "renders the correct template" do
          expect(response).to render_template(:index)
        end 
      end
    end
    

    这相当于 TDD 中的 setup 和 teardown 步骤。这里的关键点是你不能在 after 块之后继续。它不应该用于构建示例,而应该仅用于拆除任何固定装置或任何可能滞留的东西,例如会话数据或重置缓存。

    after 块很少在spec_helper.rb 或rails_helper.rb 之外使用。使用database_cleaner 和the test helpers for your auth system 之类的工具提供一个干净的状态,您几乎不需要在完成单个示例后进行清理。

    集成测试。

    控制器规格适用于低级别测试,让您可以快速测试控制器如何响应不同的输入。他们使用几个抽象来提高速度,这意味着他们不会触及整个堆栈,例如路由和视图渲染。

    要测试身份验证系统,您很可能需要 feature spec。

    主要区别在于,功能规范从实际用户的角度测试应用程序,您在应用程序中点击自己的方式并在用户呈现的内容上写下期望:

    # spec/features/authentication_spec.rb
    require 'rails_helper'
    RSpec.feature "Authentication" do
    
      let(:user) { FactoryGirl.create(:user) }
    
      scenario "when a user attemps to access a resource that requires authentication" do
        visit some_path
        expect(page).to have_content 'Please sign in'
      end
    
      scenario "when a user signs in with correct details" do
        visit root_path
        click_link 'Sign In'
        fill_in 'Email', with: user.email
        fill_in 'Password', with: user.password
        click_button 'Sign In'
        expect(page).to have_content "Welcome #{user.email}!"
      end
    end
    

    【讨论】:

    • 谢谢@max,会搞定的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 2017-05-01
    • 2012-03-16
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    相关资源
    最近更新 更多