Created
February 26, 2015 16:44
-
-
Save Jeiwan/65f8ba9afb2743f2f190 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
module Authenticable | |
def current_user | |
@current_user ||= 'Pedro' | |
end | |
def authenticate_with_token! | |
render json: { errors: "Not authenticated" }, | |
status: :unauthorized unless user_signed_in? | |
end | |
def user_signed_in? | |
current_user != nil | |
end | |
end | |
class Authentication | |
include Authenticable | |
end | |
RSpec.describe Authenticable do | |
let(:authentication) { Authentication.new } | |
subject { authentication } | |
describe '.authenticate_with_token' do | |
before do | |
allow(authentication).to receive(:current_user).and_return(nil) | |
allow(authentication).to receive(:render) do |args| | |
args | |
end | |
end | |
it 'returns error' do | |
expect(authentication.authenticate_with_token![:json][:errors]).to eq 'Not authenticated' | |
end | |
it 'returns unauthorized status' do | |
expect(authentication.authenticate_with_token![:status]).to eq :unauthorized | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment