https://github.com/cmstead/dject-dynamic-double-loader
A simple test double builder for Dject factory-style dependencies
https://github.com/cmstead/dject-dynamic-double-loader
Last synced: about 1 year ago
JSON representation
A simple test double builder for Dject factory-style dependencies
- Host: GitHub
- URL: https://github.com/cmstead/dject-dynamic-double-loader
- Owner: cmstead
- License: mpl-2.0
- Created: 2019-02-13T22:05:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-13T22:07:23.000Z (over 7 years ago)
- Last Synced: 2025-06-14T05:04:52.846Z (about 1 year ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Dject Dynamic Double Loader #
A simple tool for building test doubles. The Dject Dynamic Double Loader allows you to create injectable dependencies which are easy to consume and easy to use in your tests. It's actually harder to write about how this works than to just demonstrate it!
## Installation ##
In your project run this:
`npm i dject-dynamic-double-loader --save-dev`
Done!
## Using it ##
### Writing a Test Double ###
If you use Dject to load your test doubles, the loader works a little like this:
(This example was pulled from a real test double! Sinon usage not required, but it sure makes testing easier!)
```javascript
function textFileServiceFake(
djectDynamicDoubleLoader,
sinon
) {
'use strict';
// Yes, this is really all you need to create a fully injectible test double!
return djectDynamicDoubleLoader
.buildDjectTestDouble('textFileService')
.appendProperty('readJsonFile', sinon.stub())
.appendProperty('writeTextFile', sinon.stub());
}
module.exports = textFileServiceFake;
```
If you don't want to do it this way, here's what you can do instead:
```javascript
const djectDynamicDoubleLoader = require('dject-dynamic-double-loader');
const sinon = require('sinon');
module.exports = djectDynamicDoubleLoader
.buildDjectTestDouble('textFileService')
.appendProperty('readJsonFile', sinon.stub())
.appendProperty('writeTextFile', sinon.stub());
```
Seriously, I couldn't make this easier.
### Using your Test Double ###
Once you are down to writing your test, it just looks like this:
```javascript
const container = require('../container');
const testContainer = require('./testContainer');
describe('doing some testing', function () {
let importDIBuilder;
let textFileServiceFake;
beforeEach(function () {
const appContainer = container.new();
const someFakeData = testContainer.build('someFakeData');
// No, seriously, this is it! All the code lives in the test double file
textFileServiceFake = testContainer.build('textFileServiceFake');
textFileServiceFake.readJsonFile.callsFake(() => someFakeData);
appContainer.register(textFileServiceFake);
importDIBuilder = appContainer.build('importDIBuilder');
});
it('connects the dots, la la lala', function() {
// your test code goeth here.
});
});
```
## Version History ##
**v1.0.0**
Initial commit - Introduced core functionality:
- `djectDynamicDoubleLoader.buildDjectTestDouble(string)`
- `testDouble.appendProperty(string, any)`