Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-factory

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-factory

A Jasmine helper for creating mocked classes

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
42
decreased by-60%
Maintainers
1
Weekly downloads
 
Created
Source

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 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 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

Running unit tests

Run ng test to execute the unit tests via Karma.

Keywords

FAQs

Package last updated on 16 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc