Skip to content
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

One-off error in unit tests #123

Closed
igorkulman opened this issue Apr 3, 2014 · 1 comment · Fixed by #131
Closed

One-off error in unit tests #123

igorkulman opened this issue Apr 3, 2014 · 1 comment · Fixed by #131

Comments

@igorkulman
Copy link
Contributor

I am trying to add Slovak localization, that requires a separate Formatter. I have written unit tests but when I run them, some of them are one-off.

Consider

        [Theory]
        [InlineData(1, "o hodinu")]
        [InlineData(2, "o 2 hodiny")]
        [InlineData(3, "o 3 hodiny")]
        [InlineData(4, "o 4 hodiny")]
        [InlineData(5, "o 5 hodín")]
        [InlineData(6, "o 6 hodín")]
        [InlineData(10, "o 10 hodín")]
        public void NHoursFromNow(int number, string expected)
        {
            var humanize = DateTime.UtcNow.AddHours(number).Humanize();
            Assert.Equal(expected, humanize);
        }

When I run the tests, I get "o 9 hodín" for the last case testing "o 10 hodín". I guess the Humanize method got called with 9 instead of 10.

Am I missing something in writting the tests?

@MehdiK
Copy link
Member

MehdiK commented Apr 3, 2014

This is an interesting issue which is hard to avoid. The problem is that sometimes some of the date/time related tests fail just because by the time the calculation is happening the CPU has ticked over which results into a value that's one unit larger or smaller than the expected value when you're checking the past and future dates respectively.

The only thing we could do to avoid this issue was to inject the base date in and to avoid the CPU ticking over by "freezing" the time. You can see this in DateTimeHumanizeTests here.

To fix this for your locale and to prevent it for all the other locales we will need to refactor the Verify logic out of the DateTimeHumanizeTests and apply it on all other locales in tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants