Skip to content

Commit

Permalink
Convert to rspec-mocks
Browse files Browse the repository at this point in the history
Another step in modernizing the clearance test suite. We've been using
rspec-mocks on our projects for over a year now.
  • Loading branch information
derekprior committed Dec 20, 2014
1 parent 23d715d commit 32e828c
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 52 deletions.
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -4,7 +4,6 @@ gemspec

gem 'appraisal', '~> 1.0'
gem 'aruba', '~> 0.5'
gem 'bourne', '~> 1.4'
gem 'bundler', '~> 1.3'
gem 'capybara', '~> 2.2.0'
gem 'cucumber-rails', '~> 1.3', require: false
Expand Down
6 changes: 0 additions & 6 deletions Gemfile.lock
Expand Up @@ -45,8 +45,6 @@ GEM
cucumber (>= 1.1.1)
rspec-expectations (>= 2.7.0)
bcrypt (3.1.9)
bourne (1.5.0)
mocha (>= 0.13.2, < 0.15)
builder (3.2.2)
capybara (2.2.1)
mime-types (>= 1.16)
Expand Down Expand Up @@ -87,12 +85,9 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
metaclass (0.0.4)
mime-types (1.25.1)
mini_portile (0.6.0)
minitest (5.4.0)
mocha (0.14.0)
metaclass (~> 0.0.1)
multi_json (1.10.1)
multi_test (0.1.1)
nokogiri (1.6.3.1)
Expand Down Expand Up @@ -166,7 +161,6 @@ PLATFORMS
DEPENDENCIES
appraisal (~> 1.0)
aruba (~> 0.5)
bourne (~> 1.4)
bundler (~> 1.3)
capybara (~> 2.2.0)
clearance!
Expand Down
8 changes: 4 additions & 4 deletions spec/clearance/back_door_spec.rb
Expand Up @@ -3,8 +3,8 @@
describe Clearance::BackDoor do
it 'signs in as a given user' do
user_id = '123'
user = stub('user')
User.stubs(:find).with(user_id).returns(user)
user = double("user")
allow(User).to receive(:find).with(user_id).and_return(user)
env = env_for_user_id(user_id)
back_door = Clearance::BackDoor.new(mock_app)

Expand All @@ -20,7 +20,7 @@

result = back_door.call(env)

expect(env[:clearance]).to have_received(:sign_in).never
expect(env[:clearance]).not_to have_received(:sign_in)
expect(result).to eq mock_app.call(env)
end

Expand All @@ -29,7 +29,7 @@ def env_without_user_id
end

def env_for_user_id(user_id)
clearance = stub('clearance', sign_in: true)
clearance = double("clearance", sign_in: true)
Rack::MockRequest.env_for("/?as=#{user_id}").merge(clearance: clearance)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/clearance/default_sign_in_guard_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe Clearance::DefaultSignInGuard do
context 'session is signed in' do
it 'returns success' do
session = stub('Session', signed_in?: true)
session = double("Session", signed_in?: true)
guard = Clearance::DefaultSignInGuard.new(session)

expect(guard.call).to be_a Clearance::SuccessStatus
Expand All @@ -12,7 +12,7 @@

context 'session is not signed in' do
it 'returns failure' do
session = stub('Session', signed_in?: false)
session = double("Session", signed_in?: false)
guard = Clearance::DefaultSignInGuard.new(session)

response = guard.call
Expand Down
6 changes: 3 additions & 3 deletions spec/clearance/rack_session_spec.rb
Expand Up @@ -3,8 +3,8 @@
describe Clearance::RackSession do
it 'injects a clearance session into the environment' do
expected_session = 'the session'
expected_session.stubs :add_cookie_to_headers
Clearance::Session.stubs new: expected_session
allow(expected_session).to receive(:add_cookie_to_headers)
allow(Clearance::Session).to receive(:new).and_return(expected_session)
headers = { 'X-Roaring-Lobster' => 'Red' }

app = Rack::Builder.new do
Expand All @@ -19,6 +19,6 @@
expect(Clearance::Session).to have_received(:new).with(env)
expect(response.body).to eq expected_session
expect(expected_session).to have_received(:add_cookie_to_headers).
with(has_entries(headers))
with(hash_including(headers))
end
end
34 changes: 18 additions & 16 deletions spec/clearance/session_spec.rb
Expand Up @@ -59,14 +59,14 @@
end

def stub_status(status_class, success)
stub('status', success?: success).tap do |status|
status_class.stubs(new: status)
double("status", success?: success).tap do |status|
allow(status_class).to receive(:new).and_return(status)
end
end

def stub_callable
lambda {}.tap do |callable|
callable.stubs(:call)
allow(callable).to receive(:call)
end
end
end
Expand Down Expand Up @@ -104,27 +104,29 @@ def stub_callable
def stub_sign_in_guard(options)
session_status = stub_status(options.fetch(:succeed))

stub('guard', call: session_status).tap do |guard|
double("guard", call: session_status).tap do |guard|
Clearance.configuration.sign_in_guards << stub_guard_class(guard)
end
end

def stub_default_sign_in_guard
stub(:default_sign_in_guard).tap do |sign_in_guard|
Clearance::DefaultSignInGuard.stubs(:new).with(session).
returns(sign_in_guard)
double("default_sign_in_guard").tap do |sign_in_guard|
allow(Clearance::DefaultSignInGuard).to receive(:new).
with(session).
and_return(sign_in_guard)
end
end

def stub_guard_class(guard)
stub(:guard_class).tap do |guard_class|
guard_class.stubs(:new).with(session, stub_default_sign_in_guard).
returns(guard)
double("guard_class").tap do |guard_class|
allow(guard_class).to receive(:new).
with(session, stub_default_sign_in_guard).
and_return(guard)
end
end

def stub_status(success)
stub('status', success?: success)
double("status", success?: success)
end

after do
Expand Down Expand Up @@ -163,7 +165,7 @@ def stub_status(success)
describe 'remember token cookie expiration' do
context 'default configuration' do
it 'is set to 1 year from now' do
user = stub('User', remember_token: '123abc')
user = double("User", remember_token: "123abc")
headers = {}
session = Clearance::Session.new(env_without_remember_token)
session.sign_in user
Expand All @@ -181,7 +183,7 @@ def stub_status(success)
expiration = -> { Time.now }
with_custom_expiration expiration do
session = Clearance::Session.new(env_without_remember_token)
session.stubs(:warn)
allow(session).to receive(:warn)
session.add_cookie_to_headers headers

expect(session).to have_received(:warn).once
Expand All @@ -191,11 +193,11 @@ def stub_status(success)
it 'is set to the value of the evaluated lambda' do
expires_at = -> { 1.day.from_now }
with_custom_expiration expires_at do
user = stub('User', remember_token: '123abc')
user = double("User", remember_token: "123abc")
headers = {}
session = Clearance::Session.new(env_without_remember_token)
session.sign_in user
session.stubs(:warn)
allow(session).to receive(:warn)
session.add_cookie_to_headers headers

expect(headers).to set_cookie(
Expand All @@ -213,7 +215,7 @@ def stub_status(success)
cookies['remember_me'] ? remembered_expires : nil
end
with_custom_expiration expires_at do
user = stub('User', remember_token: '123abc')
user = double("User", remember_token: "123abc")
headers = {}
environment = env_with_cookies(remember_me: 'true')
session = Clearance::Session.new(environment)
Expand Down
18 changes: 10 additions & 8 deletions spec/clearance/sign_in_guard_spec.rb
Expand Up @@ -3,25 +3,27 @@
module Clearance
describe SignInGuard do
it 'handles success' do
sign_in_guard = SignInGuard.new(stub('session'))
status = stub('status')
SuccessStatus.stubs(:new).returns(status)
sign_in_guard = SignInGuard.new(double("session"))
status = double("status")
allow(SuccessStatus).to receive(:new).and_return(status)

expect(sign_in_guard.success).to eq(status)
end

it 'handles failure' do
sign_in_guard = SignInGuard.new(stub('session'))
status = stub('status')
sign_in_guard = SignInGuard.new(double("session"))
status = double("status")
failure_message = "Failed"
FailureStatus.stubs(:new).with(failure_message).returns(status)
allow(FailureStatus).to receive(:new).
with(failure_message).
and_return(status)

expect(sign_in_guard.failure(failure_message)).to eq(status)
end

it 'can proceed to the next guard' do
guards = stub('guards', call: true)
sign_in_guard = SignInGuard.new(stub('session'), guards)
guards = double("guards", call: true)
sign_in_guard = SignInGuard.new(double("session"), guards)
sign_in_guard.next_guard
expect(guards).to have_received(:call)
end
Expand Down
5 changes: 3 additions & 2 deletions spec/clearance/testing/helpers_spec.rb
Expand Up @@ -16,8 +16,9 @@ def sign_in(user); end
describe '#sign_in' do
it 'creates an instance of the clearance user model with FactoryGirl' do
MyUserModel = Class.new
FactoryGirl.stubs(:create)
Clearance.configuration.stubs(user_model: MyUserModel)
allow(FactoryGirl).to receive(:create)
allow(Clearance.configuration).to receive(:user_model).
and_return(MyUserModel)

TestClass.new.sign_in

Expand Down
2 changes: 1 addition & 1 deletion spec/clearance/token_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe Clearance::Token do
it 'is a random hex string' do
token = 'my_token'
SecureRandom.stubs(:hex).with(20).returns(token)
allow(SecureRandom).to receive(:hex).with(20).and_return(token)

expect(Clearance::Token.new).to eq token
end
Expand Down
6 changes: 3 additions & 3 deletions spec/models/bcrypt_migration_from_sha1_spec.rb
Expand Up @@ -10,13 +10,13 @@
describe '#password=' do
let(:salt) { 'salt' }
let(:password) { 'password' }
let(:encrypted_password) { stub('encrypted password') }
let(:encrypted_password) { double("encrypted password") }

before do
subject.salt = salt
digestable = "--#{salt}--#{password}--"
subject.encrypted_password = Digest::SHA1.hexdigest(digestable)
BCrypt::Password.stubs create: encrypted_password
allow(BCrypt::Password).to receive(:create).and_return(encrypted_password)
subject.password = password
end

Expand All @@ -43,7 +43,7 @@
before do
subject.salt = salt
subject.encrypted_password = sha1_hash
subject.stubs save: true
allow(subject).to receive(:save).and_return(true)
end

it 'is authenticated' do
Expand Down
7 changes: 4 additions & 3 deletions spec/models/bcrypt_spec.rb
Expand Up @@ -7,10 +7,10 @@

describe '#password=' do
let(:password) { 'password' }
let(:encrypted_password) { stub('encrypted password') }
let(:encrypted_password) { double("encrypted password") }

before do
BCrypt::Password.stubs create: encrypted_password
allow(BCrypt::Password).to receive(:create).and_return(encrypted_password)
end

it 'encrypts the password into encrypted_password' do
Expand All @@ -20,7 +20,8 @@
end

it 'encrypts with BCrypt using default cost in non test environments' do
Rails.stubs env: ActiveSupport::StringInquirer.new("production")
allow(Rails).to receive(:env).
and_return(ActiveSupport::StringInquirer.new("production"))

subject.password = password

Expand Down
2 changes: 1 addition & 1 deletion spec/models/user_spec.rb
Expand Up @@ -84,7 +84,7 @@
end

it 'does not generate same remember token for users with same password at same time' do
Time.stubs now: Time.now
allow(Time).to receive(:now).and_return(Time.now)
password = 'secret'
first_user = create(:user, password: password)
second_user = create(:user, password: password)
Expand Down
10 changes: 8 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -10,7 +10,6 @@

require 'clearance/testing/application'
require 'rspec/rails'
require 'bourne'
require 'factory_girl_rails'
require 'shoulda-matchers'
require 'clearance/rspec'
Expand All @@ -22,9 +21,16 @@

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.mock_with :mocha
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!

config.expect_with :rspec do |expectations|
expectations.syntax = :expect
end

config.mock_with :rspec do |mocks|
mocks.syntax = :expect
end
end

def restore_default_config
Expand Down

0 comments on commit 32e828c

Please sign in to comment.