Skip to content

Commit 203c71f

Browse files
committed
Fix some tests that would fail when running under a non en-US culture
Before this commit, two tests would fail with this error (FaultExceptionTest.Serializable_TDetail and FaultExceptionTest.Serializable_Default): ``` Xunit.Sdk.EqualException Assert.Equal() Failure ↓ (pos 1259) Expected: ···:text><a:xmlLang>en-US</a:xmlLang></a:FaultException.FaultRea··· Actual: ···:text><a:xmlLang>fr-CH</a:xmlLang></a:FaultException.FaultRea··· ↑ (pos 1259) ``` Explicitly setting the expected culture with a `BeforeAfterTestAttribute` solves this issue.
1 parent 3551afc commit 203c71f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Globalization;
7+
using System.Reflection;
8+
using Xunit.Sdk;
9+
10+
namespace Infrastructure.Common
11+
{
12+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
13+
public class WcfCurrentCultureAttribute : BeforeAfterTestAttribute
14+
{
15+
private readonly CultureInfo _cultureInfo;
16+
private CultureInfo _savedCultureInfo;
17+
18+
public WcfCurrentCultureAttribute(string name)
19+
{
20+
_cultureInfo = new CultureInfo(name);
21+
}
22+
23+
public override void Before(MethodInfo methodUnderTest)
24+
{
25+
_savedCultureInfo = CultureInfo.CurrentCulture;
26+
CultureInfo.CurrentCulture = _cultureInfo;
27+
}
28+
29+
public override void After(MethodInfo methodUnderTest)
30+
{
31+
CultureInfo.CurrentCulture = _savedCultureInfo;
32+
}
33+
}
34+
}

src/System.ServiceModel.Primitives/tests/ServiceModel/FaultExceptionTest.cs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static void Ctor_TDetail_FaultReason()
3636
}
3737

3838
[WcfFact]
39+
[WcfCurrentCulture("en-US")]
3940
public static void Serializable_TDetail()
4041
{
4142
// This isn't exactly what NetFx generates as the HResult is different due to CommunicationException deriving from SystemException on NetFx
@@ -57,6 +58,7 @@ public static void Serializable_TDetail()
5758
}
5859

5960
[WcfFact]
61+
[WcfCurrentCulture("en-US")]
6062
public static void Serializable_Default()
6163
{
6264
string netfxBsl = @"<FaultException xmlns=""http://schemas.datacontract.org/2004/07/System.ServiceModel"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:x=""http://www.w3.org/2001/XMLSchema""><ClassName i:type=""x:string"" xmlns="""">System.ServiceModel.FaultException</ClassName><Message i:type=""x:string"" xmlns="""">The creator of this fault did not specify a Reason.</Message><Data i:nil=""true"" xmlns=""""/><InnerException i:nil=""true"" xmlns=""""/><HelpURL i:nil=""true"" xmlns=""""/><StackTraceString i:nil=""true"" xmlns=""""/><RemoteStackTraceString i:nil=""true"" xmlns=""""/><RemoteStackIndex i:type=""x:int"" xmlns="""">0</RemoteStackIndex><ExceptionMethod i:nil=""true"" xmlns=""""/><HResult i:type=""x:int"" xmlns="""">-2146233088</HResult><Source i:nil=""true"" xmlns=""""/><WatsonBuckets i:nil=""true"" xmlns=""""/><code i:type=""a:ArrayOfFaultException.FaultCodeData"" xmlns="""" xmlns:a=""http://schemas.datacontract.org/2004/07/System.ServiceModel""><a:FaultException.FaultCodeData><a:name>Sender</a:name><a:ns/></a:FaultException.FaultCodeData></code><reason i:type=""a:ArrayOfFaultException.FaultReasonData"" xmlns="""" xmlns:a=""http://schemas.datacontract.org/2004/07/System.ServiceModel""><a:FaultException.FaultReasonData><a:text>The creator of this fault did not specify a Reason.</a:text><a:xmlLang>en-US</a:xmlLang></a:FaultException.FaultReasonData></reason><messageFault i:nil=""true"" xmlns=""""/><action i:nil=""true"" xmlns=""""/></FaultException>";

0 commit comments

Comments
 (0)