florinn/typemoq

Interface mock object mutation does not work

Closed this issue · 1 comments

Hello, thanks for the repo.

I am looking for a bit of guidance here. As you can see neither of the cases below does what I expect it to do, which is to modify the object property.

Is this just a bug? Or is part of the guiding principle here that projects using Typemoq should implement immutable strategies, or not to use typemoq for interfaces where they might be mutated?

Thanks!

interface ISomething {
  propName: string
}

function changeSomething(something: ISomething): ISomething {
  something.propName = "nope";
  return something;
}

describe("will not modify a property on an interface mock", () => {
  it("will throw if an interface mock property is setup, and the object property is modified", () => {
    const mockedSomething = TypeMoq.Mock.ofType<ISomething>();
    mockedSomething.setup(m => m.propName).returns(() => "a");

    // will throw TypeError: 'set' on proxy: trap returned falsish for property 'propName'
    changeSomething(mockedSomething.object);
  });

  it("will not modify an object prop if an interface mock property not is setup", () => {
    const mockedSomething = TypeMoq.Mock.ofType<ISomething>();

    // will not throw, but will not change the object property
    changeSomething(mockedSomething.object);

    // will throw AssertionError: expected Function { name: '' } to be 'nope'
    mockedSomething.object.propName.should.equal("nope");
  });
});

Nevermind,

Thanks to @ddsol for helping me out!

mockSomething.setup(m => m.propName = TypeMoq.It.isValue("nope")).verifiable();

https://github.com/florinn/typemoq#matching-properties