diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-09-17 13:08:05 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-09-25 12:57:55 +0100 |
commit | 66356f84ff478cc785bc1fb1d976a8f661ca79ab (patch) | |
tree | d65e5d108a7be9ed18940ede7fc554e32b395b7d /spec/lib | |
parent | 1abc8026afed626676ce4ca156f93962b93aab3f (diff) |
Add HealthCheck and HealthCheckable
HealthCheck is a basic library for creating health checks.
HealthCheck works with any class so long as it includes HealthCheckable
and defines the `check` method.
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/health_checks/health_checkable_spec.rb | 128 | ||||
-rw-r--r-- | spec/lib/health_checks/health_checks_spec.rb | 77 |
2 files changed, 205 insertions, 0 deletions
diff --git a/spec/lib/health_checks/health_checkable_spec.rb b/spec/lib/health_checks/health_checkable_spec.rb new file mode 100644 index 000000000..abfeb5c21 --- /dev/null +++ b/spec/lib/health_checks/health_checkable_spec.rb @@ -0,0 +1,128 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe HealthChecks::HealthCheckable do + + before(:each) do + class MockCheck + include HealthChecks::HealthCheckable + end + @subject = MockCheck.new + end + + describe :initialize do + + it 'allows a custom failure message to be set' do + @subject = MockCheck.new(:failure_message => 'F') + expect(@subject.failure_message).to eq('F') + end + + it 'allows a custom success message to be set' do + @subject = MockCheck.new(:success_message => 'S') + expect(@subject.success_message).to eq('S') + end + + end + + describe :name do + + it 'returns the name of the check' do + expect(@subject.name).to eq('MockCheck') + end + + end + + describe :check do + + it 'is intended to be overridden by the includer' do + expect{ @subject.check }.to raise_error(NotImplementedError) + end + + end + + describe :ok? do + + it 'returns true if the check was successful' do + @subject.stub(:check => true) + expect(@subject.ok?).to be_true + end + + it 'returns false if the check failed' do + @subject.stub(:check => false) + expect(@subject.ok?).to be_false + end + + end + + describe :failure_message do + + it 'returns a default message if one has not been set' do + expect(@subject.failure_message).to eq('Failed') + end + + end + + describe :failure_message= do + + it 'allows a custom failure message to be set' do + @subject.failure_message = 'F' + expect(@subject.failure_message).to eq('F') + end + + end + + describe :success_message do + + it 'returns a default message if one has not been set' do + expect(@subject.success_message).to eq('Success') + end + + end + + describe :success_message= do + + it 'allows a custom success message to be set' do + @subject.success_message = 'S' + expect(@subject.success_message).to eq('S') + end + + end + + describe :message do + + context 'if the check succeeds' do + + before(:each) do + @subject.stub(:check => true) + end + + it 'returns the default success message' do + expect(@subject.message).to eq('Success') + end + + it 'returns a custom success message if one has been set' do + @subject.success_message = 'Custom Success' + expect(@subject.message).to eq('Custom Success') + end + + end + + context 'if the check fails' do + + before(:each) do + @subject.stub(:check => false) + end + + it 'returns the default failure message' do + expect(@subject.message).to eq('Failed') + end + + it 'returns a custom failure message if one has been set' do + @subject.failure_message = 'Custom Failed' + expect(@subject.message).to eq('Custom Failed') + end + + end + + end + +end diff --git a/spec/lib/health_checks/health_checks_spec.rb b/spec/lib/health_checks/health_checks_spec.rb new file mode 100644 index 000000000..c7037b813 --- /dev/null +++ b/spec/lib/health_checks/health_checks_spec.rb @@ -0,0 +1,77 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe HealthChecks do + include HealthChecks + + describe :add do + + it 'adds a check to the collection and returns the check' do + check = double('MockCheck', :check => true) + expect(add(check)).to eq(check) + end + + it 'does not add checks that do not define the check method' do + check = double('BadCheck') + expect(add(check)).to eq(false) + end + + end + + describe :all do + + it 'returns all the checks' do + check1 = double('MockCheck', :check => true) + check2 = double('AnotherCheck', :check => false) + add(check1) + add(check2) + expect(all).to include(check1, check2) + end + + end + + describe :each do + + it 'iterates over each check' do + expect(subject).to respond_to(:each) + end + + end + + describe :ok? do + + it 'returns true if all checks are ok' do + checks = [ + double('MockCheck', :ok? => true), + double('FakeCheck', :ok? => true), + double('TestCheck', :ok? => true) + ] + HealthChecks.stub(:all => checks) + + expect(HealthChecks.ok?).to be_true + end + + it 'returns false if all checks fail' do + checks = [ + double('MockCheck', :ok? => false), + double('FakeCheck', :ok? => false), + double('TestCheck', :ok? => false) + ] + HealthChecks.stub(:all => checks) + + expect(HealthChecks.ok?).to be_false + end + + it 'returns false if a single check fails' do + checks = [ + double('MockCheck', :ok? => true), + double('FakeCheck', :ok? => false), + double('TestCheck', :ok? => true) + ] + HealthChecks.stub(:all => checks) + + expect(HealthChecks.ok?).to be_false + end + + end + +end |