Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Setup testdata
1. A Better Way to Setup
Test Data
Poya Manouchehri
@PoyaManouchehri
poya.manouchehri@readify.net
http://askpoya.wordpress.com/
2. 1. Intent is Unclear:
• What is 160?
• Is nationality important?
2. Test is Brittle:
• Changing the constructor will break many tests
[Test]
public void GivenAPersonUnder16_WhenAssigningADriverLicense_ThenAnExceptionIsThrown()
{
// Arrange
var sarah = new Person("Sarah", "Smith", DateTime.Today.AddYears(-15), 160, "Australian");
// Act / Assert
Assert.Throws<InvalidOperationException>(() =>
sarah.AssignDriverLicense(DriverLicenseType.Automatic));
}
3. public static partial class ObjectMother
{
public static class Persons
{
public static Person FifteenYearOldSarah
{
get
{
return new Person("Sarah", "Smith", DateTime.Today.AddYears(-17), 160, "Australian");
}
}
public static Person BrazilianRafael
{
get
{
return new Person("Rafael", "Gomez", new DateTime(1985, 12 , 2), 177, “Brazilian")
}
}
}
}
Object Mother Pattern
8. var people = PersonBuilder.CreateListOfSize(10)
.All().Do(p => p.WithNationality("Australian"))
.TheFirst(2).Do(p => p.BornOn(new DateTime(1980, 1, 1)))
.TheNext(3).Do(p => p.BornOn(new DateTime(1974, 10, 15)))
.Random(2).Do(p => p.WithHeight(180))
.BuildList<Person, PersonBuilder>();
Building Collections with NBuilder
var people = new[]
{
new PersonBuilder().WithNationality("Australian").BornOn(new DateTime(1980, 1, 1)).Build(),
new PersonBuilder().WithNationality("Australian").BornOn(new DateTime(1980, 1, 1))
.WithHeight(180).Build(),
new PersonBuilder().WithNationality("Australian").BornOn(new DateTime(1974, 10, 15)).Build(),
new PersonBuilder().WithNationality("Australian").BornOn(new DateTime(1974, 10, 15))
.WithHeight(180).Build(),
new PersonBuilder().WithNationality("Australian").BornOn(new DateTime(1974, 10, 15)).Build()
};
9. public class TransportCentre
{
...
public void PerformTruckDrivingTest(Person person)
{
// Do the test
person.AssignDriverLicense(DriverLicenseType.Truck);
}
}
Building Mocks with NSubstitute
10. [Test]
public void GivenAPerson_WhenTruckDrivingTestIsCompleted_ThenThePersonIsAssignedALicense()
{
var person = ObjectMother.Persons.John.Build();
var transportCentre = ObjectMother.TransportCentres.BrisbaneCityCentre.Build();
transportCentre.PerformTruckDrivingTest(person);
Assert.That(person.DriverLicenseType, Is.EqualTo(DriverLicenseType.Truck));
}
What’s the actual “Unit” under test?
• PerformTruckDrivingTest()
• Not Person.AssignDriverLicense()
Building Mocks with NSubstitute
11. [Test]
public void GivenAPerson_WhenTruckDrivingTestIsCompleted_ThenThePersonIsAssignedALicense()
{
var person = ObjectMother.Persons.John.AsProxy().Build();
var transportCentre = ObjectMother.TransportCentres.BrisbaneCityCentre.Build();
transportCentre.PerformTruckDrivingTest(person);
person.Received().AssignDriverLicense(DriverLicenseType.Truck);
}
Building Mocks with NSubstitute
What’s the actual “Unit” under test?
• PerformTruckDrivingTest()
• Not Person.AssignDriverLicense()
12. Building Mocks with NSubstitute
Caveats:
• Requires a protected parameteress constructor
• Requires the virtual keyword for any properties or methods that need
to be mocked