Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15e7e95
commit 9c3aefc
Showing
7 changed files
with
267 additions
and
8 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -245,6 +245,5 @@ ModelManifest.xml | |
.fake/ | ||
/src/.idea | ||
|
||
tests/ | ||
.vscode/ | ||
.idea/ |
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
101 changes: 101 additions & 0 deletions
101
Chapter13/tests/Marketplace.Ads.Tests/ClassifiedAd_Publish_Spec.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,101 @@ | ||
using System; | ||
using Marketplace.Ads.Domain.ClassifiedAds; | ||
using Marketplace.Ads.Domain.Shared; | ||
using Xunit; | ||
|
||
namespace Marketplace.ClassifiedAds.Tests | ||
{ | ||
public class ClassifiedAd_Publish_Spec | ||
{ | ||
public ClassifiedAd_Publish_Spec() | ||
=> Ad = ClassifiedAd.Create( | ||
ClassifiedAdId.FromGuid(Guid.NewGuid()), | ||
UserId.FromGuid(Guid.NewGuid()) | ||
); | ||
|
||
ClassifiedAd Ad { get; } | ||
|
||
[Fact] | ||
public void Can_publish_a_valid_ad() | ||
{ | ||
Ad.SetTitle(ClassifiedAdTitle.FromString("Test ad")); | ||
|
||
Ad.UpdateText( | ||
ClassifiedAdText.FromString("Please buy my stuff") | ||
); | ||
|
||
Ad.UpdatePrice( | ||
Price.FromDecimal(100.10m, "EUR", new FakeCurrencyLookup()) | ||
); | ||
|
||
Ad.RequestToPublish(); | ||
|
||
Assert.Equal( | ||
ClassifiedAd.ClassifiedAdState.PendingReview, | ||
Ad.State | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Cannot_publish_with_zero_price() | ||
{ | ||
Ad.SetTitle(ClassifiedAdTitle.FromString("Test ad")); | ||
|
||
Ad.UpdateText( | ||
ClassifiedAdText.FromString("Please buy my stuff") | ||
); | ||
|
||
Ad.UpdatePrice( | ||
Price.FromDecimal(0.0m, "EUR", new FakeCurrencyLookup()) | ||
); | ||
|
||
Assert.Throws<DomainExceptions.InvalidEntityState>( | ||
() => Ad.RequestToPublish() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Cannot_publish_without_price() | ||
{ | ||
Ad.SetTitle(ClassifiedAdTitle.FromString("Test ad")); | ||
|
||
Ad.UpdateText( | ||
ClassifiedAdText.FromString("Please buy my stuff") | ||
); | ||
|
||
Assert.Throws<DomainExceptions.InvalidEntityState>( | ||
() => Ad.RequestToPublish() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Cannot_publish_without_text() | ||
{ | ||
Ad.SetTitle(ClassifiedAdTitle.FromString("Test ad")); | ||
|
||
Ad.UpdatePrice( | ||
Price.FromDecimal(100.10m, "EUR", new FakeCurrencyLookup()) | ||
); | ||
|
||
Assert.Throws<DomainExceptions.InvalidEntityState>( | ||
() => Ad.RequestToPublish() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Cannot_publish_without_title() | ||
{ | ||
Ad.UpdateText( | ||
ClassifiedAdText.FromString("Please buy my stuff") | ||
); | ||
|
||
Ad.UpdatePrice( | ||
Price.FromDecimal(100.10m, "EUR", new FakeCurrencyLookup()) | ||
); | ||
|
||
Assert.Throws<DomainExceptions.InvalidEntityState>( | ||
() => Ad.RequestToPublish() | ||
); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
Chapter13/tests/Marketplace.Ads.Tests/FakeCurrencyLookup.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,44 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Marketplace.Ads.Domain.Shared; | ||
|
||
namespace Marketplace.ClassifiedAds.Tests | ||
{ | ||
public class FakeCurrencyLookup : ICurrencyLookup | ||
{ | ||
static readonly IEnumerable<Currency> _currencies = | ||
new[] | ||
{ | ||
new Currency | ||
{ | ||
CurrencyCode = "EUR", | ||
DecimalPlaces = 2, | ||
InUse = true | ||
}, | ||
new Currency | ||
{ | ||
CurrencyCode = "USD", | ||
DecimalPlaces = 2, | ||
InUse = true | ||
}, | ||
new Currency | ||
{ | ||
CurrencyCode = "JPY", | ||
DecimalPlaces = 0, | ||
InUse = true | ||
}, | ||
new Currency | ||
{ | ||
CurrencyCode = "DEM", | ||
DecimalPlaces = 2, | ||
InUse = false | ||
} | ||
}; | ||
|
||
public Currency FindCurrency(string currencyCode) | ||
{ | ||
var currency = _currencies.FirstOrDefault(x => x.CurrencyCode == currencyCode); | ||
return currency ?? Currency.None; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Chapter13/tests/Marketplace.Ads.Tests/Marketplace.Ads.Tests.csproj
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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
<LangVersion>latest</LangVersion> | ||
<IsPackable>false</IsPackable> | ||
<RootNamespace>Marketplace.ClassifiedAds.Tests</RootNamespace> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" /> | ||
<PackageReference Include="Shouldly" Version="3.0.2" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.4.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Marketplace.Ads.Domain\Marketplace.Ads.Domain.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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,96 @@ | ||
using System; | ||
using Marketplace.Ads.Domain.Shared; | ||
using Xunit; | ||
|
||
namespace Marketplace.ClassifiedAds.Tests | ||
{ | ||
public class Money_Spec | ||
{ | ||
static readonly ICurrencyLookup CurrencyLookup = | ||
new FakeCurrencyLookup(); | ||
|
||
[Fact] | ||
public void FromString_and_FromDecimal_should_be_equal() | ||
{ | ||
var firstAmount = Money.FromDecimal(5, "EUR", CurrencyLookup); | ||
var secondAmount = Money.FromString("5.00", "EUR", CurrencyLookup); | ||
|
||
Assert.Equal(firstAmount, secondAmount); | ||
} | ||
|
||
[Fact] | ||
public void Sum_of_money_gives_full_amount() | ||
{ | ||
var coin1 = Money.FromDecimal(1, "EUR", CurrencyLookup); | ||
var coin2 = Money.FromDecimal(2, "EUR", CurrencyLookup); | ||
var coin3 = Money.FromDecimal(2, "EUR", CurrencyLookup); | ||
|
||
var banknote = Money.FromDecimal(5, "EUR", CurrencyLookup); | ||
|
||
Assert.Equal(banknote, coin1 + coin2 + coin3); | ||
} | ||
|
||
[Fact] | ||
public void Throw_when_too_many_decimal_places() | ||
=> Assert.Throws<ArgumentOutOfRangeException>( | ||
() => | ||
Money.FromDecimal(100.123m, "EUR", CurrencyLookup) | ||
); | ||
|
||
[Fact] | ||
public void Throws_on_adding_different_currencies() | ||
{ | ||
var firstAmount = Money.FromDecimal(5, "USD", CurrencyLookup); | ||
var secondAmount = Money.FromDecimal(5, "EUR", CurrencyLookup); | ||
|
||
Assert.Throws<CurrencyMismatchException>( | ||
() => | ||
firstAmount + secondAmount | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Throws_on_substracting_different_currencies() | ||
{ | ||
var firstAmount = Money.FromDecimal(5, "USD", CurrencyLookup); | ||
var secondAmount = Money.FromDecimal(5, "EUR", CurrencyLookup); | ||
|
||
Assert.Throws<CurrencyMismatchException>( | ||
() => | ||
firstAmount - secondAmount | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Two_of_same_amount_but_different_Currencies_should_not_be_equal() | ||
{ | ||
var firstAmount = Money.FromDecimal(5, "EUR", CurrencyLookup); | ||
var secondAmount = Money.FromDecimal(5, "USD", CurrencyLookup); | ||
|
||
Assert.NotEqual(firstAmount, secondAmount); | ||
} | ||
|
||
[Fact] | ||
public void Two_of_same_amount_should_be_equal() | ||
{ | ||
var firstAmount = Money.FromDecimal(5, "EUR", CurrencyLookup); | ||
var secondAmount = Money.FromDecimal(5, "EUR", CurrencyLookup); | ||
|
||
Assert.Equal(firstAmount, secondAmount); | ||
} | ||
|
||
[Fact] | ||
public void Unknown_currency_should_not_be_allowed() | ||
=> Assert.Throws<ArgumentException>( | ||
() => | ||
Money.FromDecimal(100, "WHAT?", CurrencyLookup) | ||
); | ||
|
||
[Fact] | ||
public void Unused_currency_should_not_be_allowed() | ||
=> Assert.Throws<ArgumentException>( | ||
() => | ||
Money.FromDecimal(100, "DEM", CurrencyLookup) | ||
); | ||
} | ||
} |