Skip to content

Fix date related tests in project on non en-US-like environments #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
jballe opened this issue Mar 25, 2025 · 0 comments
Open
1 task done

Fix date related tests in project on non en-US-like environments #34

jballe opened this issue Mar 25, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@jballe
Copy link
Contributor

jballe commented Mar 25, 2025

What happened?

Description

When I run the (nice) test suite, I get errors on date related tests, when my locale on the workstation is eg. Danish.
I would be happy to make a PR for this. The intention of this issue is as well to align, highlight which tests are failing and agree on the relevant and proper way to handle it.

DateTagHelper

tagHelperOutput.Content.GetContent().Should().Be(_date.ToString(dateFormat, CultureInfo.InvariantCulture));

This test is for when no specific culture is assigned (which is great and there are other tests for specific cultures which are working completely fine). The test is assuming CultureInfo.InvariantCulture but the implementation is specifically using CultureInfo.CurrentCulture when nothing is specified.

CultureInfo culture = !string.IsNullOrWhiteSpace(Culture) ? CultureInfo.CreateSpecificCulture(Culture) : CultureInfo.CurrentCulture;

I would expect those to be aligned, and the implementation sounds like the right one.

DateFieldTagHelper

This uses the (nice) TestServer to render the razor view

And hereby it is implicitly testing the current culture. This has already been handled for a different line in the view

sectionNode.ChildNodes[5].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString(CultureInfo.CurrentCulture));

So we should either

a) we should in similar way generate the expected string based on CultureInfo.CurrentCulture

Alternatively

b) we need to set the current culture for the request in the TestServer. This could be done by adding this to

             app.UseRequestLocalization(cfg =>
             {
                 cfg.SupportedCultures = cfg.SupportedUICultures = [new CultureInfo("en-US"), new CultureInfo("uk-UA")];
                 cfg.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US");
                 cfg.RequestCultureProviders.Clear();
             });

The view includes a scenario with the specific uk-UA culture, so this must be included in the supported cultures.

Reproduction Steps

It can be reproduced with the powershell commandlet Set-Culture

Actual Behavior

set-culture "da-dk"
dotnet test --no-restore -v quiet

Expected Behavior

The same as

set-culture "en-us"
dotnet test --no-restore -v quiet

Next steps

Let me know if I should create PR with those changes and if you prefer a) or b) - or something completely different.

Relevant log output

C:\Program Files\dotnet\sdk\9.0.202\Microsoft.TestPlatform.targets(48,5): warning : No test is available in C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\data\Sitecore.AspNetCore.SDK.TestData\bin\Debug\net8.0\Sitecore.AspNetCore.SDK.TestData.dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.
C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.RenderingEngine.Tests\TagHelpers\Fields\DateTagHelperFixture.cs(276): error TESTERROR:
  Sitecore.AspNetCore.SDK.RenderingEngine.Tests.TagHelpers.Fields.DateTagHelperFixture.Process_ScDateTagWithAspDataAttri
  buteWithCustomFormat_GeneratesCustomDateFormatOutput (200ms): Error Message: Expected tagHelperOutput.Content.GetConte
  nt() to be "05/04/2012 2:00", but "05-04-2012 2:00" differs near "-04" (index 2).
  Stack Trace:
     at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
     at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
     at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
     at FluentAssertions.Primitives.StringEqualityValidator.ValidateAgainstMismatch()
     at FluentAssertions.Primitives.StringValidator.Validate()
     at FluentAssertions.Primitives.StringAssertions`1.Be(String expected, String because, Object[] becauseArgs)
     at Sitecore.AspNetCore.SDK.RenderingEngine.Tests.TagHelpers.Fields.DateTagHelperFixture.Process_ScDateTagWithAspDat
  aAttributeWithCustomFormat_GeneratesCustomDateFormatOutput(DateTagHelper sut, TagHelperContext tagHelperContext, TagHe
  lperOutput tagHelperOutput) in C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.RenderingEngine
  .Tests\TagHelpers\Fields\DateTagHelperFixture.cs:line 276
     at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
     at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags inv
  okeAttr)
C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.RenderingEngine.Tests\TagHelpers\Fields\DateTagHelperFixture.cs(141): error TESTERROR:
  Sitecore.AspNetCore.SDK.RenderingEngine.Tests.TagHelpers.Fields.DateTagHelperFixture.Process_ScDateTagWithCustomFormat
  _GeneratesCustomDateFormatOutput (2ms): Error Message: Expected tagHelperOutput.Content.GetContent() to be "05/04/2012
   2:00", but "05-04-2012 2:00" differs near "-04" (index 2).
  Stack Trace:
     at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
     at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
     at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
     at FluentAssertions.Primitives.StringEqualityValidator.ValidateAgainstMismatch()
     at FluentAssertions.Primitives.StringValidator.Validate()
     at FluentAssertions.Primitives.StringAssertions`1.Be(String expected, String because, Object[] becauseArgs)
     at Sitecore.AspNetCore.SDK.RenderingEngine.Tests.TagHelpers.Fields.DateTagHelperFixture.Process_ScDateTagWithCustom
  Format_GeneratesCustomDateFormatOutput(DateTagHelper sut, TagHelperContext tagHelperContext, TagHelperOutput tagHelper
  Output) in C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.RenderingEngine.Tests\TagHelpers\Fi
  elds\DateTagHelperFixture.cs:line 141
     at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
     at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags inv
  okeAttr)
C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests\Fixtures\TagHelpers\AllFieldTagHelpersFixture.cs(84): error TESTERROR:
  Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.TagHelpers.AllFieldTagHelpersFixture.ComponentWithA
  llFieldTypes_RendersFieldsCorrectly (240ms): Error Message: Expected sectionNode.ChildNodes.First(n => n.Name.Equals("
  div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div6", StringComparison.OrdinalIgnoreCase)).InnerHtml to be
  "05/04/2012", but "05-04-2012" differs near "-04" (index 2).
  Stack Trace:
     at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
     at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
     at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
     at FluentAssertions.Primitives.StringEqualityValidator.ValidateAgainstMismatch()
     at FluentAssertions.Primitives.StringValidator.Validate()
     at FluentAssertions.Primitives.StringAssertions`1.Be(String expected, String because, Object[] becauseArgs)
     at Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.TagHelpers.AllFieldTagHelpersFixture.Componen
  tWithAllFieldTypes_RendersFieldsCorrectly() in C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK
  .RenderingEngine.Integration.Tests\Fixtures\TagHelpers\AllFieldTagHelpersFixture.cs:line 84
  --- End of stack trace from previous location ---
C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests\Fixtures\Binding\ViewFieldsBindingFixture.cs(78): error TESTERROR:
  Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.Binding.ViewFieldsBindingFixture.SitecoreLayoutMode
  lBinders_BindDataCorrectly (109ms): Error Message: Expected sectionNode.ChildNodes.First(n => n.Name.Equals("textarea"
  , StringComparison.OrdinalIgnoreCase)).InnerText "
  2019-12-12 00:00:00" to contain "12/12/2019".
  Stack Trace:
     at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
     at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
     at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
     at FluentAssertions.Primitives.StringAssertions`1.Contain(String expected, String because, Object[] becauseArgs)
     at Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.Binding.ViewFieldsBindingFixture.SitecoreLayo
  utModelBinders_BindDataCorrectly() in C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.Renderin
  gEngine.Integration.Tests\Fixtures\Binding\ViewFieldsBindingFixture.cs:line 78
  --- End of stack trace from previous location ---
C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests\Fixtures\TagHelpers\RichTextFieldTagHelperFixture.cs(73): error TESTERROR:
  Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.TagHelpers.RichTextFieldTagHelperFixture.RichTextFi
  eldTagHelper_DoesNotResetOtherTagHelperOutput (73ms): Error Message: Expected sectionNode.ChildNodes.First(n => n.Name
  .Equals("textarea", StringComparison.OrdinalIgnoreCase)).InnerText "
  2019-12-12 00:00:00" to contain "12/12/2019".
  Stack Trace:
     at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
     at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
     at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
     at FluentAssertions.Primitives.StringAssertions`1.Contain(String expected, String because, Object[] becauseArgs)
     at Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.TagHelpers.RichTextFieldTagHelperFixture.Rich
  TextFieldTagHelper_DoesNotResetOtherTagHelperOutput() in C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspN
  etCore.SDK.RenderingEngine.Integration.Tests\Fixtures\TagHelpers\RichTextFieldTagHelperFixture.cs:line 73
  --- End of stack trace from previous location ---
C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests\Fixtures\TagHelpers\DateFieldTagHelperFixture.cs(96): error TESTERROR:
  Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.TagHelpers.DateFieldTagHelperFixture.DateTagHelper_
  GeneratesProperDate (88ms): Error Message: Expected sectionNode.ChildNodes[1].InnerHtml to be "05/04/2012", but "05-04
  -2012" differs near "-04" (index 2).
  Stack Trace:
     at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
     at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
     at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
     at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
     at FluentAssertions.Primitives.StringEqualityValidator.ValidateAgainstMismatch()
     at FluentAssertions.Primitives.StringValidator.Validate()
     at FluentAssertions.Primitives.StringAssertions`1.Be(String expected, String because, Object[] becauseArgs)
     at Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.TagHelpers.DateFieldTagHelperFixture.DateTagH
  elper_GeneratesProperDate() in C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK\tests\Sitecore.AspNetCore.SDK.RenderingEngine
  .Integration.Tests\Fixtures\TagHelpers\DateFieldTagHelperFixture.cs:line 96
  --- End of stack trace from previous location ---
PS C:\Repos\jballe\Sitecore-ASP.NET-Core-SDK>

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jballe jballe added the bug Something isn't working label Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant