bogus is a small utility for stubbing dependencies when testing RequireJS based projects
You can install bogus into your project using either npm
or bower
$ npm install bogus --save-dev
or
$ bower install bogus --save-dev
// SteeringWheel
define('SteeringWheel', function(){
function SteeringWheel(){
this.color = 'black';
}
return SteeringWheel;
});
// Car
define('Car', ['SteeringWheel'], function(SteeringWheel){
function Car(){
this.steeringWheel = new SteeringWheel();
}
Car.prototype.getSteeringWheelColor = function getSteeringWheelColor(){
return this.steeringWheel.color;
}
return Car;
});
// load bogus
define([
'bower_components/bogus/bogus' // this is ofc. dependent on where you installed it
], function(
bogus
){
describe('myModule', function{
var Car;
beforeEach(function(done){
var fakeSteeringWheel = function(){
this.color = 'blue';
};
// stub out a dependency (SteeringWheel) with our fake
bogus.stub('SteeringWheel', fakeSteeringWheel, function(){
// load Car module, that depends on SteeringWheel
bogus.requireWithStubs('Car', function(module){
Car = module;
done();
});
});
});
afterEach(function(done){
bogus.reset(done);
})
describe('Car', function(){
describe('getSteeringWheelColor method', function(){
it('should return the actual color of the SteeringWheel', function(){
var car = new Car();
assert.equal(car.getSteeringWheelColor(), 'blue');
});
});
});
});
});
You can run the tests with
$ npm test
or with
$ mocha
if you have mocha installed as a global
- Injecting Stubs/Mocks into Tests with Require.js - the blog post that laid the groundwork for what became bogus
- Squire.js - also stubs out RequireJS dependencies. I couldn't get it to stop re-downloading all the dependencies for each test ... nor could someone else