Skip to content

Instantly share code, notes, and snippets.

@Jeiwan
Created February 26, 2015 16:44
Show Gist options
  • Save Jeiwan/65f8ba9afb2743f2f190 to your computer and use it in GitHub Desktop.
Save Jeiwan/65f8ba9afb2743f2f190 to your computer and use it in GitHub Desktop.
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