Comments (2)
You can create custom container for each test case.
const IoC = require('electrolyte');
describe('myModule', function() {
let container;
beforeEach(function() {
container = new IoC.Container();
container.use(IoC.dir('app/myContainerFolder'));
myModule = container.create('myModule');
});
});
... and override modules with mocks like
container.use(function(id) {
if (id === 'myDep') {
myDepMock['@literal'] = true;
return myDepMock;
}
});
...or create something like this...
from electrolyte.
IoC.Container().use
uses a hierarchy – the last .use(...)
is the first one to be checked for an object. So while using a different container for each test is one option, adding extra .use(...)
s during tests is another.
Assuming you have src/models
which stores your UserModel
, ExampleModel
, etc., you can have test/mocks
include mocks for ExampleModel
; or if you need more refined control test/mocks/ExampleModel
will be a directory containing only the ExampleModel mock.
This allows you to use the following in your main application:
const IoC = require('electrolyte');
const container = IoC.Container();
container.use(IoC.node_modules());
container.use(IoC.dir('src/models'));
container.use(IoC.dir('src/controllers'));
container.use(IoC.dir('src/routes'));
// ...
And then in your test set-up, you can either manually instantiate individual models with mocks (semi-ugly):
// IoC code above...
const ExampleController = require('src/controllers/ExampleModel');
const ExampleModelMock = require('test/mocks/ExampleModel');
let controller = new ExampleController(new ExampleModelMock(), IoC.create('UserModel'));
Or, add an extra .use(...)
so that a mock called ExampleModel is resolved:
// IoC code above...
container.use(IoC.dir('test/mocks')); // mocks must be named exactly the same as the original object
let controller = IoC.create('ExampleController');
Feature Request:
Although I would like to see some method that allows hot-swapping of different mocks. Perhaps something similar to
// in your tests bootstrap file:
container.use(IoC.dir('test/mocks')); // mocks suffixed with 'Mock' such as 'ExampleModelMock'
// in a particular test:
let controller = IoC.create('ExampleController', {
// override the `ExampleModel` with a mock
'ExampleModel': IoC.create('ExampleModelMock')
});
from electrolyte.
Related Issues (20)
- Implicit factory resolution HOT 2
- change default as singleton = true HOT 1
- change default as singleton = true HOT 3
- Better error handling on requiring component HOT 2
- Babel transpile with babel-preset-es2015 + async function breaks Electrolyte
- Electrolyte 0.4.0 should reject promise if a dependency is not found HOT 1
- Feature: Override existing Modules
- Release 0.4.0 - IoC.create now returns promise, createAsync doesn't exist HOT 4
- Failure to uphold semver expectations HOT 3
- version 0.3.0 returns objects while 0.4.0 returns promises HOT 11
- Child container
- support for es6 module syntax HOT 5
- Run example in repository : .get() requires callback functions but got a [object Object] Error: .get() requires callback functions but got a [object Object] HOT 5
- Unable to create object "XXXX" required by: unknown HOT 1
- Using it with class HOT 2
- Webpack? HOT 1
- Fix insecure dependencies
- Does IoC.use have to return a function? HOT 1
- Using custom resolvers to substitute components for testing and other purposes
- Is this repo being actively maintained HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from electrolyte.