简体   繁体   English

单元测试DateTime字符串

[英]Unit testing DateTime strings

I have a class that converts a string into a DateTime and perform some business logic in it: 我有一个类将string转换为DateTime并在其中执行一些业务逻辑:

public class Foo
{
    public Foo(string value)
    {
        Value = DateTime.Parse(value);
    }

    public DateTime Value { get; }

    // Some additional methods
}

I would like to unit test Foo so that it only accepts valid datetime string s, so I wrote a unit test: 我想对Foo进行单元测试,以便它只接受有效的日期时间string s,所以我写了一个单元测试:

public class FooTests
{
    [Fact]
    public void Foo_ValidDateTimeString_Returns()
    {
        const string testInput = "2015-01-01";

        var result = new Foo(testInput);

        var expected = new DateTime(2015, 1, 1);
        Assert.Equal(expected, result.Value);
    }
}

This test passes in various cultures such as en-US. 该测试通过各种文化,例如en-US。

My concern is, this unit test will fail when another colleague who is under a different CultureInfo which does not accept yyyy-MM-dd as a valid DateTime format. 我担心的是,当另一个不接受yyyy-MM-dd作为有效DateTime格式的CultureInfo同事时,这个单元测试将失败。

I do not want to enforce the test to be run under a specific CultureInfo in order to make the test pass. 我不想强制执行测试以在特定的CultureInfo下运行以使测试通过。 Is there another way? 还有另外一种方法吗?

You can always use CultureInfo.InvariantCulture as argument to Parse() method 您始终可以使用CultureInfo.InvariantCulture作为Parse()方法的参数

    Value = DateTime.Parse(value,CultureInfo.InvariantCulture);

so that you don't have to care about culture inside unit-tests. 这样你就不必在单元测试中关心文化了。 Another way, if you want to keep data as-is the other way is to go for exact parsing 另一种方法是,如果你想保持数据的另一种方式,那就是进行精确的解析

   Value = DateTime.ParseExact(value,"yyyy-MM-dd",CultureInfo.InvariantCulture);

I do not want to enforce the test to be run under a specific CultureInfo in order to make the test pass. 我不想强制执行测试以在特定的CultureInfo下运行以使测试通过。 Is there another way? 还有另外一种方法吗?

No. 没有。

Using the CurrentCulture on the system is usually a bad idea, because the string that you provided can mean different things for different culture settings. 在系统上使用CurrentCulture 通常是一个坏主意,因为您提供的字符串对于不同的文化设置可能意味着不同的东西。

For example; 例如; 2015-01-02 can mean 1st February or 2nd January for different cultures with using DateTime.Parse(string) overload. 对于使用DateTime.Parse(string)重载的不同文化, 2015-01-02可能意味着2月1日或1月2日。

DateTime.ParseExact with a specific culture is a much more detailed, controlled and exact approach. 具有特定文化的DateTime.ParseExact是一种更加详细,可控且精确的方法。

This stated in documentation as well; 这在文件中也有说明;

Because the Parse(String) method tries to parse the string representation of a date and time using the formatting rules of the current culture, trying to parse a particular string across different cultures can either fail or return different results . 由于Parse(String)方法尝试使用当前区域性的格式设置规则来解析日期和时间的字符串表示形式,因此尝试解析跨不同文化的特定字符串可能会失败或返回不同的结果 If a specific date and time format will be parsed across different locales, use the DateTime.Parse(String, IFormatProvider) method or one of the overloads of the ParseExact method and provide a format specifier . 如果将跨不同的语言环境解析特定的日期和时间格式,请使用DateTime.Parse(String,IFormatProvider)方法或ParseExact方法的重载之一,并提供格式说明符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM