Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve test from a business point of view (now compares books instea…
…d of just counting them), use specification and split builders for better separation of concerns.
- Loading branch information
1 parent
76a577e
commit eaa522c
Showing
6 changed files
with
131 additions
and
64 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
tests/BookShop.AcceptanceTests/Builders/BookSpecification.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using BookShop.domain; | ||
using BookShop.domain.Catalog; | ||
using Diverse; | ||
|
||
namespace BookShop.AcceptanceTests.Builders; | ||
|
||
public class BookSpecification | ||
{ | ||
public ISBN Isbn { get; } | ||
public Quantity Quantity { get; } | ||
public Uri PictureUrl { get; } | ||
public int NumberOfPages { get; } | ||
public string Author { get; } | ||
public string Title { get; } | ||
public int NumberOfRatings { get; } | ||
public decimal AverageRating { get; } | ||
|
||
public BookSpecification(IFuzz fuzzer) | ||
{ | ||
Isbn = fuzzer.GenerateIsbn10(); | ||
Title = fuzzer.GenerateSentence(6); | ||
var firstName = fuzzer.GenerateFirstName(); | ||
var lastName = fuzzer.GenerateLastName(firstName); | ||
Author = $"{firstName} {lastName}"; | ||
NumberOfPages = fuzzer.GenerateInteger(10, 1500); | ||
PictureUrl = new Uri(fuzzer.GenerateStringFromPattern("http://picture-url-for-tests/xxxxxxx.jpg")); | ||
Quantity = new Quantity(fuzzer.GenerateInteger(1, 100)); | ||
AverageRating = Math.Round(fuzzer.GeneratePositiveDecimal(0m, 5m), 2); | ||
NumberOfRatings = fuzzer.GenerateInteger(2, 20000); | ||
} | ||
|
||
public Book ToBook() | ||
{ | ||
return new Book(new BookReference(Isbn, Title, Author, NumberOfPages, PictureUrl), Quantity); | ||
} | ||
} |
58 changes: 10 additions & 48 deletions
58
tests/BookShop.AcceptanceTests/Builders/CatalogControllerBuilder.cs
This file contains 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
This file contains 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
41 changes: 41 additions & 0 deletions
41
tests/BookShop.AcceptanceTests/Builders/CatalogServiceBuilder.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using BookShop.domain.Catalog; | ||
using NSubstitute; | ||
|
||
namespace BookShop.AcceptanceTests.Builders; | ||
|
||
public class CatalogServiceBuilder(BookSpecification[] booksInCatalog) | ||
{ | ||
public CatalogService Build() | ||
{ | ||
var bookMetadataProvider = StubBookMetadataProvider(); | ||
var inventoryProvider = StubInventoryProvider(); | ||
|
||
return new CatalogService(bookMetadataProvider, inventoryProvider); | ||
} | ||
|
||
private IProvideBookMetadata StubBookMetadataProvider() | ||
{ | ||
var bookMetadataProvider = Substitute.For<IProvideBookMetadata>(); | ||
|
||
bookMetadataProvider.Get() | ||
.Returns(booksInCatalog.Select(book => new BookReference(book.Isbn, book.Title, book.Author, book.NumberOfPages, book.PictureUrl)).ToList()); | ||
|
||
return bookMetadataProvider; | ||
} | ||
|
||
private IProvideInventory StubInventoryProvider() | ||
{ | ||
var inventoryProvider = Substitute.For<IProvideInventory>(); | ||
inventoryProvider.Get(Arg.Any<IEnumerable<BookReference>>()) | ||
.Returns(callInfo => | ||
{ | ||
var requestedBookReferences = callInfo.Arg<IEnumerable<BookReference>>(); | ||
return booksInCatalog | ||
.Where(book => requestedBookReferences.Contains(new BookReference(book.Isbn, book.Title, book.Author, book.NumberOfPages, book.PictureUrl))) | ||
.Select(book => book.ToBook()); | ||
}); | ||
|
||
return inventoryProvider; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/BookShop.AcceptanceTests/Builders/FuzzerExtensions.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using BookShop.domain; | ||
using Diverse; | ||
|
||
namespace BookShop.AcceptanceTests.Builders; | ||
public static class FuzzerExtensions | ||
{ | ||
public static ISBN.ISBN10 GenerateIsbn10(this IFuzz fuzzer) | ||
{ | ||
return new ISBN.ISBN10(fuzzer.GenerateInteger(1, 100), fuzzer.GenerateInteger(1, 10000), fuzzer.GenerateInteger(1, 1000), fuzzer.GenerateInteger(1, 10)); | ||
} | ||
} |
19 changes: 11 additions & 8 deletions
19
tests/BookShop.AcceptanceTests/Builders/MockBookAdvisorHttpHandler.cs
This file contains 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