Can not call Swap.AllInstances() more than once on a type

427 views Asked by At

I have a test case that has a problem where the exception in the title is thrown.

The exception is only thrown when the static methods fake is also present in the test.

My presumption is that StaticMethods fake is also doing the swap. If this is the case, how can I fake static methods and also replace instances with my instance fake?

[Test]
[Isolated]
[Factory("TruckDispatchData")]
public void TruckDispatchTest(
    IEnumerable<DeliveryInfo> deliveryInfo,
    bool expectedResult)
{
    Isolate.Fake.StaticMethods(typeof(Order), Members.MustSpecifyReturnValues);
    var order = Isolate.Fake.Instance<Order>(Members.MustSpecifyReturnValues, ConstructorWillBe.Ignored, BaseConstructorWillBe.Ignored);
    Isolate.Swap.AllInstances<Order>().With(order);
2

There are 2 answers

0
Al.exe On BEST ANSWER

I work at Typemock, Just checked this issue and I can confirm that this is a bug. The workaround for this issue is pretty simple, you just have to switch the order of the calls. e.g.

var order = Isolate.Fake.Instance<Order>(Members.MustSpecifyReturnValues, ConstructorWillBe.Ignored, BaseConstructorWillBe.Ignored);
Isolate.Swap.AllInstances<Order>().With(order);
Isolate.Fake.StaticMethods(typeof(Order), Members.MustSpecifyReturnValues);

It will be fixed in future versions.

1
VMAtm On

Can't say for sure, as didn't use TypeMock library, but I think that the problem is in your Swap method:

Isolate.Swap.AllInstances<Order>()

As you call the all instances of Order class, the static methods aren't involved (as they aren't Order instance methods, they are Type of typeof(Order) instance methods), so, may be, Swap approach can be used in some different way, something like this:

Isolate.Swap.Static_SOMETHING_HERE.With(order);