简体   繁体   English

在单元测试中,如何确定结果是否为Guid?

[英]In unit testing, how to Assert if result is Guid?

I am working on unit testing using visual studio unit test framework 我正在使用Visual Studio单元测试框架进行单元测试

In my unit test method, I want to assert if the result is a Guid like 在我的单元测试方法中,我想断言结果是否是类似的Guid

3C99A192-9844-4174-AC32-91976A5F2CBF . 3C99A192-9844-4174-AC32-91976A5F2CBF

Currently, I have come up with this. 目前,我已经提出了这个。 But I am sure there will be a better way to handle this. 但是我相信会有更好的方法来解决这个问题。

[TestMethod]
public void CreateAppointment_Should_Return_Guid()
{
  string result = CreateAppointment();
  Guid guidResult;
  if (Guid.TryParse(result.GuestId, guidResult))
  {
    Assert.IsTrue(true);
  }
  else
  {
    Assert.IsTrue(false);
  }
}

Why not shorter one? 为什么不矮一个? TryParse returns bool. TryParse返回布尔值。

Guid guidResult;
Assert.IsTrue(Guid.TryParse(result.GuestId, out guidResult));

your idea seems to be legit. 您的想法似乎合法。 You are checking if string parses to guid, so you can tell if string is valid guid. 您正在检查字符串是否解析为guid,因此可以判断字符串是否为有效的guid。

这可能会有所帮助:Assert.IsInstanceOfType(CreateAppointment(),typeof(Guid));

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

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