question

paramjit-0569 avatar image
0 Votes"
paramjit-0569 asked paramjit-0569 answered

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

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














vs-testing
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @paramjit-0569
What did you do after "the tests was running successfully before"?Have you used third-party extensions?
You can check all initialization logic of the type you are referring to for the first time in the statement where the exception is thrown-in your case: LoginTests.cs line 22.

0 Votes 0 ·

Hi, @PengGe-MSFT
I had Separate projects for Views, ViewModels and Models. After the tests were running successfully I moved all the code to single dotnetstandard 2.1 project. The Xamarin.Forms project builds and runs successfully on Android Simulator.
The issue arises with test project only.

0 Votes 0 ·

Hi, @PengGe-MSFT
I tried to reproduce your issue on my device but failed.
You can try to reinstall the xUnit extension. I think you can rewrite the test code because your project has changed.

0 Votes 0 ·
Show more comments

1 Answer

paramjit-0569 avatar image
0 Votes"
paramjit-0569 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.