Search code examples
c#unit-testingtypemocktypemock-isolator

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


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);

Solution

  • 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.