How to set culture in xunit test for dotnetcore

I have the following unit test, which I am transferring from the .Net Framework library to the .Net core xunint test library. The unit test project must be added to

https://github.com/dotliquid/dotliquid

and is added to the selected file, as shown here.

enter image description here

unit test I'm trying to add

[Test] public void ParsingWithCommaDecimalSeparatorShouldWork() { var ci = new CultureInfo(CultureInfo.CurrentCulture.Name) { NumberFormat = { NumberDecimalSeparator = "," , NumberGroupSeparator = "." } }; Thread.CurrentThread.CurrentCulture = ci; var t = Template.Parse("{{2.5}}"); var result = t.Render( new Hash(), CultureInfo.InvariantCulture ); Assert.AreEqual( result, "2.5" ); } 

However, the test is not compiled in the dotnet core.

Severity Code Description Project File Line Suppression State Error CS1061 "Thread" does not contain a definition for "CurrentCulture" and the extension method "CurrentCulture" accepts the first argument of the type "Thread" (you don’t see using the directive or assembly link?) DotLiquid.Tests (net451) C: \ Users \ phelan \ workspace \ dotliquid \ src \ DotLiquid.Tests \ OutputTests.cs 113 N / A

I need to have different unit tests with different cultures. I would like to create an XUnit theory where each instance is passed in a different culture for unit test for validation. How is this done in .NetCore?

+5
source share
2 answers

I looked at some of the dotnet source and I found this.

 CultureInfo.DefaultThreadCurrentCulture = ci; 

It looks like you can set the culture of the current thread by default from the static property CultureInfo , and not from Thread.CurrentThread

missed you a little more, I found this

 public CultureInfo CurrentCulture { get { Contract.Ensures(Contract.Result<CultureInfo>() != null); return CultureInfo.CurrentCulture; } set { Contract.EndContractBlock(); // If you add more pre-conditions to this method, check to see if you also need to // add them to CultureInfo.DefaultThreadCurrentCulture.set. if (m_CurrentCulture == null && m_CurrentUICulture == null) nativeInitCultureAccessors(); CultureInfo.CurrentCulture = value; } } 

This is the value of Thread.cs . Thus, you can explicitly set the CultureInfo.CurrentCulture property.

Example:

 CultureInfo.CurrentCulture = new CultureInfo("en-GB"); ; Assert.Equal("Β£1,000.00", String.Format("{0:C}", 1000)); CultureInfo.CurrentCulture = new CultureInfo("en-US"); ; Assert.Equal("$1,000.00", String.Format("{0:C}", 1000)); 

enter image description here

Csproj file for unit test project:

 <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp1.0</TargetFramework> <IsPackable>false</IsPackable> <ApplicationIcon /> <OutputType>Library</OutputType> <StartupObject /> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170425-07" /> <PackageReference Include="xunit" Version="2.2.0" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" /> </ItemGroup> </Project> 
+5
source

The solution is to install

 CultureInfo.DefaultThreadCurrentCulture = ci; 

and then expand the new thread. This will set the current culture for the next stream. Last test case.

 [Test] public void ParsingWithCommaDecimalSeparatorShouldWork() { var ci = new CultureInfo(CultureInfo.CurrentCulture.Name) { NumberFormat = { NumberDecimalSeparator = "," , NumberGroupSeparator = "." } }; CultureInfo.DefaultThreadCurrentCulture = ci; var result = ""; var thread = new Thread ( delegate() { Console.WriteLine(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); Console.WriteLine(CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator); var t = Template.Parse("{{2.5}}"); result = t.Render(new Hash(), CultureInfo.InvariantCulture); } ); thread.Start(); thread.Join(); Assert.AreEqual(result, "2.5"); } 

which is a little messy but doing the job.

0
source

Source: https://habr.com/ru/post/1271763/


All Articles