Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions GettingStartedWithInterfacesAndMocks/DiceGame/Entities/Die.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using GettingStartedWithInterfacesAndMocks.DiceGame.Extensions;
using GettingStartedWithInterfacesAndMocks.DiceGame.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;

namespace GettingStartedWithInterfacesAndMocks.DiceGame.Entities
{
public class Die : IDie
{
public Die()
{
Roll();
}

public int Value { get; private set; }
public void Roll()
{
Value = DieRoller.GetD6Roll();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using GettingStartedWithInterfacesAndMocks.DiceGame.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;

namespace GettingStartedWithInterfacesAndMocks.DiceGame.Extensions
{
public static class DieExtensions
{
public static bool IsValidRoll(this IEnumerable<IDie> dice)
{
return dice.Count() == 5;
}

public static bool IsFiveOfAKind(this IEnumerable<IDie> dice)
{
if (!dice.IsValidRoll())
{
throw new ArgumentException("A valid roll must contain exactly five dice");
}

return dice.GroupBy(d => d.Value).Count() == 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using GettingStartedWithInterfacesAndMocks.DiceGame.Entities;
using System;
using System.Collections.Generic;
using System.Linq;

namespace GettingStartedWithInterfacesAndMocks.DiceGame.Extensions
{
public static class DieRoller
{
static Random _rng = new Random();

public static int GetD6Roll()
{
return _rng.Next(1, 7);
}

public static IEnumerable<Die> GetGameRoll()
{
return Enumerable.Range(1, 5).Select(_ => new Die());
}
}
}
10 changes: 10 additions & 0 deletions GettingStartedWithInterfacesAndMocks/DiceGame/Interfaces/IDie.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace GettingStartedWithInterfacesAndMocks.DiceGame.Interfaces
{
public interface IDie
{
void Roll();
int Value { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
66 changes: 66 additions & 0 deletions GettingStartedWithInterfacesAndMocksTests/DieExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using FluentAssertions;
using GettingStartedWithInterfacesAndMocks.DiceGame.Entities;
using GettingStartedWithInterfacesAndMocks.DiceGame.Extensions;
using GettingStartedWithInterfacesAndMocksTests.TestingMocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace GettingStartedWithInterfacesAndMocksTests
{
[TestClass]
public class DieExtensionsTests
{
[TestMethod]
public void IsValidRoll_RollHas4Dice_ReturnsFalse()
{
var dice = Enumerable.Range(1, 4).Select(_ => new Die());

dice.IsValidRoll().Should().BeFalse(because: "A valid game roll has five dice");
}

[TestMethod]
public void IsValidRoll_RollHas5Dice_ReturnsTrue()
{
var dice = Enumerable.Range(1, 5).Select(_ => new Die());

dice.IsValidRoll().Should().BeTrue(because: "A valid game roll has five dice");
}

[TestMethod]
public void IsFiveOfAKind_AllDiceAreTheSame_ReturnsTrue()
{
var dice = Enumerable.Range(1, 5).Select(_ => new SettableD6(5));

dice.IsFiveOfAKind().Should().BeTrue();
}

[TestMethod]
public void IsFiveOfAKind_AllDiceAreNotTheSame_ReturnsFalse()
{
var dice = new[]{
new SettableD6(1),
new SettableD6(3),
new SettableD6(4),
new SettableD6(1),
new SettableD6(1)
};

dice.IsFiveOfAKind().Should().BeFalse();
}

[TestMethod]
public void IsFiveOfAKind_NotAValidRoll_ThrowsArgumentException()
{
var dice = new[]{
new SettableD6(2),
new SettableD6(3)
};

Action validateRoll = () => dice.IsFiveOfAKind();

validateRoll.Should().Throw<ArgumentException>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GettingStartedWithInterfacesAndMocks\GettingStartedWithInterfacesAndMocks.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using GettingStartedWithInterfacesAndMocks.DiceGame.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;

namespace GettingStartedWithInterfacesAndMocksTests.TestingMocks
{
public class SettableD6 : IDie
{
public SettableD6(int value)
{
Value = value;
}

public int Value { get; set; }

public void Roll()
{
//Does nothing
}
}
}
22 changes: 17 additions & 5 deletions TestingPracticesWithExamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29609.76
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GettingStarted", "GettingStarted\GettingStarted.csproj", "{AAB66502-A5F2-40D8-BCB4-F7EDB0923640}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "InterfacesAndMocks", "InterfacesAndMocks", "{42786B9F-B4BA-4D56-8104-D359C5E0E917}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GettingStartedWithInterfacesAndMocks", "GettingStartedWithInterfacesAndMocks\GettingStartedWithInterfacesAndMocks.csproj", "{22E941F6-26EC-47CE-84A7-0456BF6EF2A1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GettingStartedWithInterfacesAndMocksTests", "GettingStartedWithInterfacesAndMocksTests\GettingStartedWithInterfacesAndMocksTests.csproj", "{7BE9A85F-1902-4448-8023-9DC5EF821EE7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AAB66502-A5F2-40D8-BCB4-F7EDB0923640}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAB66502-A5F2-40D8-BCB4-F7EDB0923640}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAB66502-A5F2-40D8-BCB4-F7EDB0923640}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAB66502-A5F2-40D8-BCB4-F7EDB0923640}.Release|Any CPU.Build.0 = Release|Any CPU
{22E941F6-26EC-47CE-84A7-0456BF6EF2A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{22E941F6-26EC-47CE-84A7-0456BF6EF2A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22E941F6-26EC-47CE-84A7-0456BF6EF2A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{22E941F6-26EC-47CE-84A7-0456BF6EF2A1}.Release|Any CPU.Build.0 = Release|Any CPU
{7BE9A85F-1902-4448-8023-9DC5EF821EE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7BE9A85F-1902-4448-8023-9DC5EF821EE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BE9A85F-1902-4448-8023-9DC5EF821EE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7BE9A85F-1902-4448-8023-9DC5EF821EE7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{22E941F6-26EC-47CE-84A7-0456BF6EF2A1} = {42786B9F-B4BA-4D56-8104-D359C5E0E917}
{7BE9A85F-1902-4448-8023-9DC5EF821EE7} = {42786B9F-B4BA-4D56-8104-D359C5E0E917}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BA44BE1E-A335-4CCF-9ACF-D80E199328B3}
EndGlobalSection
Expand Down