如何在 dotnetcore xunit 测试中设置文化
How to set the culture in a dotnetcore xunit test
我有以下单元测试,我正在从 .Net Framework 库移植到 .Net 核心 xunint 测试库。需要添加单元测试的项目是
https://github.com/dotliquid/dotliquid
并且正在添加到所选文件中,如此处所示
我要添加的单元测试是
[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" );
}
但是测试无法在 dotnet 核心中编译。
Severity Code Description Project File Line Suppression State
Error CS1061 'Thread' does not contain a definition for
'CurrentCulture' and no extension method 'CurrentCulture' accepting a
first argument of type 'Thread' could be found (are you missing a
using directive or an assembly
reference?) DotLiquid.Tests(net451) C:\Users\phelan\workspace\dotliquid\src\DotLiquid.Tests\OutputTests.cs 113 N/A
我需要针对不同的文化进行不同的单元测试。我想创建一个 XUnit 理论,其中每个实例都以不同的文化传递,以便单元测试进行验证。这在 .NetCore 中是如何完成的?
我查看了一些 dotnet source 并找到了这个。
CultureInfo.DefaultThreadCurrentCulture = ci;
基本上看起来您可以从 CultureInfo
的静态 属性 而不是 Thread.CurrentThread
设置默认线程当前文化
再四处寻找,我发现 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;
}
}
这是在 Thread.cs
。因此,您可以明确设置 CultureInfo.CurrentCulture
属性。
示例:
CultureInfo.CurrentCulture = new CultureInfo("en-GB"); ;
Assert.Equal("£1,000.00", String.Format("{0:C}", 1000));
CultureInfo.CurrentCulture = new CultureInfo("en-US"); ;
Assert.Equal(",000.00", String.Format("{0:C}", 1000));
单元测试项目的 csproj 文件:
<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>
解决方法是设置
CultureInfo.DefaultThreadCurrentCulture = ci;
然后然后启动一个新线程。这将为下一个线程设置当前文化。最终的测试用例是。
[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");
}
有点乱,但完成工作。
我有以下单元测试,我正在从 .Net Framework 库移植到 .Net 核心 xunint 测试库。需要添加单元测试的项目是
https://github.com/dotliquid/dotliquid
并且正在添加到所选文件中,如此处所示
我要添加的单元测试是
[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" );
}
但是测试无法在 dotnet 核心中编译。
Severity Code Description Project File Line Suppression State Error CS1061 'Thread' does not contain a definition for 'CurrentCulture' and no extension method 'CurrentCulture' accepting a first argument of type 'Thread' could be found (are you missing a using directive or an assembly reference?) DotLiquid.Tests(net451) C:\Users\phelan\workspace\dotliquid\src\DotLiquid.Tests\OutputTests.cs 113 N/A
我需要针对不同的文化进行不同的单元测试。我想创建一个 XUnit 理论,其中每个实例都以不同的文化传递,以便单元测试进行验证。这在 .NetCore 中是如何完成的?
我查看了一些 dotnet source 并找到了这个。
CultureInfo.DefaultThreadCurrentCulture = ci;
基本上看起来您可以从 CultureInfo
的静态 属性 而不是 Thread.CurrentThread
再四处寻找,我发现 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;
}
}
这是在 Thread.cs
。因此,您可以明确设置 CultureInfo.CurrentCulture
属性。
示例:
CultureInfo.CurrentCulture = new CultureInfo("en-GB"); ;
Assert.Equal("£1,000.00", String.Format("{0:C}", 1000));
CultureInfo.CurrentCulture = new CultureInfo("en-US"); ;
Assert.Equal(",000.00", String.Format("{0:C}", 1000));
单元测试项目的 csproj 文件:
<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>
解决方法是设置
CultureInfo.DefaultThreadCurrentCulture = ci;
然后然后启动一个新线程。这将为下一个线程设置当前文化。最终的测试用例是。
[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");
}
有点乱,但完成工作。