Yep, thank you Chris, I didn't know this tip, really useful.

On Fri, 17 Aug 2018 at 22:34, Chris Irish <[email protected]> wrote:

> You could also just reload the user
>
> expect(user.reload.token).to be_nil
>
> On Friday, August 17, 2018 at 7:02:48 AM UTC-7, belgoros wrote:
>>
>>
>>
>> On Friday, 17 August 2018 15:49:36 UTC+2, belgoros wrote:
>>>
>>>
>>>
>>> On Friday, 17 August 2018 15:32:23 UTC+2, belgoros wrote:
>>>>
>>>> What's wrong with the following request spec example (using
>>>> FactoryBotRails):
>>>>
>>>> RSpec.describe "Users", type: :request do
>>>>   let(:user)    { create(:user) }
>>>>   let(:headers) { valid_headers(user.username) }
>>>>
>>>>
>>>>   describe 'PATCH /users/logout' do
>>>>     before { patch users_logout_path, params: {}, headers: headers }
>>>>
>>>>
>>>>     it 'nullifies user token' do
>>>>       expect(user.token).to be_nil
>>>>       expect(response).to have_http_status(204)
>>>>     end
>>>>   end
>>>> end
>>>>
>>>> Here is helper method in support/controller_spec_helper.rb
>>>>
>>>> def valid_headers(username)
>>>>     {
>>>>       'Authorization' => "Bearer #{token_generator(username)}",
>>>>       'Content-Type' => 'application/json'
>>>>     }
>>>>   end
>>>>
>>>>
>>>> def token_generator(username)
>>>>     JsonWebToken.encode(sub: username)
>>>> end
>>>>
>>>> Here is the controller code:
>>>>
>>>> #UsersController
>>>>
>>>> def logout
>>>>     @current_user.update_attribute(:token, nil)
>>>>     head :no_content
>>>> end
>>>>
>>>> I checked `@current_user` after updating his token, it was `nil`. Why
>>>> it is nit the case in the spec example ?
>>>>
>>>
>>> Here is how @current_user is implemented:
>>>
>>> #ApplicationController
>>>
>>>
>>> class ApplicationController < ActionController::API
>>>     before_action :authorize_request
>>>   attr_reader :current_user
>>>
>>>
>>>   private
>>>
>>>
>>>     def authorize_request
>>>       @current_user = (AuthorizeApiRequest.new(request.headers).call)[:
>>> user]
>>>     end
>>> end
>>>
>>>
>>> I figured out, - the difference between the User instance created in
>> spec example and the one used in  the controller. The solution I came to is
>> to find the user by id in the spec example once logout action executed as
>> follows:
>>
>> RSpec.describe 'Users', type: :request do
>>   let(:user)    { create(:user) }
>>   let(:headers) { valid_headers(user.username) }
>>
>>
>>   describe 'PATCH /users/logout' do
>>     before do
>>       patch users_logout_path, params: {}, headers: headers
>>     end
>>
>>
>>     it 'nullifies user token' do
>>       user_without_token = User.find(user.id)
>>       expect(user_without_token.token).to be_nil
>>       expect(response).to have_http_status(204)
>>     end
>>   end
>> end
>>
>>
>> Hope this helps
>>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "rspec" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/rspec/pVS-VwwtRGM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rspec/8d3f0603-465d-4388-9b4f-813dd511fbe0%40googlegroups.com
> <https://groups.google.com/d/msgid/rspec/8d3f0603-465d-4388-9b4f-813dd511fbe0%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/CAJGQ%3DvauuGRVEk2D5akd0xSFiM__voJbLcxF_%2B%2B5LUK5s-SmBw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to