# mock-factory

> A Jasmine helper for creating mocked classes

Latest version **1.0.3** (published 2017-06-16) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install mock-factory
pnpm add mock-factory
yarn add mock-factory
bun add mock-factory
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2017-06-16 |
| First published | 2017-06-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Chuanqi Sun |
| Maintainers | henrysun918 |
| Keywords | mock, jasmine, mocking, test, mocking, testing |

## Links

- npm: https://www.npmjs.com/package/mock-factory
- Repository: https://github.com/henrysun918/mock-factory
- Homepage: https://github.com/henrysun918/mock-factory#readme
- Issues: https://github.com/henrysun918/mock-factory/issues
- npm.io page: https://npm.io/package/mock-factory

## Dependencies (1)

- [@types/jasmine](https://npm.io/package/@types/jasmine.md) 2.5.45

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2017-06-16
- 1.0.2 — 2017-06-16
- 1.0.1 — 2017-06-16
- 1.0.0 — 2017-06-16
- 0.0.1 — 2017-06-15

## README

# MockFactory

A Jasmine test util that uses a TyoeScript class or an instance of a class to create a mock instance of that class.

## Prerequisite

This util is built with and for [Jasmine](https://jasmine.github.io/) test framework. Basic understanding of Jasmine is assumed.

This util requires [ES6 Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) and only contains un-compiled `*.ts` files which must be compiled with a [TypeScript](https://www.typescriptlang.org/) compiler.



## Usage
### Install
`npm install mock-factory --save-dev`

### Import
Import the library with ES6 Module Syntax:
```
import { MockFactory } from 'mock-factory'
```

### Creating a mock

#### From a TypeScript class
```
class RealClass {
  // This is a typescript class
}

...

const mockInstance = MockFactory.create(RealClass);
```

#### From an instance of a class
```
const realInstance: RealInterface;

...

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);

// get, set property
expect(mockInstance.someProperty).toBeUndefined();
mockInstance.someProperty = 'hello';
expect(mockInstance.someProperty).toBe('hello');

// use function spy
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](https://cli.angular.io/)

### Running unit tests
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).

---
_Source: https://npm.io/package/mock-factory · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
