When running a request spec test that requires authentication you will need
to have the web clients session set up accordingly, which would involve
making the necessary requests to log in. Eg, you could have something like,

before do
  post('/login', params: { client_id: client.id, password: 'test-password'
})
end

However, if you are using the devise gem for authentication there is a
helper you can use to do the signing in for your. See
https://github.com/heartcombo/devise/wiki/How-To:-sign-in-and-out-a-user-in-Request-type-specs-(specs-tagged-with-type:-:request)

So you might have something like this,

before do
  sign_in client
end

Incidentally, for controller specs you can add `session: { client_id:
client.id }` (for example) to the get and post requests in your tests to do
the same without having to stub the authentication methods. See
https://stackoverflow.com/questions/22451969/rspec-set-session-object

Hope this helps. The code examples may not be exactly correct as I am
typing them in mostly from memory, with the aid of Google.

Regards,

Joe

On Mon, 6 Sept 2021 at 21:25, Fernando Iwamoto <[email protected]>
wrote:

> I have my test controller using this type:
>
> describe CompanyController, type: :controller do
>
> which allow me to skip authentication:
>
> before do
> allow(controller).to receive(:authenticate_client).and_return(true)
> allow(controller).to receive(:current_client).and_return(client)
> end
>
> How can I achieve using the same as above, but for request type?
>
> before do
> allow(controller).to receive(:authenticate_client).and_return(true)
> allow(controller).to receive(:current_client).and_return(client)
> end
>
> --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rspec/fbe7b1ae-7e75-47eb-a718-41d48bf5a9ban%40googlegroups.com
> <https://groups.google.com/d/msgid/rspec/fbe7b1ae-7e75-47eb-a718-41d48bf5a9ban%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/CAKRXwc1Vb5qY9xmAg1sgEZ3k%2B%2B8Qcy7tYpE_cZUUNJ_TdHOR8g%40mail.gmail.com.

Reply via email to