Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
  }
}
like image 855
Deepak Raj Avatar asked Apr 02 '14 07:04

Deepak Raj


2 Answers

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.

like image 60
Kamil Budziewski Avatar answered Sep 28 '22 17:09

Kamil Budziewski


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

like image 32
amp Avatar answered Sep 28 '22 17:09

amp