Rspec测试失败authenthicate_client不起作用 [英] Rspec test fails authenthicate_client not working

查看:43
本文介绍了Rspec测试失败authenthicate_client不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个旧项目升级到Rails 5,在失败的rspec测试中,我说:

I am upgrading a legacy project to rails 5 and among the rspec tests that are failing I have one that says:

 Failure/Error: expect(response).to be_redirect
   expected `#<ActionDispatch::TestResponse:0x00007fbe5fde51f0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::...ch::Http::Headers:0x00007fbe5fdde9e0 @req=#<ActionController::TestRequest:0x00007fbe5fde5358 ...>>>>.redirect?` to return true, got false
 # ./spec/controllers/search_controller_spec.rb:86:in `block (3 levels) in <top (required)>'

我正在使用devise gem对客户端进行身份验证。

测试如下:

I am using devise gem to authenticate clients.
The tests are as follows:

describe SearchController do

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

  describe "authenticated search" do
    # a bunch of tests
  end

  describe "unauthenticated search" do
    it "requires a user to be authenticated" do
      get :search, params: { q: "tec" }, format: :json

      expect(response).to be_redirect  # FAILING HERE
    end
  end
end

如果我手动运行测试并转到 / search?q = tec 我被重定向到sign_in页面。 search_controller.rb 具有 before_action:authenticate_client!

If I run the test manually and go to /search?q=tec I get redirected to the sign_in page. The search_controller.rb has a before_action :authenticate_client!

我尝试在搜索之前添加 sign_out @client ,但是没有用。
还尝试了 current_client.reload ,但无法识别current_client。

I tried adding sign_out @client before the search but it didn't work. Also tried current_client.reload but didn't recognize current_client.

在经过身份验证的搜索测试中是对具有以下代码的 stub_authenticate_client 的调用:

In the authenticated search tests there is a call to stub_authenticate_client that has the following code:

  def stub_authenticate_client(client)
    allow(request.env['warden']).to receive(:authenticate!) { client }
    allow(controller).to receive(:current_client) { client }
  end

以防解决此问题。

我也尝试过创建 stub_logout_client 方法,如下所示:

I also tried creating a stub_logout_client method like this:

  def stub_logout_client(client)
    allow(request.env['warden']).to receive(:authenticate!) { nil }
    allow(controller).to receive(:current_client) { nil }
  end

并在测试开始时调用它,但仍然传递 before_action authenticate_client!

and calling it at the beginning of the test, but it is still passing the before_action authenticate_client!

也尝试了建议的内容在这里,但不起作用

Also tried what it was suggested here, but didn't work

正在测试:

class SearchController < ClientApplicationController
  before_action :authenticate_client!

  def search
    limit = params[:limit] ? params[:limit].to_i : 10
    query = params[:q].to_s.downcase.strip

    results = {}

    if params[:report]
      results[:this_report] = Report.where("SOME QUERY")
    end

    render json: results
  end
end

谢谢!

推荐答案

问题与 be_redirect 检查有关。更改测试以检查响应中的内容并解决了该问题,如下所示:

The problem is related to the be_redirect check. Changed the test to check for content in the response and that solved it, like this:

describe "unauthenticated search" do
    it "requires a user to be authenticated" do
      get :search, params: { q: "tec" }, format: :json
      expect(response.body).to have_content("content from the page I render")
    end
  end

这篇关于Rspec测试失败authenthicate_client不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