具有 sytem.DateTime 值的属性的单元测试 C#

如何为以下代码块编写单元测试


public class Hotel

{

    DateTime CLOSING_TIME // imported from somewhere else

    public bool IsOpen

    {

        get

        {

            return DateTime.Now <= CLOSING_TIME

        }

    }

}

我尝试遵循任何一个都会失败,我如何确保每次都通过两个单元测试用例:


[TestFixture]

public void ShouldBeOpen()

{

    var Hot= new Hotel();

    Assert.True(Hot.IsOpen); 

}


[TestFixture]

public void ShouldBeOpen()

{

    var Hot= new Hotel();

    Assert.False(Hot.IsOpen); 

}

有人可以帮忙吗?


富国沪深
浏览 177回答 2
2回答

慕标琳琳

你的测试有很多问题:它们是不确定的(正如您已经注意到的),因为它们依赖于时间它们是多余的(第二次测试与第一次测试完全相同)从您输入的语义来看,您似乎需要处理时间而不是完整的DateTime,而您无法表达IsOpen我个人也认为这样一个琐碎的测试是没有用的,因为它比这个测试没有任何价值:[TestFixture]public void ShouldBeOpen(){&nbsp; &nbsp; Assert.True(DateTime.Now <= CLOSING_TIME);}然而,稍微改变一下就IsOpen可以改进您的实现和测试代码,并使您的代码保持简单(无需存根/模拟系统日期):public sealed class Hotel{&nbsp; &nbsp; public bool IsOpenAt(DateTime time) => time.TimeOfDay <= CLOSING_TIME.TimeOfDay;}通过这种真正测试一些业务断言的测试:[TestFixture]public void ShouldBeOpen(){&nbsp; &nbsp; Assert.True(new Hotel().IsOpenAt(CLOSING_TIME));}[TestFixture]public void ShouldNotBeOpen(){&nbsp; &nbsp; Assert.False(new Hotel().IsOpenAt(CLOSING_TIME.AddMinutes(1)));}笔记虽然没有被要求,但我想解决一些问题:CLOSING_TIME。这个字段在我看来很可疑。事实上,我怀疑你设置这个值的方式引入不必要的耦合东西用Hotel。如果不是这种情况,我建议您将此值作为构造函数的参数请求以实现更好的解耦:public sealed class Hotel{&nbsp; &nbsp; private readonly DateTime _closingTime;&nbsp; &nbsp; public Hotel(DateTime closingTime) => _closingTime = closingTime;&nbsp; &nbsp; public bool IsOpenAt(DateTime time) => time.TimeOfDay <= _closingTime.TimeOfDay;}

沧海一幻觉

我将下面的代码放在一起模拟一个可用于单元测试的时间接口。使用它,您可以将时间设置为您指定的实时时间或假时间。我在单元测试时经常使用这种方法。它被称为依赖注入或构造函数注入,这对于单元测试非常有用。&nbsp; &nbsp; class Hotel&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public DateTime ClosingTime = DateTime.ParseExact("17:00:00", "HH:ii:ss", CultureInfo.InvariantCulture);&nbsp; &nbsp; &nbsp; &nbsp; public IStubClock Clock;&nbsp; &nbsp; &nbsp; &nbsp; public bool IsOpen&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Clock.Now.TimeOfDay <= ClosingTime.TimeOfDay;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Hotel(IStubClock clock)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Clock = clock;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }使用此接口,您可以模拟任何 DateTime.Now 结构&nbsp; &nbsp; public interface IStubClock&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DateTime Now { get; }&nbsp; &nbsp; }一个假变种&nbsp; &nbsp; public class FakeClock : IStubClock&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private DateTime _now;&nbsp; &nbsp; &nbsp; &nbsp; public DateTime Now&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _now;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public FakeClock(DateTime now)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _now = now;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }和一个真正的变种&nbsp; &nbsp; public class RealClock : IStubClock&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public DateTime Now&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return DateTime.Now;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }然后你可以在你的测试中使用它们做这样的事情&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IStubClock fakeClock = new FakeClock(new DateTime(1, 1, 1, 10, 0, 0)); //time is set to 10am&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IStubClock realClock = new RealClock(); //time is set to whatever the time now is.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hotel hotel1 = new Hotel(fakeClock); //using fake time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hotel hotel2 = new Hotel(realClock); //using the real time&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP