Вопрос

I am working on unit testing using visual studio unit test framework

In my unit test method, I want to assert if the result is a Guid like

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.

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.

Другие советы

This might help: Assert.IsInstanceOfType(CreateAppointment(), typeof(Guid));

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top