https://github.com/ovrmrw/inversify-mock-example
Mocking your class using DI based on InversifyJS
https://github.com/ovrmrw/inversify-mock-example
inversifyjs
Last synced: over 1 year ago
JSON representation
Mocking your class using DI based on InversifyJS
- Host: GitHub
- URL: https://github.com/ovrmrw/inversify-mock-example
- Owner: ovrmrw
- License: mit
- Created: 2017-02-01T02:38:48.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-08T09:24:33.000Z (over 9 years ago)
- Last Synced: 2025-01-04T06:27:05.380Z (over 1 year ago)
- Topics: inversifyjs
- Language: TypeScript
- Size: 30.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# inversify-mock-example
Mocking your class using DI based on InversifyJS
---
```js
@injectable()
class Ninja {
constructor(
private katana: Katana,
private shuriken: Shuriken,
) { }
fight() {
return this.katana.hit()
}
sneak() {
return this.shuriken.throw()
}
}
@injectable()
class MockKatana implements Katana {
hit() {
return 'cut! (mock)'
}
}
const container = rootContainer.createChild()
container.bind(Ninja).toSelf()
/*
If the line below are removed, Katana class will be bind to Ninja class.
So in that case, "cut!" will be displayed in console.
*/
container.bind(Katana).to(MockKatana)
const ninja = container.get(Ninja)
console.log(ninja.fight()) // output: "cut! (mock)"
console.log(ninja.sneak()) // output: "hit!"
```
---
## Setup
```
$ yarn install
or
$ npm install
```
## Run (ts files directly)
```
$ npm start
or
$ npm run ts index
```
## Build JS files
```
$ npm run build
```