Skip to content

Instantly share code, notes, and snippets.

@darrencauthon
Created August 3, 2016 11:14
Show Gist options
  • Save darrencauthon/416828d609ac1195a247a7e4f7ebbea8 to your computer and use it in GitHub Desktop.
Save darrencauthon/416828d609ac1195a247a7e4f7ebbea8 to your computer and use it in GitHub Desktop.
Testing each usage of ?.
void Main()
{
It_should_return_the_name_from_the_person_on_the_invoice_on_the_order();
It_should_return_nothing_if_the_person_is_not_set();
It_should_return_nothing_if_the_invoice_is_not_set();
It_should_return_nothing_if_given_nothing();
}
public static string GetNameFrom(Order order)
{
return order?.Invoice?.Person?.Name;
}
public static void It_should_return_the_name_from_the_person_on_the_invoice_on_the_order()
{
var name = Guid.NewGuid().ToString();
var order = new Order { Invoice = new Invoice { Person = new Person { Name = name } } };
GetNameFrom(order).ShouldEqual(name);
}
public static void It_should_return_nothing_if_the_person_is_not_set()
{
var order = new Order { Invoice = new Invoice { Person = null } };
GetNameFrom(order).ShouldBeNull();
}
public void It_should_return_nothing_if_the_invoice_is_not_set()
{
var order = new Order { Invoice = null };
GetNameFrom(order).ShouldBeNull();
}
public void It_should_return_nothing_if_given_nothing()
{
Order order = null;
GetNameFrom(order).ShouldBeNull();
}
public class Order
{
public Invoice Invoice { get; set; }
}
public class Invoice
{
public Person Person { get; set; }
}
public class Person
{
public string Name { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment