Rspec 测试失败 authenthicate_client 不工作 [英] Rspec test fails authenthicate_client not working
问题描述
我正在将一个遗留项目升级到 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
如果这有助于解决这个问题.
in case that is useful to solve this issue.
我也尝试创建一个 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
正在测试的搜索控制器:
The search controller that is being tested:
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屋!