Skip to content

Instantly share code, notes, and snippets.

@bariloce
Last active August 29, 2015 14:01
Show Gist options
  • Save bariloce/f4b4135e18a2665ae1f9 to your computer and use it in GitHub Desktop.
Save bariloce/f4b4135e18a2665ae1f9 to your computer and use it in GitHub Desktop.
Learning factory - what should I change in my unit tests? Is it ok?
public abstract class CarFactory
{
public abstract Car CreateSportsCar();
public abstract Car CreateFamilyCar();
}
public abstract class Car
{
public abstract string CompareSpeed(Car car);
}
public class MercedesFactory : CarFactory
{
public override Car CreateSportsCar()
{
return new MercedesSportsCar();
}
public override Car CreateFamilyCar()
{
return new MercedesFamilyCar();
}
}
public class AudiFactory : CarFactory
{
public override Car CreateSportsCar()
{
return new AudiSportsCar();
}
public override Car CreateFamilyCar()
{
return new AudiFamilyCar();
}
}
public class AudiSportsCar : Car
{
public override string CompareSpeed(Car familyCar)
{
return this.GetType().Name + " is faster than " + familyCar.GetType().Name;
}
}
public class AudiFamilyCar : Car
{
public override string CompareSpeed(Car sportsCar)
{
return this.GetType().Name + " is slower than " + sportsCar.GetType().Name;
}
}
public class MercedesSportsCar : Car
{
public override string CompareSpeed(Car familyCar)
{
return this.GetType().Name + " is faster than " + familyCar.GetType().Name;
}
}
public class MercedesFamilyCar : Car
{
public override string CompareSpeed(Car sportsCar)
{
return this.GetType().Name + " is slower than " + sportsCar.GetType().Name;
}
}
public class Driver
{
readonly Car sportsCar;
readonly Car familyCar;
public Driver(CarFactory carFactory)
{
sportsCar = carFactory.CreateSportsCar();
familyCar = carFactory.CreateFamilyCar();
}
public string CompareSpeeds()
{
return familyCar.CompareSpeed(sportsCar);
}
}
public class Tests
{
[Fact]
public void ShouldCreateNotNullObjectMercedesFamilyCarFromGenericFactory()
{
var car = new MercedesFamilyCar();
Assert.NotNull(car);
}
[Fact]
public void ShouldCompareSpeedOfMercedesSportsAndFamilyCars()
{
var factory = new MercedesFactory();
var driver = new Driver(factory);
Assert.Equal("MercedesFamilyCar is slower than MercedesSportsCar", driver.CompareSpeeds());
}
[Fact]
public void ShouldCompareSpeedOfAudiSportsAndFamilyCars()
{
var factory = new AudiFactory();
var driver = new Driver(factory);
Assert.Equal("AudiFamilyCar is slower than AudiSportsCar", driver.CompareSpeeds());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment