Refactoring and Adding Tests (#58)

* Moving Identity seeding to its own class and method.

* Adding tests for AddItem

* Added catalog api controller and functional tests
Added and cleaned up some comments

* Adding integration tests for OrderRepository

* Getting integration test for order working with inmemory db
This commit is contained in:
Steve Smith
2017-10-20 12:52:42 -04:00
committed by GitHub
parent 32950aa175
commit 0eb4d72b89
17 changed files with 306 additions and 94 deletions

View File

@@ -0,0 +1,57 @@
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System.Linq;
using Xunit;
namespace UnitTests.ApplicationCore.Entities.BasketTests
{
public class AddItem
{
private int _testCatalogItemId = 123;
private decimal _testUnitPrice = 1.23m;
private int _testQuantity = 2;
[Fact]
public void AddsBasketItemIfNotPresent()
{
var basket = new Basket();
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
var firstItem = basket.Items.Single();
Assert.Equal(_testCatalogItemId, firstItem.CatalogItemId);
Assert.Equal(_testUnitPrice, firstItem.UnitPrice);
Assert.Equal(_testQuantity, firstItem.Quantity);
}
[Fact]
public void IncrementsQuantityOfItemIfPresent()
{
var basket = new Basket();
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
var firstItem = basket.Items.Single();
Assert.Equal(_testQuantity * 2, firstItem.Quantity);
}
[Fact]
public void KeepsOriginalUnitPriceIfMoreItemsAdded()
{
var basket = new Basket();
basket.AddItem(_testCatalogItemId, _testUnitPrice, _testQuantity);
basket.AddItem(_testCatalogItemId, _testUnitPrice * 2, _testQuantity);
var firstItem = basket.Items.Single();
Assert.Equal(_testUnitPrice, firstItem.UnitPrice);
}
[Fact]
public void DefaultsToQuantityOfOne()
{
var basket = new Basket();
basket.AddItem(_testCatalogItemId, _testUnitPrice);
var firstItem = basket.Items.Single();
Assert.Equal(1, firstItem.Quantity);
}
}
}

View File

@@ -0,0 +1,28 @@
using ApplicationCore.Entities.OrderAggregate;
namespace UnitTests.Builders
{
public class AddressBuilder
{
private Address _address;
public string TestStreet => "123 Main St.";
public string TestCity => "Kent";
public string TestState => "OH";
public string TestCountry => "USA";
public string TestZipCode => "44240";
public AddressBuilder()
{
_address = WithDefaultValues();
}
public Address Build()
{
return _address;
}
public Address WithDefaultValues()
{
_address = new Address(TestStreet, TestCity, TestState, TestCountry, TestZipCode);
return _address;
}
}
}

View File

@@ -0,0 +1,38 @@
using ApplicationCore.Entities.OrderAggregate;
using System;
using System.Collections.Generic;
using System.Text;
namespace UnitTests.Builders
{
public class OrderBuilder
{
private Order _order;
public string TestBuyerId => "12345";
public int TestCatalogItemId => 234;
public string TestProductName => "Test Product Name";
public string TestPictureUri => "http://test.com/image.jpg";
public decimal TestUnitPrice = 1.23m;
public int TestUnits = 3;
public CatalogItemOrdered TestCatalogItemOrdered { get; }
public OrderBuilder()
{
TestCatalogItemOrdered = new CatalogItemOrdered(TestCatalogItemId, TestProductName, TestPictureUri);
_order = WithDefaultValues();
}
public Order Build()
{
return _order;
}
public Order WithDefaultValues()
{
var orderItem = new OrderItem(TestCatalogItemOrdered, TestUnitPrice, TestUnits);
var itemList = new List<OrderItem>() { orderItem };
_order = new Order(TestBuyerId, new AddressBuilder().WithDefaultValues(), itemList);
return _order;
}
}
}