Commit e003383b authored by Nick Stenning's avatar Nick Stenning

Be strict about request counting in tests

The angular $httpBackend mocking service supports two primary means of
usage:

   .expect(...).respond(...)

and

   .when(...).respond(...)

The latter pattern allows a single handler to respond to multiple
requests, in any order. The latter is precise -- one .expect() must be
matched by one request, in the correct order.

This commit changes the tests for the "features" service to use this
more precise form of testing.
parent ed0e1dfd
...@@ -8,7 +8,6 @@ sinon.assert.expose(assert, {prefix: null}); ...@@ -8,7 +8,6 @@ sinon.assert.expose(assert, {prefix: null});
describe('h:features', function () { describe('h:features', function () {
var $httpBackend; var $httpBackend;
var httpHandler;
var features; var features;
var sandbox; var sandbox;
...@@ -32,9 +31,6 @@ describe('h:features', function () { ...@@ -32,9 +31,6 @@ describe('h:features', function () {
beforeEach(mock.inject(function ($injector) { beforeEach(mock.inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend'); $httpBackend = $injector.get('$httpBackend');
features = $injector.get('features'); features = $injector.get('features');
httpHandler = $httpBackend.when('GET', 'http://foo.com/app/features');
httpHandler.respond(200, {foo: true, bar: false});
})); }));
afterEach(function () { afterEach(function () {
...@@ -43,25 +39,32 @@ describe('h:features', function () { ...@@ -43,25 +39,32 @@ describe('h:features', function () {
sandbox.restore(); sandbox.restore();
}); });
function defaultHandler() {
var handler = $httpBackend.expect('GET', 'http://foo.com/app/features');
handler.respond(200, {foo: true, bar: false});
return handler;
}
it('fetch should retrieve features data', function () { it('fetch should retrieve features data', function () {
$httpBackend.expect('GET', 'http://foo.com/app/features'); defaultHandler();
features.fetch(); features.fetch();
$httpBackend.flush(); $httpBackend.flush();
}); });
it('fetch should not explode for errors fetching features data', function () { it('fetch should not explode for errors fetching features data', function () {
httpHandler.respond(500, "ASPLODE!"); defaultHandler().respond(500, "ASPLODE!");
features.fetch(); features.fetch();
$httpBackend.flush(); $httpBackend.flush();
}); });
it('flagEnabled should retrieve features data', function () { it('flagEnabled should retrieve features data', function () {
$httpBackend.expect('GET', 'http://foo.com/app/features'); defaultHandler();
features.flagEnabled('foo'); features.flagEnabled('foo');
$httpBackend.flush(); $httpBackend.flush();
}); });
it('flagEnabled should return false initially', function () { it('flagEnabled should return false initially', function () {
defaultHandler();
var result = features.flagEnabled('foo'); var result = features.flagEnabled('foo');
$httpBackend.flush(); $httpBackend.flush();
...@@ -69,6 +72,7 @@ describe('h:features', function () { ...@@ -69,6 +72,7 @@ describe('h:features', function () {
}); });
it('flagEnabled should return flag values when data is loaded', function () { it('flagEnabled should return flag values when data is loaded', function () {
defaultHandler();
features.fetch(); features.fetch();
$httpBackend.flush(); $httpBackend.flush();
...@@ -80,6 +84,7 @@ describe('h:features', function () { ...@@ -80,6 +84,7 @@ describe('h:features', function () {
}); });
it('flagEnabled should return false for unknown flags', function () { it('flagEnabled should return false for unknown flags', function () {
defaultHandler();
features.fetch(); features.fetch();
$httpBackend.flush(); $httpBackend.flush();
...@@ -90,13 +95,13 @@ describe('h:features', function () { ...@@ -90,13 +95,13 @@ describe('h:features', function () {
it('flagEnabled should trigger a new fetch after cache expiry', function () { it('flagEnabled should trigger a new fetch after cache expiry', function () {
var clock = sandbox.useFakeTimers(); var clock = sandbox.useFakeTimers();
$httpBackend.expect('GET', 'http://foo.com/app/features'); defaultHandler();
features.flagEnabled('foo'); features.flagEnabled('foo');
$httpBackend.flush(); $httpBackend.flush();
clock.tick(301 * 1000); clock.tick(301 * 1000);
$httpBackend.expect('GET', 'http://foo.com/app/features'); defaultHandler();
features.flagEnabled('foo'); features.flagEnabled('foo');
$httpBackend.flush(); $httpBackend.flush();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment