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.
"System.TypeInitializationException" when running xUnit tests in Visual Studio 2019 Version 16.10.4
Paramjit Singh
191
Reputation points
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 msMessage: 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
371 questions