Search code examples
xamarinintegration-testingmvvmcross

Mvvmcross Testing different view models fails when running together


I've come across an interesting error. I have two test files for my xamarin mobile application, both testing view models:

public class TestFirstViewModel : MvxIoCSupportingTest 
{

    public void AdditionalSetup() {
    //Register services and dependencies here.
    }

    [Fact]
    public TestMethod1() {
    // Successful test code here.
    }
}

That's in one file. In another file, I have:

public class TestSecondViewModel : MvxIoCSupportingTest 
{

    public void AdditionalSetup() {
    //Register services and dependencies here, slightly different from first
    }

    [Fact]
    public TestMethod2() {
    // Successful test code here.
    }
}

When I run these files individually (I'm using xunit), they work just fine. However, when I run them together, I get the following error on one of the test cases:

Result Message: Cirrious.CrossCore.Exceptions.MvxException : You cannot create more than one instance of MvxSingleton
Result StackTrace:  
at Cirrious.CrossCore.Core.MvxSingleton`1..ctor()
   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer..ctor(IMvxIocOptions options)
   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.Initialize(IMvxIocOptions options)
   at Cirrious.MvvmCross.Test.Core.MvxIoCSupportingTest.ClearAll()
   at Cirrious.MvvmCross.Test.Core.MvxIoCSupportingTest.Setup()
   at Project.Test.TestFirstViewModel.TestMethod1() in ...

Can anyone tell me what's going on here?


Solution

  • I ended up solving this question by changing testing frameworks. I had different ioc singleton initializations, because, well, they're different test cases and needed different inputs/mocks. Instead of using Xunit, I resorted to Nunit where their cache clearing was much more defined: Xunit doesn't exactly believe in setup and tear-down, so it made a test environment like this more difficult.