Rspec用于检查用户使用设计宝石登录或未登录的天气

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rspec用于检查用户使用设计宝石登录或未登录的天气相关的知识,希望对你有一定的参考价值。

我已经在rails中使用了devise gem来验证用户身份。我需要编写测试用例来检查用户登录或不使用Rspec的天气。只有登录的用户才有权访问帖子索引页面。它非常适合在Rspec中登录用户。但是对于已退出的用户,我已经尝试了几个步骤,但我无法正确使用。我使用Warden进行简单的身份验证以进行测试。我的签名控制器规范为签名用户是:

describe "Unauthorized Access" do
    let(:user) {create(:user)}
    it "User tries to view post after Sign Out" do
      get :index
      expect(response).to have_http_status 401
    end
  end

规范文件夹中的守望助手。

RSpec.shared_context "api request global before and after hooks" do
  before(:each) do
    Warden.test_mode!
  end

  after(:each) do
    Warden.test_reset!
  end
end

RSpec.shared_context "api request authentication helper methods" do
  def sign_in(user)
    login_as(user, scope: :users)
  end

  def sign_out
    logout(:users)
  end
end
When I click sign out button it is redirected to the log in page and the rails log show the log like this

Started GET "/" for 127.0.0.1 at 2018-09-05 11:33:10 +0530
Processing by PostsController#index as html
Completed 401 Unauthorized in 2ms (ActiveRecord: 0.0ms)


Started GET "/users/sign_in" for 127.0.0.1 at 2018-09-05 11:33:10 +0530
Processing by Devise::SessionsController#new as HTML
  Rendering devise/sessions/new.html.erb within layouts/application
  Rendered devise/shared/_links.html.erb (0.9ms)
  Rendered devise/sessions/new.html.erb within layouts/application (5.0ms)
Completed 200 OK in 69ms (Views: 67.9ms | ActiveRecord: 0.0ms)
But when I run the tests the rspec console showing error.

expected the response to have status code 401 but it was 302

  0) PostsController Unauthorized Access User tries to view post after Sign Out
     Failure/Error: expect(response).to have_http_status 401
       expected the response to have status code 401 but it was 302
     # ./spec/controllers/posts_controller_spec.rb:150:in `block (3 levels) in <top (required)>'
Rspec log

  (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
   (0.4ms)  begin transaction
Processing by PostsController#index as HTML
Completed 401 Unauthorized in 8ms (ActiveRecord: 0.0ms)
   (0.1ms)  rollback transaction
答案

您应该按以下方式进行测试,根据您的要求进行更改。

describe PostsController do

  describe "Index" do

    it "should allow user to view the list of posts" do
      get :index, {}, sign_in(@user)
      expect(response).to redirect_to(posts_path)
    end

    it "should not list posts if the user is not logged in" do
      get :index
      expect(response.status).to eq(302)
      expect(response).to redirect_to(new_user_sessions_path)
    end
  end

end

现在这个测试会做什么,当它运行时它将调用你的控制器,假设你已经放了authenticate_user!,它将检查用户是否登录或查看列表。否则,action将返回401。

您也可以尝试以下方法

  RSpec.describe "Posts", :type => :request do
    context "Index" do
      it 'should return 401 when not logged in' do
        sign_out user
        get posts_path
        expect(response).to have_http_status(302)
        follow_redirect!
        expect(response).to have_http_status(401)
      end
    end
  end

以上是关于Rspec用于检查用户使用设计宝石登录或未登录的天气的主要内容,如果未能解决你的问题,请参考以下文章

使用登录设计的Rspec测试只工作一次

检查用户是不是已经在浏览器中使用 Firebase 登录 [重复]

测试保持与Rspec签署

如果用户登录或未登录,实现条件导航栏的策略

设计注册成功,登录失败

devise 和 rspec-rails - 如何在请求类型规范(带有 type: :request 标记的规范)中登录用户?