Jasmine Mock Factory
A Jasmine test util that uses a TypeScript class or an instance of a class to create a mock instance of that class.
Quick Start
import { SomeClass } from 'some-library';
import { MockFactory} from 'jasmine-mock-factory';
it('should pass', () => {
const mockInstance = MockFactory.create(SomeClass);
mockInstance.doSomething.and.returnValue('awesome!');
mockInstance.doSomething();
expect(mockInstance.doSomething).toHaveBeenCalled();
}
Prerequisite
This util is built with and for Jasmine test framework. Basic understanding of Jasmine is assumed.
This util requires ES6 Proxy and only contains un-compiled *.ts
files which must be compiled with a TypeScript compiler.
Usage
Install
npm install jasmine-mock-factory --save-dev
Import
Import the library with ES6 Module Syntax:
import { MockFactory } from 'jasmine-mock-factory'
Creating a mock
From a TypeScript class
class RealClass {
}
...
const mockInstance = MockFactory.create(RealClass);
From an instance of a class
const realInstance: RealInterface = new RealClass();
...
const mockInstance = MockFactory.create(realInstance);
Using a mock
MockFactory.create()
will return an object with the same interface as the original object. You can invoke methods and get/set properties on this object.
- All the public and private methods will have a jasmine.Spy as the initial value. The Spy cannot be overwritten.
- All the public and private properties will have
undefined
as the initial value. The value can be overwritten with anything.
Examples
class RealClass {
public doSomething(...arg: any[]) { ... }
public someProperty = 'whatever';
}
const mockInstance = MockFactory.create(RealClass);
expect(mockInstance.someProperty).toBeUndefined();
mockInstance.someProperty = 'hello';
expect(mockInstance.someProperty).toBe('hello');
expect(mockInstance.doSomething).not.toHaveBeenCalled();
(mockInstance.doSomething as jasmine.Spy).and.returnValue('awesome!');
expect(mockInstance.doSomething(42)).toBe('awesome!');
expect(mockInstance.doSomething).toHaveBeenCalledWith(42);
Develope
This project is built with Angular CLI
Running unit tests
Run ng test
to execute the unit tests via Karma.