Socket
Socket
Sign inDemoInstall

ts-mockito

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-mockito - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

lib/capture/ArgCaptor.d.ts

14

lib/MethodStubCollection.d.ts

@@ -1,11 +0,11 @@

import { MethodStub } from './stub/MethodStub';
import { MethodStub } from "./stub/MethodStub";
export declare class MethodStubCollection {
private hadMoreThanOneBehavior;
private items;
add(item: MethodStub): void;
getFirstMatchingAndRemove(args: any): MethodStub;
getFirstMatching(args: any): MethodStub;
getHadMoreThanOneBehavior(): boolean;
hasMatching(args: any): boolean;
private getFirstMatchingIndex(args);
getLastMatchingGroupIndex(args: any): number;
getFirstMatchingFromGroupAndRemoveIfNotLast(groupIndex: number, args: any[]): MethodStub;
private getFirstMatchingFromGroup(groupIndex, args);
hasMatchingInAnyGroup(args: any[]): boolean;
private getFirstMatchingIndexFromGroup(groupIndex, args);
private getItemsCountInGroup(groupIndex);
}

@@ -5,3 +5,2 @@ "use strict";

function MethodStubCollection() {
this.hadMoreThanOneBehavior = false;
this.items = [];

@@ -11,10 +10,16 @@ }

