Skip to content

Instantly share code, notes, and snippets.

@Kevin-Bronsdijk
Last active August 15, 2020 15:22
Show Gist options
  • Save Kevin-Bronsdijk/f887a6f05ee1f0da1ae3529d5766b944 to your computer and use it in GitHub Desktop.
Save Kevin-Bronsdijk/f887a6f05ee1f0da1ae3529d5766b944 to your computer and use it in GitHub Desktop.
[Test]
public void Example1_GetProducts_WhenPassingAValidCategory_ThenShouldReturnProducts()
{
// arrange act assert
Assert.IsTrue(GetProducts_WhenPassingAValidCategoryAndTheRepositoryIsAbleToReturnProducts_ThenShouldReturnProducts());
}
[Test]
public void Example2_GetProducts_WhenPassingAValidCategory_ThenShouldReturnProducts()
{
// setup repository mock
var products = new List<Product>
{
new Product { Id = 1, Name = "Product 1" },
new Product { Id = 2, Name = "Product 2" }
};
IProductCatalogRepository repository = Substitute.For<IProductCatalogRepository>();
repository.GetProducts(Arg.Is("ElectronicDevices")).Returns(products);
// create object under test
var testSubject = new ProductCatalogService(repository);
// assert
var result = testSubject.GetProducts(Category.ElectronicDevices);
Assert.IsNotNull(result);
Assert.IsTrue(result.ToList().Count != 0);
}
[Test]
public void Example3_GetProducts_WhenPassingAValidCategory_ThenShouldReturnProducts()
{
Given.AConcreteProductCatalogService.WhenBasedOnScenario(
Given.AProductCatalogRepository.WhenReturningProductsForCategory("ElectronicDevices"))
.When().GetProducts(Category.ElectronicDevices)
.Then().Should().NotBeNull().And.HaveCountGreaterThan(0);
}
// using assert
var expected = true;
Assert.AreEqual(expected, actual);
// using Fluent Assertions
result.Should().BeTrue();
public static class Given
{
public static IProductCatalogRepository AProductCatalogRepository => Substitute.For<IProductCatalogRepository>();
public static ProductCatalogService AConcreteProductCatalogService => new ProductCatalogService(null);
public static List<Product> AConcreteProductList => new List<Product>();
}
//Create:
var calculator = Substitute.For<ICalculator>();
//Set a return value:
calculator.Add(1, 2).Returns(3);
Assert.AreEqual(3, calculator.Add(1, 2));
//Check received calls:
calculator.Received().Add(1, Arg.Any<int>());
calculator.DidNotReceive().Add(2, 2);
[Test]
public void Example3_GetProducts_WhenPassingAValidCategory_ThenShouldReturnProducts()
{
Given.AConcreteProductCatalogService.BasedOnScenario(
Given.AProductCatalogRepository.ThatReturnsProductsForCategory("ElectronicDevices"))
.When().GetProducts(Category.ElectronicDevices)
.Then().TheResult().Should().NotBeNull().And.HaveCountGreaterThan(0);
}
public static class ScenarioHelper
{
public static T When<T>(this T @this)
{
return @this;
}
public static T Then<T>(this T @this)
{
return @this;
}
//...
}
public static class Scenarios
{
public static IProductCatalogRepository ThatReturnsProductsForCategory(this IProductCatalogRepository @this, string category)
{
@this.GetProducts(Arg.Is(category)).Returns(Given.AConcreteProductList.ThatReturnsTwoProducts());
return @this;
}
public static ProductCatalogService BasedOnScenario(this ProductCatalogService @this, IProductCatalogRepository repository)
{
return new ProductCatalogService(repository);
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment