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 ?
--
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/c3c8d57e-c38d-4ec2-a7d4-1dc18576c63f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.