this.items.push(item);
if (this.items.length > 1) {
this.hadMoreThanOneBehavior = true;
};
MethodStubCollection.prototype.getLastMatchingGroupIndex = function (args) {
for (var i = this.items.length - 1; i >= 0; i--) {
var item = this.items[i];
if (item.isApplicable(args)) {
return item.getGroupIndex();
}
}
return -1;
};
MethodStubCollection.prototype.getFirstMatchingAndRemove = function (args) {
var index = this.getFirstMatchingIndex(args);
var result = this.getFirstMatching(args);
if (index > -1) {
MethodStubCollection.prototype.getFirstMatchingFromGroupAndRemoveIfNotLast = function (groupIndex, args) {
var index = this.getFirstMatchingIndexFromGroup(groupIndex, args);
var result = this.getFirstMatchingFromGroup(groupIndex, args);
if (index > -1 && this.getItemsCountInGroup(groupIndex) > 1) {
this.items.splice(index, 1);

@@ -24,6 +29,6 @@ }

};
MethodStubCollection.prototype.getFirstMatching = function (args) {
MethodStubCollection.prototype.getFirstMatchingFromGroup = function (groupIndex, args) {
for (var _i = 0, _a = this.items; _i < _a.length; _i++) {
var item = _a[_i];
if (item.isApplicable(args)) {
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) {
return item;

@@ -34,13 +39,16 @@ }

};
MethodStubCollection.prototype.getHadMoreThanOneBehavior = function () {
return this.hadMoreThanOneBehavior;
MethodStubCollection.prototype.hasMatchingInAnyGroup = function (args) {
for (var _i = 0, _a = this.items; _i < _a.length; _i++) {
var item = _a[_i];
if (item.isApplicable(args)) {
return true;
}
}
return false;
};
MethodStubCollection.prototype.hasMatching = function (args) {
return this.getFirstMatchingIndex(args) > -1;
};
MethodStubCollection.prototype.getFirstMatchingIndex = function (args) {
MethodStubCollection.prototype.getFirstMatchingIndexFromGroup = function (groupIndex, args) {
var index = 0;
for (var _i = 0, _a = this.items; _i < _a.length; _i++) {
var item = _a[_i];
if (item.isApplicable(args)) {
if (item.getGroupIndex() === groupIndex && item.isApplicable(args)) {
return index;

@@ -52,2 +60,12 @@ }

};
MethodStubCollection.prototype.getItemsCountInGroup = function (groupIndex) {
var result = 0;
for (var _i = 0, _a = this.items; _i < _a.length; _i++) {
var item = _a[_i];
if (item.getGroupIndex() === groupIndex) {
result++;
}
}
return result;
};
return MethodStubCollection;

@@ -54,0 +72,0 @@ }());

@@ -1,10 +0,10 @@

import { MethodToStub } from './MethodToStub';
import { Captor } from './Captor';
import { MethodToStub } from "./MethodToStub";
export declare class MethodStubSetter<T> {
private methodToStub;
private static globalGroupIndex;
private groupIndex;
constructor(methodToStub: MethodToStub);
thenReturn(value: T): void;
throwsError(error: Error): void;
thenCall(func: (...args: any[]) => any): void;
thenCapture(...args: Captor<any>[]): void;
thenReturn(...rest: T[]): this;
thenThrow(...rest: Error[]): this;
thenCall(func: (...args: any[]) => any): this;
}

@@ -6,26 +6,37 @@ "use strict";

var CallFunctionMethodStub_1 = require("./stub/CallFunctionMethodStub");
var CaptorMethodStub_1 = require("./stub/CaptorMethodStub");
var MethodStubSetter = (function () {
function MethodStubSetter(methodToStub) {
this.methodToStub = methodToStub;
this.groupIndex = ++MethodStubSetter.globalGroupIndex;
}
MethodStubSetter.prototype.thenReturn = function (value) {
this.methodToStub.methodStubCollection.add(new ReturnValueMethodStub_1.ReturnValueMethodStub(this.methodToStub.matchers, value));
MethodStubSetter.prototype.thenReturn = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
for (var _a = 0, rest_1 = rest; _a < rest_1.length; _a++) {
var value = rest_1[_a];
this.methodToStub.methodStubCollection.add(new ReturnValueMethodStub_1.ReturnValueMethodStub(this.groupIndex, this.methodToStub.matchers, value));
}
return this;
};
MethodStubSetter.prototype.throwsError = function (error) {
this.methodToStub.methodStubCollection.add(new ThrowErrorMethodStub_1.ThrowErrorMethodStub(this.methodToStub.matchers, error));
MethodStubSetter.prototype.thenThrow = function () {
var rest = [];
for (var _i = 0; _i < arguments.length; _i++) {
rest[_i] = arguments[_i];
}
for (var _a = 0, rest_2 = rest; _a < rest_2.length; _a++) {
var error = rest_2[_a];
this.methodToStub.methodStubCollection.add(new ThrowErrorMethodStub_1.ThrowErrorMethodStub(this.groupIndex, this.methodToStub.matchers, error));
}
return this;
};
MethodStubSetter.prototype.thenCall = function (func) {
this.methodToStub.methodStubCollection.add(new CallFunctionMethodStub_1.CallFunctionMethodStub(this.methodToStub.matchers, func));
this.methodToStub.methodStubCollection.add(new CallFunctionMethodStub_1.CallFunctionMethodStub(this.groupIndex, this.methodToStub.matchers, func));
return this;
};
MethodStubSetter.prototype.thenCapture = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this.methodToStub.methodStubCollection.add(new CaptorMethodStub_1.CaptorMethodStub(this.methodToStub.matchers, args));
};
return MethodStubSetter;
}());
MethodStubSetter.globalGroupIndex = 0;
exports.MethodStubSetter = MethodStubSetter;
//# sourceMappingURL=MethodStubSetter.js.map

@@ -34,2 +34,3 @@ import { Matcher } from "./matcher/type/Matcher";

private getMethodStub(key, args);
getActionsByName(name: string): MethodAction[];
}

@@ -230,14 +230,15 @@ "use strict";

if (!methodStub) {
return new ReturnValueMethodStub_1.ReturnValueMethodStub([], null);
return new ReturnValueMethodStub_1.ReturnValueMethodStub(-1, [], null);
}
else if (methodStub.getHadMoreThanOneBehavior() && methodStub.hasMatching(args)) {
return methodStub.getFirstMatchingAndRemove(args);
else if (methodStub.hasMatchingInAnyGroup(args)) {
var groupIndex = methodStub.getLastMatchingGroupIndex(args);
return methodStub.getFirstMatchingFromGroupAndRemoveIfNotLast(groupIndex, args);
}
else if (methodStub.hasMatching(args)) {
return methodStub.getFirstMatching(args);
}
else {
return new ReturnValueMethodStub_1.ReturnValueMethodStub([], null);
return new ReturnValueMethodStub_1.ReturnValueMethodStub(-1, [], null);
}
};
Mocker.prototype.getActionsByName = function (name) {
return this.methodActions.filter(function (action) { return action.methodName === name; });
};
return Mocker;

@@ -244,0 +245,0 @@ }());

@@ -1,4 +0,6 @@

import { Matcher } from '../matcher/type/Matcher';
import { MethodStub } from './MethodStub';
export declare class CallFunctionMethodStub implements MethodStub {
import { Matcher } from "../matcher/type/Matcher";
import { MethodStub } from "./MethodStub";
import { AbstractMethodStub } from "./AbstractMethodStub";
export declare class CallFunctionMethodStub extends AbstractMethodStub implements MethodStub {
protected groupIndex: number;
private matchers;

@@ -8,3 +10,3 @@ private func;

private functionResult;
constructor(matchers: Array<Matcher>, func: any);
constructor(groupIndex: number, matchers: Array<Matcher>, func: any);
isApplicable(args: any[]): boolean;

@@ -11,0 +13,0 @@ execute(args: any[]): void;

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ArgsToMatchersValidator_1 = require("../matcher/ArgsToMatchersValidator");
var CallFunctionMethodStub = (function () {
function CallFunctionMethodStub(matchers, func) {
this.matchers = matchers;
this.func = func;
this.validator = new ArgsToMatchersValidator_1.ArgsToMatchersValidator();
var AbstractMethodStub_1 = require("./AbstractMethodStub");
var CallFunctionMethodStub = (function (_super) {
__extends(CallFunctionMethodStub, _super);
function CallFunctionMethodStub(groupIndex, matchers, func) {
var _this = _super.call(this) || this;
_this.groupIndex = groupIndex;
_this.matchers = matchers;
_this.func = func;
_this.validator = new ArgsToMatchersValidator_1.ArgsToMatchersValidator();
return _this;
}

@@ -20,4 +35,4 @@ CallFunctionMethodStub.prototype.isApplicable = function (args) {

return CallFunctionMethodStub;
}());
}(AbstractMethodStub_1.AbstractMethodStub));
exports.CallFunctionMethodStub = CallFunctionMethodStub;
//# sourceMappingURL=CallFunctionMethodStub.js.map

@@ -5,2 +5,3 @@ export interface MethodStub {

getValue(): any;
getGroupIndex(): number;
}
import { Matcher } from '../matcher/type/Matcher';
import { MethodStub } from './MethodStub';
export declare class ReturnValueMethodStub implements MethodStub {
import { AbstractMethodStub } from "./AbstractMethodStub";
export declare class ReturnValueMethodStub extends AbstractMethodStub implements MethodStub {
protected groupIndex: number;
private matchers;
private returns;
private validator;
constructor(matchers: Array<Matcher>, returns: any);
constructor(groupIndex: number, matchers: Array<Matcher>, returns: any);
isApplicable(args: any[]): boolean;

@@ -9,0 +11,0 @@ execute(args: any[]): void;

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ArgsToMatchersValidator_1 = require("../matcher/ArgsToMatchersValidator");
var ReturnValueMethodStub = (function () {
function ReturnValueMethodStub(matchers, returns) {
this.matchers = matchers;
this.returns = returns;
this.validator = new ArgsToMatchersValidator_1.ArgsToMatchersValidator();
var AbstractMethodStub_1 = require("./AbstractMethodStub");
var ReturnValueMethodStub = (function (_super) {
__extends(ReturnValueMethodStub, _super);
function ReturnValueMethodStub(groupIndex, matchers, returns) {
var _this = _super.call(this) || this;
_this.groupIndex = groupIndex;
_this.matchers = matchers;
_this.returns = returns;
_this.validator = new ArgsToMatchersValidator_1.ArgsToMatchersValidator();
return _this;
}

@@ -19,4 +34,4 @@ ReturnValueMethodStub.prototype.isApplicable = function (args) {

return ReturnValueMethodStub;
}());
}(AbstractMethodStub_1.AbstractMethodStub));
exports.ReturnValueMethodStub = ReturnValueMethodStub;
//# sourceMappingURL=ReturnValueMethodStub.js.map
import { Matcher } from '../matcher/type/Matcher';
import { MethodStub } from './MethodStub';
export declare class ThrowErrorMethodStub implements MethodStub {
import { AbstractMethodStub } from "./AbstractMethodStub";
export declare class ThrowErrorMethodStub extends AbstractMethodStub implements MethodStub {
protected groupIndex: number;
private matchers;
private error;
private validator;
constructor(matchers: Array<Matcher>, error: Error);
constructor(groupIndex: number, matchers: Array<Matcher>, error: Error);
isApplicable(args: any[]): boolean;

@@ -9,0 +11,0 @@ execute(args: any[]): void;

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ArgsToMatchersValidator_1 = require("../matcher/ArgsToMatchersValidator");
var ThrowErrorMethodStub = (function () {
function ThrowErrorMethodStub(matchers, error) {
this.matchers = matchers;
this.error = error;
this.validator = new ArgsToMatchersValidator_1.ArgsToMatchersValidator();
var AbstractMethodStub_1 = require("./AbstractMethodStub");
var ThrowErrorMethodStub = (function (_super) {
__extends(ThrowErrorMethodStub, _super);
function ThrowErrorMethodStub(groupIndex, matchers, error) {
var _this = _super.call(this) || this;
_this.groupIndex = groupIndex;
_this.matchers = matchers;
_this.error = error;
_this.validator = new ArgsToMatchersValidator_1.ArgsToMatchersValidator();
return _this;
}

@@ -20,4 +35,4 @@ ThrowErrorMethodStub.prototype.isApplicable = function (args) {

return ThrowErrorMethodStub;
}());
}(AbstractMethodStub_1.AbstractMethodStub));
exports.ThrowErrorMethodStub = ThrowErrorMethodStub;
//# sourceMappingURL=ThrowErrorMethodStub.js.map

@@ -0,5 +1,5 @@

import { ArgCaptor1, ArgCaptor10, ArgCaptor2, ArgCaptor3, ArgCaptor4, ArgCaptor5, ArgCaptor6, ArgCaptor7, ArgCaptor8, ArgCaptor9 } from "./capture/ArgCaptor";
import { Matcher } from "./matcher/type/Matcher";
import { MethodStubSetter } from "./MethodStubSetter";
import { MethodStubVerificator } from "./MethodStubVerificator";
import { MethodStubSetter } from "./MethodStubSetter";
import { Matcher } from "./matcher/type/Matcher";
export { Captor } from "./Captor";
export declare function mock<T>(clazz: {

@@ -11,2 +11,12 @@ new (...args: any[]): T;

export declare function instance<T>(mock: T): T;
export declare function capture<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7, i: T8, j: T9) => any): ArgCaptor10<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>;
export declare function capture<T0, T1, T2, T3, T4, T5, T6, T7, T8>(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7, i: T8) => any): ArgCaptor9<T0, T1, T2, T3, T4, T5, T6, T7, T8>;
export declare function capture<T0, T1, T2, T3, T4, T5, T6, T7>(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7) => any): ArgCaptor8<T0, T1, T2, T3, T4, T5, T6, T7>;
export declare function capture<T0, T1, T2, T3, T4, T5, T6>(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6) => any): ArgCaptor7<T0, T1, T2, T3, T4, T5, T6>;
export declare function capture<T0, T1, T2, T3, T4, T5>(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5) => any): ArgCaptor6<T0, T1, T2, T3, T4, T5>;
export declare function capture<T0, T1, T2, T3, T4>(method: (a: T0, b: T1, c: T2, d: T3, e: T4) => any): ArgCaptor5<T0, T1, T2, T3, T4>;
export declare function capture<T0, T1, T2, T3>(method: (a: T0, b: T1, c: T2, d: T3) => any): ArgCaptor4<T0, T1, T2, T3>;
export declare function capture<T0, T1, T2>(method: (a: T0, b: T1, c: T2) => any): ArgCaptor3<T0, T1, T2>;
export declare function capture<T0, T1>(method: (a: T0, b: T1) => any): ArgCaptor2<T0, T1>;
export declare function capture<T0>(method: (a: T0) => any): ArgCaptor1<T0>;
export declare function reset<T>(mock: T): void;

@@ -13,0 +23,0 @@ export declare function resetCalls<T>(mock: T): void;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Mock_1 = require("./Mock");
var MethodStubVerificator_1 = require("./MethodStubVerificator");
var MethodStubSetter_1 = require("./MethodStubSetter");
var ArgCaptor_1 = require("./capture/ArgCaptor");
var AnyNumberMatcher_1 = require("./matcher/type/AnyNumberMatcher");

@@ -13,4 +11,6 @@ var AnyStringMatcher_1 = require("./matcher/type/AnyStringMatcher");

var StrictEqualMatcher_1 = require("./matcher/type/StrictEqualMatcher");
var Captor_1 = require("./Captor");
exports.Captor = Captor_1.Captor;
var MethodStubSetter_1 = require("./MethodStubSetter");
var MethodStubVerificator_1 = require("./MethodStubVerificator");
var MethodToStub_1 = require("./MethodToStub");
var Mock_1 = require("./Mock");
function mock(clazz) {

@@ -32,2 +32,13 @@ return new Mock_1.Mocker(clazz).getMock();

exports.instance = instance;
function capture(method) {
var methodStub = method();
if (methodStub instanceof MethodToStub_1.MethodToStub) {
var actions = methodStub.mocker.getActionsByName(methodStub.name);
return new ArgCaptor_1.ArgCaptor(actions);
}
else {
throw Error("Cannot capture from not mocked object.");
}
}
exports.capture = capture;
function reset(mock) {

@@ -34,0 +45,0 @@ mock.__tsmockitoMocker.reset();

{
"name": "ts-mockito",
"version": "1.2.0",
"description": "",
"version": "2.0.0",
"description": "Mocking library for TypeScript",
"main": "lib/ts-mockito.js",

@@ -18,2 +18,11 @@ "typings": "lib/ts-mockito",

},
"keywords": [
"mock",
"typescript",
"tests",
"fake",
"stub",
"spy",
"javascript"
],
"devDependencies": {

@@ -23,19 +32,15 @@ "@types/jasmine": "^2.2.33",

"@types/node": "^6.0.38",
"awesome-typescript-loader": "^3.0.0-beta.17",
"istanbul-instrumenter-loader": "^0.2.0",
"awesome-typescript-loader": "^3.1.2",
"jasmine-core": "^2.4.1",
"json-loader": "^0.5.4",
"karma": "^1.2.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-phantomjs2-launcher": "^0.5.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-source-map-support": "^1.2.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.8.0",
"karma-webpack": "^2.0.3",
"sourcemap": "^0.1.0",
"ts-helpers": "^1.1.1",
"ts-loader": "^0.8.2",
"typescript": "^2.1.0",
"webpack": "^1.13.2",
"webpack-merge": "^0.14.1"
"typescript": "^2.2.2",
"webpack": "^2.3.2"
},

@@ -42,0 +47,0 @@ "dependencies": {

@@ -5,2 +5,5 @@ # ts-mockito [![build badge](https://travis-ci.org/NagRock/ts-mockito.svg?branch=master)](https://travis-ci.org/NagRock/ts-mockito)

## 1.x to 2.x migration guide
[1.x to 2.x migration guide](https://github.com/NagRock/ts-mockito/wiki/ts-mockito-1.x-to-2.x-migration-guide)
## Main features

@@ -21,3 +24,3 @@

* Resetting mock (`reset`, `resetCalls`)
* Capturing arguments passed to method (`thenCapture`)
* Capturing arguments passed to method (`capture`)
* Recording multiple behaviors

@@ -215,55 +218,37 @@ * Readable error messages (ex. 'Expected "convertNumberToString(strictEqual(3))" to be called 2 time(s). But has been called 1 time(s).')

// Create captor for all arguments
let firstArgCaptor: Captor<number> = new Captor<number>();
let secondArgCaptor: Captor<number> = new Captor<number>();
// Set matcher with anything() to capture all calls
when(mockedFoo.sumTwoNumbers(anything(), anything())).thenCapture(firstArgCaptor, secondArgCaptor);
// Call method twice with different values
// Call method
foo.sumTwoNumbers(1, 2);
foo.sumTwoNumbers(3, 4);
// Check first arg captor values
console.log(firstArgCaptor.getFirstCallValue()); // prints 1
console.log(firstArgCaptor.getLastCallValue()); // prints 3
// Check second arg captor values
console.log(secondArgCaptor.getFirstCallValue()); // prints 2
console.log(secondArgCaptor.getLastCallValue()); // prints 4
const [firstArg, secondArg] = capture(mockedFoo.sumTwoNumbers).last();
console.log(firstArg); // prints 1
console.log(secondArg); // prints 2
```
You can also capture single arg and give matcher to the other
You can also get other calls using `first()`, `second()`, `byCallIndex(3)` and more...
``` typescript
let mockedFoo:Foo = mock(Foo);
let foo:Foo = instance(mockedFoo);
### Recording multiple behaviors
// Create captor for second argument
let secondArgCaptor: Captor<number> = new Captor<number>();
You can set multiple returning values for same matching values
// Set matcher equal matcher (number === 3) for first arg and anything() for second
when(mockedFoo.sumTwoNumbers(3, anything())).thenCapture(new Captor(), secondArgCaptor);
``` typescript
const mockedFoo:Foo = mock(Foo);
// Call method twice with different values
foo.sumTwoNumbers(1, 2); // this call will not be captured becasue first arg !== 3
foo.sumTwoNumbers(3, 4);
when(mockedFoo.getBar(anyNumber())).thenReturn('one').thenReturn('two').thenReturn('three');
// Check second arg captor values
console.log(secondArgCaptor.getFirstCallValue()); // prints 4
console.log(secondArgCaptor.getLastCallValue()); // prints 4
const foo:Foo = instance(mockedFoo);
// As you can see first and last call values are same, because only second call has been captured
console.log(foo.getBar(1)); // one
console.log(foo.getBar(1)); // two
console.log(foo.getBar(1)); // three
console.log(foo.getBar(1)); // three - last defined behavior will be repeated infinitely
```
### Recording multiple behaviors
Another example with specific values
If more than one behavior is set, first matching is executed and removed
``` typescript
let mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(anyNumber()).thenReturn('two');
when(mockedFoo.getBar(anyNumber())).thenReturn('three');
when(mockedFoo.getBar(1)).thenReturn('one').thenReturn('another one');
when(mockedFoo.getBar(2)).thenReturn('two');

@@ -273,24 +258,37 @@ let foo:Foo = instance(mockedFoo);

console.log(foo.getBar(1)); // one
console.log(foo.getBar(2)); // two
console.log(foo.getBar(1)); // another one
console.log(foo.getBar(1)); // another one - this is last defined behavior for arg '1' so it will be repeated
console.log(foo.getBar(2)); // two
console.log(foo.getBar(2)); // two - this is last defined behavior for arg '2' so it will be repeated
```
Short notation:
``` typescript
const mockedFoo:Foo = mock(Foo);
// You can specify return values as multiple thenReturn args
when(mockedFoo.getBar(anyNumber())).thenReturn('one', 'two', 'three');
const foo:Foo = instance(mockedFoo);
console.log(foo.getBar(1)); // one
console.log(foo.getBar(1)); // two
console.log(foo.getBar(1)); // three
console.log(foo.getBar(1)); // null - no more behaviors defined
console.log(foo.getBar(1)); // three - last defined behavior will be repeated infinity
```
Another example with specific values
Possible errors:
``` typescript
let mockedFoo:Foo = mock(Foo);
const mockedFoo:Foo = mock(Foo);
when(mockedFoo.getBar(1)).thenReturn('one');
when(mockedFoo.getBar(1)).thenReturn('second time one');
when(mockedFoo.getBar(2)).thenReturn('two');
// When multiple matchers, matches same result:
when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(3)).thenReturn('one');
let foo:Foo = instance(mockedFoo);
const foo:Foo = instance(mockedFoo);
foo.getBar(3); // MultipleMatchersMatchSameStubError will be thrown, two matchers match same method call
console.log(foo.getBar(1)); // one
console.log(foo.getBar(1)); // second time one
console.log(foo.getBar(1)); // null - no more behaviors for arg === 1 defined
console.log(foo.getBar(2)); // two
console.log(foo.getBar(2)); // null - no more behaviors for arg === 2 defined
```

@@ -303,2 +301,3 @@

* Clickmeeting (http://clickmeeting.com)
* MichaƂ Stocki (https://github.com/michalstocki)
* Thomas Hilzendegen (https://github.com/thomashilzendegen)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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