Last active
August 15, 2020 15:22
-
-
Save Kevin-Bronsdijk/f887a6f05ee1f0da1ae3529d5766b944 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void Example1_GetProducts_WhenPassingAValidCategory_ThenShouldReturnProducts() | |
{ | |
// arrange act assert | |
Assert.IsTrue(GetProducts_WhenPassingAValidCategoryAndTheRepositoryIsAbleToReturnProducts_ThenShouldReturnProducts()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void Example3_GetProducts_WhenPassingAValidCategory_ThenShouldReturnProducts() | |
{ | |
Given.AConcreteProductCatalogService.WhenBasedOnScenario( | |
Given.AProductCatalogRepository.WhenReturningProductsForCategory("ElectronicDevices")) | |
.When().GetProducts(Category.ElectronicDevices) | |
.Then().Should().NotBeNull().And.HaveCountGreaterThan(0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using assert | |
var expected = true; | |
Assert.AreEqual(expected, actual); | |
// using Fluent Assertions | |
result.Should().BeTrue(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ScenarioHelper | |
{ | |
public static T When<T>(this T @this) | |
{ | |
return @this; | |
} | |
public static T Then<T>(this T @this) | |
{ | |
return @this; | |
} | |
//... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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