florinn/typemoq

Override existing setup/don't use record-replay functionality

Amy-Lynn opened this issue · 4 comments

I set up the mock in a helper function/beforeEach with a couple of setup calls. Later in a specific test, I'll want to change the return values of one of those functions that were previously set up, but I want to keep the old setups in place.

If I run the same setup with a different return value, this ends up activating the record/replay functionality when I want to override it.

There's the reset functionality, but that seems to wipe out all of my existing setups,

Is there any way to do this? If not, would it make sense to either be able to disable the record functionality or be able to reset only on a specific function?

It would be great to clarify this. Would probably make sense to reset just one setup.

I would love this feature as well, e.g.:

myMock.setup(m=> m.myMethod(It.isAnyString())).returns(()=> 0);
...
myMock.setup(m=> m.myMethod("1")).returns(()=> 1);

In this scenario if I call myMethod("any") I should get 0 and if I call myMethod("1") I should get 1.

In the absence of this feature, I find a good way to "use up" the existing setups is to pass them as arguments to void. It is a little cumbersome but is the cleanest way I have found to achieve what I want. It also of reads in a vaguely sensible fashion, if you consider you are "voiding" the default configured value.

// In your test fixture setup
myMock.setup(x => x.myGetter).returns(() => 1)

// In your test arrangement
void (myMock.object.myGetter)
myMock.setup(x => x.myGetter).returns(() => 42)

This works for methods too, if you invoke them inside the void call.

Also very conveniently .myGetter will line up vertically in each statement, making it easier to spot what's going on.

In the absence of this feature, I find a good way to "use up" the existing setups is to pass them as arguments to void. It is a little cumbersome but is the cleanest way I have found to achieve what I want. It also of reads in a vaguely sensible fashion, if you consider you are "voiding" the default configured value.

// In your test fixture setup
myMock.setup(x => x.myGetter).returns(() => 1)

// In your test arrangement
void (myMock.object.myGetter)
myMock.setup(x => x.myGetter).returns(() => 42)

This works for methods too, if you invoke them inside the void call.

Also very conveniently .myGetter will line up vertically in each statement, making it easier to spot what's going on.

Sadly your workaround does not work for me :-(