UNPKG

typemoq

Version:

A simple mocking library for TypeScript

1,337 lines (904 loc) 103 kB
///<reference path="../../.tmp/src/typemoq.d.ts"/> import * as TypeMoq from "typemoq"; import * as _ from "lodash"; import { TypeMoqTests } from "./fixtures"; import { Utils } from "./Utils"; const Mock = TypeMoq.Mock; const MockBehavior = TypeMoq.MockBehavior; const It = TypeMoq.It; const Times = TypeMoq.Times; const ExpectedCallType = TypeMoq.ExpectedCallType; const MockException = TypeMoq.MockException; import * as chai from "chai"; const assert = chai.assert; const expect = chai.expect; const hasProxyES6 = (typeof Proxy != "undefined"); const noProxyES6Msg = "global 'Proxy' object not available"; const hasPromise = (typeof Promise != "undefined"); describe("Mock", () => { describe("ctor", () => { it("should create an instance using class as ctor parameter", () => { const mock: TypeMoq.IMock<TypeMoqTests.Bar> = Mock.ofType(TypeMoqTests.Bar); expect(mock.object).to.be.not.null; }); it("should create an instance using class as ctor parameter and allow interface cast", () => { const mock: TypeMoq.IMock<TypeMoqTests.IBar> = Mock.ofType(TypeMoqTests.Bar); expect(mock.object).to.be.not.null; }); it("should create an instance using interface as type variable and class as ctor parameter", () => { const mock: TypeMoq.IMock<TypeMoqTests.IBar> = Mock.ofType<TypeMoqTests.IBar>(TypeMoqTests.Bar); expect(mock.object).to.be.not.null; }); it("should create an instance using class as ctor parameter and ctor args", () => { const bar = new TypeMoqTests.Bar(); const mock: TypeMoq.IMock<TypeMoqTests.Foo> = Mock.ofType(TypeMoqTests.Foo, MockBehavior.Loose, undefined, bar); expect(mock.object).to.be.not.null; expect(mock.object.bar).to.be.not.null; }); it("should create an instance using a generic class as ctor parameter and ctor args", () => { const mock: TypeMoq.IMock<TypeMoqTests.GenericFoo<TypeMoqTests.Bar>> = Mock.ofType(TypeMoqTests.GenericFoo, MockBehavior.Loose, undefined, TypeMoqTests.Bar, 999); expect(mock.object).to.be.not.null; expect(mock.object.bar).to.be.not.null; expect(mock.object.numberValue).to.be.not.null; }); it("should create an instance from an existing object", () => { const bar = new TypeMoqTests.Bar(); const mock: TypeMoq.IMock<TypeMoqTests.Bar> = Mock.ofInstance(bar); expect(mock.object).to.be.not.null; }); it("should create an instance from a function object", () => { const mock1: TypeMoq.IMock<() => string> = Mock.ofInstance(TypeMoqTests.someFunc); const mock2: TypeMoq.IMock<(a: any, b: any, c: any) => string> = Mock.ofInstance(TypeMoqTests.someFuncWithArgs); expect(mock1.object).to.be.not.null; expect(mock2.object).to.be.not.null; }); describe("dynamic mock", () => { it("should create an instance using an interface as type variable", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock: TypeMoq.IMock<TypeMoqTests.IThing> = Mock.ofType<TypeMoqTests.IThing>(); expect(mock.object).to.be.not.null; expect(mock.object.getA("abc")).to.be.undefined; expect(mock.object.getB(123)).to.be.undefined; expect(mock.object.getC()).to.be.undefined; expect(mock.object.valueA).to.be.a("function"); } }); it("should create an instance using the 'instance' side of the class as type variable", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock: TypeMoq.IMock<TypeMoqTests.Greeter> = Mock.ofType<TypeMoqTests.Greeter>(); expect(mock.object).to.be.not.null; expect(mock.object.greet()).to.be.undefined; } }); it("should create an instance using the 'static' side of the class as type variable", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock: TypeMoq.IMock<typeof TypeMoqTests.Greeter> = Mock.ofType<typeof TypeMoqTests.Greeter>(); expect(mock.object).to.be.not.null; expect(mock.object.instance()).to.be.undefined; } }); it("should create an instance using a function as type variable", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<Function>(); expect(mock.object).to.be.not.null; } }); }); }); describe(".object", () => { it("should initialize proxy instance", () => { const mock: TypeMoq.IMock<TypeMoqTests.Bar> = Mock.ofType(TypeMoqTests.Bar); const bar: TypeMoqTests.Bar = mock.object; const bar2: TypeMoqTests.IBar = mock.object; expect(bar).to.be.not.null; expect(bar).to.eq(bar2); }); it("should expose interface passed in as type variable to ctor", () => { const mock: TypeMoq.IMock<TypeMoqTests.IBar> = Mock.ofType<TypeMoqTests.IBar>(TypeMoqTests.Bar); const bar: TypeMoqTests.IBar = mock.object; const bar2: TypeMoqTests.Bar = mock.object; expect(bar).to.be.not.null; expect(bar).to.eq(bar2); }); it("should expose type of object passed in as variable to ctor", () => { const bar = new TypeMoqTests.Bar(); const mock: TypeMoq.IMock<TypeMoqTests.Bar> = Mock.ofInstance(bar); const bar2: TypeMoqTests.Bar = mock.object; expect(bar2).to.be.not.null; }); it("should expose type of function passed in as variable to ctor", () => { const mock1: TypeMoq.IMock<() => string> = Mock.ofInstance(TypeMoqTests.someFunc); const mock2: TypeMoq.IMock<(a: any, b: any, c: any) => string> = Mock.ofInstance(TypeMoqTests.someFuncWithArgs); const func1: () => string = mock1.object; const func2: (a: any, b: any, c: any) => string = mock2.object; expect(func1).to.be.not.null; expect(func2).to.be.not.null; }); describe("dynamic mock", () => { it("should initialize proxy instance", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock: TypeMoq.IMock<TypeMoqTests.Bar> = Mock.ofType<TypeMoqTests.Bar>(); const bar: TypeMoqTests.Bar = mock.object; const bar2: TypeMoqTests.IBar = mock.object; expect(bar).to.be.not.null; expect(bar).to.eq(bar2); } }); it("should expose interface passed in as type variable", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock: TypeMoq.IMock<TypeMoqTests.IBar> = Mock.ofType<TypeMoqTests.IBar>(); const bar: TypeMoqTests.IBar = mock.object; const bar2: TypeMoqTests.Bar = mock.object; expect(bar).to.be.not.null; expect(bar).to.eq(bar2); } }); it("should allow to enumerate properties being mocked", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { interface I { prop: string, method(): string }; const mock = Mock.ofType<I>(); mock.setup(x => x.prop).returns(() => 'value1'); mock.setup(x => x.method()).returns(() => 'value2'); let count = 0; for (let prop in mock.object) count++; expect(count).eq(2); } }); }); }); describe(".target", () => { it("should initialize proxy instance", () => { const mock: TypeMoq.IMock<TypeMoqTests.Bar> = Mock.ofType(TypeMoqTests.Bar); const bar: TypeMoqTests.Bar = mock.target; const bar2: TypeMoqTests.IBar = mock.target; expect(bar).to.be.not.null; expect(bar).to.eq(bar2); }); it("should expose interface passed in as type variable to ctor", () => { const mock: TypeMoq.IMock<TypeMoqTests.IBar> = Mock.ofType<TypeMoqTests.IBar>(TypeMoqTests.Bar); const bar: TypeMoqTests.IBar = mock.target; const bar2: TypeMoqTests.Bar = mock.target; expect(bar).to.be.not.null; expect(bar).to.eq(bar2); }); it("should expose type of object passed in as variable to ctor", () => { const bar = new TypeMoqTests.Bar(); const mock: TypeMoq.IMock<TypeMoqTests.Bar> = Mock.ofInstance(bar); const bar2: TypeMoqTests.Bar = mock.target; expect(bar2).to.be.not.null; }); it("should expose type of function passed in as variable to ctor", () => { const mock1: TypeMoq.IMock<() => string> = Mock.ofInstance(TypeMoqTests.someFunc); const mock2: TypeMoq.IMock<(a: any, b: any, c: any) => string> = Mock.ofInstance(TypeMoqTests.someFuncWithArgs); const func1: () => string = mock1.target; const func2: (a: any, b: any, c: any) => string = mock2.target; expect(func1).to.be.not.null; expect(func2).to.be.not.null; }); describe("dynamic mock", () => { it("should initialize proxy instance", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock: TypeMoq.IMock<TypeMoqTests.Bar> = Mock.ofType<TypeMoqTests.Bar>(); const bar: TypeMoqTests.Bar = mock.target; const bar2: TypeMoqTests.IBar = mock.target; expect(bar).to.be.not.null; expect(bar).to.eq(bar2); } }); it("should expose interface passed in as type variable", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock: TypeMoq.IMock<TypeMoqTests.IBar> = Mock.ofType<TypeMoqTests.IBar>(); const bar: TypeMoqTests.IBar = mock.target; const bar2: TypeMoqTests.Bar = mock.target; expect(bar).to.be.not.null; expect(bar).to.eq(bar2); } }); it("should allow to enumerate properties being mocked", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { interface I { prop: string, method(): string }; const mock = Mock.ofType<I>(); mock.setup(x => x.prop).returns(() => 'value1'); mock.setup(x => x.method()).returns(() => 'value2'); let count = 0; for (let prop in mock.target) count++; expect(count).eq(2); } }); }); }); describe("mock behavior", () => { it("should return default value when no setup found and behavior is loose", () => { const mock = Mock.ofType(TypeMoqTests.Doer); expect(mock.object.doNumber(999)).to.eq(undefined); }); it("should return setup value when setup found and behavior is strict", () => { const mock = Mock.ofType(TypeMoqTests.Doer, MockBehavior.Strict); mock.setup(x => x.doNumber(123)).returns(() => 999); expect(mock.object.doNumber(123)).to.eq(999); expect(() => mock.object.doNumber(999)).to.throw(MockException); expect(() => mock.object.doNumber()).to.throw(MockException); }); it("should throw when no setup found and behavior is strict", () => { const mock = Mock.ofType(TypeMoqTests.Doer, MockBehavior.Strict); expect(() => mock.object.doNumber(999)).to.throw(MockException); }); it("should throw an exception derived from Error when no setup found and behavior is strict", () => { const mock = Mock.ofType(TypeMoqTests.Doer, MockBehavior.Strict); expect(() => mock.object.doNumber(999)).to.throw(Error); }); describe("dynamic mock", () => { it("should return default value when no setup found and behavior is loose", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.IDo>(); expect(mock.object.doNumber(999)).to.eq(undefined); } }); it("should return setup value when setup found and behavior is strict", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.IDo>(undefined, MockBehavior.Strict); mock.setup(x => x.doNumber(123)).returns(() => 999); expect(mock.object.doNumber(123)).to.eq(999); expect(() => mock.object.doNumber(999)).to.throw(MockException); expect(() => mock.object.doNumber()).to.throw(MockException); } }); it("should throw when no setup found and behavior is strict", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.IDo>(undefined, MockBehavior.Strict); expect(() => mock.object.doNumber(999)).to.throw(MockException); } }); it("should throw an exception derived from Error when no setup found and behavior is strict", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.IDo>(undefined, MockBehavior.Strict); expect(() => mock.object.doNumber(999)).to.throw(Error); } }); }); }); describe(".setup and .returns", () => { it("should match a no args function", () => { const mock: TypeMoq.IMock<() => string> = Mock.ofInstance(TypeMoqTests.someFunc); mock.setup(x => x()).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object()).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); }); it("should match a function with args", () => { const mock: TypeMoq.IMock<(a: any, b: any, c: any) => string> = Mock.ofInstance(TypeMoqTests.someFuncWithArgs); mock.setup(x => x(It.isAny(), It.isAny(), It.isAny())).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(1, 2, 3)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); }); it('should match a function with explicit number value params', () => { const mock = Mock.ofInstance<(x: number) => void>(() => { }); mock.setup(x => x(It.isValue(1))).returns(() => 123); expect(mock.object(1)).to.eq(123); }); it('should match a function with implicit number value params', () => { const mock = Mock.ofInstance<(x: number) => void>(() => { }); mock.setup(x => x(1)).returns(() => 123); expect(mock.object(1)).to.eq(123); }); it('should match a function with explicit string value params', () => { const mock = Mock.ofInstance<(x: string) => void>(() => { }); mock.setup(x => x(It.isValue("abc"))).returns(() => 123); expect(mock.object("abc")).to.eq(123); }); it('should match a function with implicit string value params', () => { const mock = Mock.ofInstance<(x: string) => void>(() => { }); mock.setup(x => x("abc")).returns(() => 123); expect(mock.object("abc")).to.eq(123); }); it('should match a function with partial object value params', () => { const mock = Mock.ofInstance<(x: any) => void>(() => { }); const anObject = { baz: 'hello', foo: 42 }; mock.setup(x => x(It.isObjectWith({ baz: 'hello' }))).returns(() => 123); expect(mock.object(anObject)).to.eq(123); }); it('should match a function with explicit object value params', () => { const mock = Mock.ofInstance<(x: any) => void>(() => { }); const anObject = {}; mock.setup(x => x(It.isValue(anObject))).returns(() => 123); expect(mock.object(anObject)).to.eq(123); }); it('should match a function with implicit object value params', () => { const mock = Mock.ofInstance<(x: any) => void>(() => { }); const anObject = {}; mock.setup(x => x(anObject)).returns(() => 123); expect(mock.object(anObject)).to.eq(123); }); it("should throw if more than one method is matched", () => { const mock = Mock.ofType(TypeMoqTests.Doer); expect(() => mock.setup(x => { x.doVoid(); x.doNumber(); })).to.throw(MockException); }); it("should match a no args method", () => { const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doNumber()).returns(() => 999); expect(mock.object.doNumber()).to.eq(999); }); it("should match a method with explicit number value params", () => { const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doNumber(It.isValue(321))).returns(() => 999); expect(mock.object.doNumber(321)).to.eq(999); expect(mock.object.doNumber(322)).to.eq(undefined); expect(mock.object.doNumber()).to.eq(undefined); }); it("should match a method with implicit number value params", () => { const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doNumber(321)).returns(() => 999); expect(mock.object.doNumber(321)).to.eq(999); expect(mock.object.doNumber(322)).to.eq(undefined); expect(mock.object.doNumber()).to.eq(undefined); }); it("should match a method with explicit string value params", () => { const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doString(It.isValue("abc"))).returns((s: string) => s.toUpperCase()); expect(mock.object.doString("abc")).to.eq("ABC"); expect(mock.object.doString("cba")).to.eq(undefined); expect(mock.object.doString()).to.eq(undefined); }); it("should match a method with implicit string value params", () => { const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doString("abc")).returns((s: string) => s.toUpperCase()); expect(mock.object.doString("abc")).to.eq("ABC"); expect(mock.object.doString("cba")).to.eq(undefined); expect(mock.object.doString()).to.eq(undefined); }); it("should match a method with partial object value params", () => { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; bar1.anyValue = 42; bar1.enumValue = TypeMoqTests.AnEnum.One; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; bar2.enumValue = TypeMoqTests.AnEnum.Two; const match = { anyValue: 42, enumValue: TypeMoqTests.AnEnum.One }; const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doObject(It.isObjectWith(match))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar2)).to.eq(undefined); bar2.anyValue = 42; bar2.enumValue = TypeMoqTests.AnEnum.One; expect(mock.object.doObject(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(new Object())).to.eq(undefined); expect(mock.object.doObject({ foo: 'nothing' })).to.eq(undefined); expect(mock.object.doObject()).to.eq(undefined); }); it("should match a method with explicit object value params", () => { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doObject(It.isValue(bar1))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar2)).to.eq(undefined); bar2.value = "Lorem ipsum dolor sit amet"; expect(mock.object.doObject(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(new Object())).to.eq(undefined); expect(mock.object.doObject()).to.eq(undefined); }); it("should match a method with implicit object value params", () => { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doObject(bar1)).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar2)).to.eq(undefined); bar2.value = "Lorem ipsum dolor sit amet"; expect(mock.object.doObject(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(new Object())).to.eq(undefined); expect(mock.object.doObject()).to.eq(undefined); }); it("should match a method with any object type params", () => { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doObject(It.isAnyObject(TypeMoqTests.Bar))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(new Object())).to.eq(undefined); expect(mock.object.doObject()).to.eq(undefined); }); it("should match a method with any string params", () => { const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doString(It.isAnyString())).returns(s => s.toUpperCase()); expect(mock.object.doString("Lorem ipsum dolor sit amet")).to.eq("LOREM IPSUM DOLOR SIT AMET"); }); it("should match a method with any number params", () => { const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doNumber(It.isAnyNumber())).returns(() => 999); expect(mock.object.doNumber(123)).to.eq(999); }); it("should match a method with any interface/class params", () => { const bar1 = new TypeMoqTests.Bar(); const bar2 = new TypeMoqTests.Bar(); const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doBar(It.isAnyObject(TypeMoqTests.Bar))).returns(() => bar2); expect(mock.object.doBar(bar1)).to.eq(bar2); }); it("should match a method param by a predicate", () => { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Ut enim ad minim veniam"; const bar2 = new TypeMoqTests.Bar(); const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doBar(It.is((x: TypeMoqTests.Bar) => x.value === "Ut enim ad minim veniam"))).returns(() => bar2); expect(mock.object.doBar(bar1)).to.eq(bar2); expect(mock.object.doBar(bar2)).to.eq(undefined); }); it("should match a property getter", () => { const mock = Mock.ofType(TypeMoqTests.FooWithPublicGetterAndSetter); mock.setup(x => x.foo).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.foo).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); }); it("should prefer oldest setup when multiple methods are setup", () => { const mock = Mock.ofType(TypeMoqTests.Doer); mock.setup(x => x.doNumber(It.isAnyNumber())).returns(() => 999); mock.setup(x => x.doString(It.isAnyString())).returns(() => "123"); mock.setup(x => x.doString(It.isAnyString())).returns(() => "456"); const user = new TypeMoqTests.DoerUser(mock.object); expect(user.execute("abc", 123)).to.eq("123"); }); it("should replay from oldest to newest record", () => { const mock = Mock.ofInstance((): number => -1, MockBehavior.Strict); mock.setup(x => x()).returns(() => 0); mock.setup(x => x()).returns(() => 1); mock.setup(x => x()).returns(() => 2); expect(mock.object()).to.eq(0); expect(mock.object()).to.eq(1); expect(mock.object()).to.eq(2); expect(() => mock.object()).to.throw(MockException); }); it("should replay indefinitely when only a single record exists", () => { const mock = Mock.ofInstance((): number => -1, MockBehavior.Strict); mock.setup(x => x()).returns(() => 0); expect(mock.object()).to.eq(0); expect(mock.object()).to.eq(0); expect(mock.object()).to.eq(0); }); it("should allow partial setup while keeping intact the target object", () => { const target = { a(): number { return 1; }, b(): number { return this.a(); }, }; const mock = Mock.ofInstance(target); mock.callBase = true; expect(mock.object.a()).equal(1); expect(mock.object.b()).equal(1); mock.setup(x => x.a()).returns(() => 2); expect(target.a()).equal(1); expect(target.b()).equal(1); expect(mock.object.a()).equal(2); expect(mock.object.b()).equal(2); }); it("should return a Promise resolved with the mocked object", done => { if (hasPromise) { const mock = TypeMoq.Mock.ofType(TypeMoqTests.Bar); Promise.resolve(mock.object) .then(x => { expect(x).eql(mock.object); done(); }); } else done(); }); describe("dynamic mock", () => { it("should be able to return for a property a falsy value", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock: TypeMoq.IMock<TypeMoqTests.IBar> = TypeMoq.Mock.ofType<TypeMoqTests.IBar>(); mock.setup(x => x.anyValue).returns(() => null); expect(mock.object.anyValue).to.be.null; mock.reset(); mock.setup(x => x.anyValue).returns(() => 0); expect(mock.object.anyValue).to.eq(0); mock.reset(); mock.setup(x => x.anyValue).returns(() => undefined); expect(mock.object.anyValue).to.be.undefined; } }); it("should throw if more than one method is matched", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.Doer>(); expect(() => mock.setup(x => { x.doVoid(); x.doNumber(); })).to.throw(MockException); } }); it("should match a no args function", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<Function>(); mock.setup(x => x()).returns(() => 999); expect(mock.object()).to.eq(999); } }); it("should match a Function.prototype function", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<Function>(); const context = {}; mock.setup(fn => fn.bind(context)).returns(fn => 999); expect(mock.object.bind(context)).to.eq(999); } }); it("should match a no args method", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doNumber()).returns(() => 999); expect(mock.object.doNumber()).to.eq(999); } }); it("should match a function with explicit number value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<Function>(); mock.setup(x => x(It.isValue(321))).returns(() => 999); expect(mock.object(321)).to.eq(999); expect(mock.object(322)).to.eq(undefined); expect(mock.object()).to.eq(undefined); } }); it("should match a method with explicit number value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doNumber(It.isValue(321))).returns(() => 999); expect(mock.object.doNumber(321)).to.eq(999); expect(mock.object.doNumber(322)).to.eq(undefined); expect(mock.object.doNumber()).to.eq(undefined); } }); it("should match a function with implicit number value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<Function>(); mock.setup(x => x(321)).returns(() => 999); expect(mock.object(321)).to.eq(999); expect(mock.object(322)).to.eq(undefined); expect(mock.object()).to.eq(undefined); } }); it("should match a method with implicit number value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doNumber(321)).returns(() => 999); expect(mock.object.doNumber(321)).to.eq(999); expect(mock.object.doNumber(322)).to.eq(undefined); expect(mock.object.doNumber()).to.eq(undefined); } }); it("should match a function with explicit string value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<Function>(); mock.setup(x => x(It.isValue("abc"))).returns((s: string) => s.toUpperCase()); expect(mock.object("abc")).to.eq("ABC"); expect(mock.object("cba")).to.eq(undefined); expect(mock.object()).to.eq(undefined); } }); it("should match a method with explicit string value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doString(It.isValue("abc"))).returns((s: string) => s.toUpperCase()); expect(mock.object.doString("abc")).to.eq("ABC"); expect(mock.object.doString("cba")).to.eq(undefined); expect(mock.object.doString()).to.eq(undefined); } }); it("should match a function with implicit string value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<Function>(); mock.setup(x => x("abc")).returns((s: string) => s.toUpperCase()); expect(mock.object("abc")).to.eq("ABC"); expect(mock.object("cba")).to.eq(undefined); expect(mock.object()).to.eq(undefined); } }); it("should match a method with implicit string value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doString("abc")).returns((s: string) => s.toUpperCase()); expect(mock.object.doString("abc")).to.eq("ABC"); expect(mock.object.doString("cba")).to.eq(undefined); expect(mock.object.doString()).to.eq(undefined); } }); it("should match a function with partial object value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; bar1.anyValue = 42; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const match = { anyValue: 42 }; const mock = Mock.ofType<Function>(); mock.setup(x => x(It.isObjectWith(match))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(bar2)).to.eq(undefined); bar2.anyValue = 42; expect(mock.object(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(new Object())).to.eq(undefined); expect(mock.object({ foo: 'nothing' })).to.eq(undefined); expect(mock.object()).to.eq(undefined); } }); it("should match a function with explicit object value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const mock = Mock.ofType<Function>(); mock.setup(x => x(It.isValue(bar1))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(bar2)).to.eq(undefined); bar2.value = "Lorem ipsum dolor sit amet"; expect(mock.object(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(new Object())).to.eq(undefined); expect(mock.object()).to.eq(undefined); } }); it("should match a method with explicit object value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doObject(It.isValue(bar1))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar2)).to.eq(undefined); bar2.value = "Lorem ipsum dolor sit amet"; expect(mock.object.doObject(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(new Object())).to.eq(undefined); expect(mock.object.doObject()).to.eq(undefined); } }); it("should match a function with implicit object value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const mock = Mock.ofType<Function>(); mock.setup(x => x(bar1)).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(bar2)).to.eq(undefined); bar2.value = "Lorem ipsum dolor sit amet"; expect(mock.object(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(new Object())).to.eq(undefined); expect(mock.object()).to.eq(undefined); } }); it("should match a method with implicit object value params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doObject(bar1)).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar2)).to.eq(undefined); bar2.value = "Lorem ipsum dolor sit amet"; expect(mock.object.doObject(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(new Object())).to.eq(undefined); expect(mock.object.doObject()).to.eq(undefined); } }); it("should match a function with any object type params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const mock = Mock.ofType<Function>(); mock.setup(x => x(It.isAnyObject(TypeMoqTests.Bar))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object(new Object())).to.eq(undefined); expect(mock.object()).to.eq(undefined); } }); it("should match a method with any object type params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Lorem ipsum dolor sit amet"; const bar2 = new TypeMoqTests.Bar(); bar2.value = "Ut enim ad minim veniam"; const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doObject(It.isAnyObject(TypeMoqTests.Bar))).returns(() => "At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar1)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(bar2)).to.eq("At vero eos et accusamus et iusto odio dignissimos ducimus"); expect(mock.object.doObject(new Object())).to.eq(undefined); expect(mock.object.doObject()).to.eq(undefined); } }); it("should match a function with any string params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<Function>(); mock.setup(x => x(It.isAnyString())).returns(s => s.toUpperCase()); expect(mock.object("Lorem ipsum dolor sit amet")).to.eq("LOREM IPSUM DOLOR SIT AMET"); } }); it("should match a method with any string params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doString(It.isAnyString())).returns(s => s.toUpperCase()); expect(mock.object.doString("Lorem ipsum dolor sit amet")).to.eq("LOREM IPSUM DOLOR SIT AMET"); } }); it("should match a function with any number params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<Function>(); mock.setup(x => x(It.isAnyNumber())).returns(() => 999); expect(mock.object(123)).to.eq(999); } }); it("should match a method with any number params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doNumber(It.isAnyNumber())).returns(() => 999); expect(mock.object.doNumber(123)).to.eq(999); } }); it("should match a function with any interface/class params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); const bar2 = new TypeMoqTests.Bar(); const mock = Mock.ofType<Function>(); mock.setup(x => x(It.isAnyObject(TypeMoqTests.Bar))).returns(() => bar2); expect(mock.object(bar1)).to.eq(bar2); } }); it("should match a method with any interface/class params", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); const bar2 = new TypeMoqTests.Bar(); const mock = Mock.ofType<TypeMoqTests.Doer>(); mock.setup(x => x.doBar(It.isAnyObject(TypeMoqTests.Bar))).returns(() => bar2); expect(mock.object.doBar(bar1)).to.eq(bar2); } }); it("should match a function param by a predicate", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Ut enim ad minim veniam"; const bar2 = new TypeMoqTests.Bar(); const mock = Mock.ofType<Function>(); mock.setup(x => x(It.is((x: TypeMoqTests.Bar) => x.value === "Ut enim ad minim veniam"))).returns(() => bar2); expect(mock.object(bar1)).to.eq(bar2); expect(mock.object(bar2)).to.eq(undefined); } }); it("should match a method param by a predicate taking a string param", () => { if (!hasProxyES6) { console.log(noProxyES6Msg); } else { const bar1 = new TypeMoqTests.Bar(); bar1.value = "Ut enim ad minim veniam"; const bar2 = new TypeMoqTests.Bar(); c