1.0.1 • Published 7 years ago

emock v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

emock

Build Status

emock automatically generates mocks from your classes. The entire public API will be mocked. It is especially designed to use in conjunction with ES6 classes (but not exclusive). Internally it uses expect spies. It has full TypeScript support and was developed for it!

Install

npm i emock --save-dev

Usage

It is as simple as:

import { Mock, expectExtensions } from 'emock';
import expect, { extend } from 'expect';

extend(expectExtensions);

class MyService {
  echo (s: string): string {
    return s;
  }
}

describe('Usage', () => {
  it('should show me usage', () => {
    let m: Mock<MyService> = Mock.of(MyService);

    let service: MyService = m.mock;

    console.log(service.echo('name')); // -> undefined

    // lets add some spy logic
    m.spyOn(x => x.echo('Foo')).andReturn('Bar'); // note: x is from type MyService so will get full code completion! :)

    console.log(service.echo('name')); // -> Bar

    // Was echo called?
    expect(service.echo).toHaveBeenCalled();

    // Was echo called with 'name'?
    expect(service.echo).toHaveBeenCalledWith('name');

    // Why I have added 'Foo' to the spyOn call? Because we could do the following:
    (<any>expect(service.echo)).toHaveBeenCalledWithSignature();

    // the last expect will fail, but why? We have recorded a call signature with m.spyOn(x => x.echo('Foo'))
    // That means, if echo is called it should be called with one parameter 'Foo'
    // toHaveBeenCalledWithSignature() verifies that for us
  });
});

See tests for more examples. :)

Matchers

Like I said before m.spyOn(x => x.echo('Foo')); records a call signature, but you don't have to use explicit values like in the example above. You can use some matchers from the It package. For example:

m.spyOn(x => x.echo(It.isString()));

m.mock.echo('test');
(<any>expect(m.mock.echo)).toHaveBeenCalledWithSignature(); // passes

m.mock.echo('test2');
(<any>expect(m.mock.echo)).toHaveBeenCalledWithSignature(); // passes

m.mock.echo(<any>5);
(<any>expect(m.mock.echo)).toHaveBeenCalledWithSignature(); // fails with 5 is not a string

Dependencies

emock itself has no dependencies, but some peerDependencies.

You will need some polyfills for your environment if there is no support for used features:

  • Reflect API with metadata support
  • Symbol polyfill (comes with babel-polyfill for example)
1.0.1

7 years ago

1.0.0

7 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago