"System.TypeInitializationException" when running xUnit tests in Visual Studio 2019 Version 16.10.4

Paramjit Singh 191 Reputation points
2021-08-10T00:09:38.653+00:00

I am working on Xamarin.Forms project. I have a xUnit test project targeting dotnetcore 3.1. The xunit NuGet version is 2.4.1. When I run the tests I get the error mentioned in the title and all tests fail. The complete test detail summary is as follows:

Milk.UnitTests.LoginTest.SelectedLanguageApplied(language: "Hindi")
Source: LoginTests.cs line 22
Duration: < 1 ms

Message:
System.TypeInitializationException : The type initializer for '<Module>' threw an exception.
---- System.InvalidProgramException : The JIT compiler encountered invalid IL code or an internal limitation.

Stack Trace:
LoginTest.SelectedLanguageApplied(String language)
AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
LoginTest.SelectedLanguageApplied(String language)
----- Inner Stack Trace -----
cctor()

The test method is:

 [Theory]
          [InlineData("Hindi")]
          [InlineData("English")]

           //The choosen language is applied
           public async void SelectedLanguageApplied(string language)
           {
               //Arrange
                LoginViewModel loginViewModel = new LoginViewModel();

             //Act
             string result = await loginViewModel.ChangeLanguage(language);

              //Assert
                Assert.Equal(language, result);
          }

The ViewModel method of the above test is:

public async Task<string> ChangeLanguage(string selectedLanguage)
         {
             _selectedLanguage = selectedLanguage;
            CultureInfo language = CultureInfo.GetCultures(CultureTypes.NeutralCultures).ToList().First(element => element.EnglishName.Contains(selectedLanguage));
             Thread.CurrentThread.CurrentUICulture = language;
            AppResources.Culture = language;
            return language.EnglishName;           
        }

I am really frustated as the tests was running successfully before.
Thank you

Developer technologies | Visual Studio | Testing
{count} votes

1 answer

Sort by: Most helpful
  1. Paramjit Singh 191 Reputation points
    2021-08-16T11:11:41.147+00:00

    The error has gone by removing the Realm Package from the Shared Project(code tested from this project). The realm package already installed in the Models project. which have all the Models of the App. The Shared project reference the Model project so it can still work with Realm.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.