diff --git a/LuckySpin.Test/SpinServiceTest.cs b/LuckySpin.Test/SpinServiceTest.cs index a5441c7..cf2b6c4 100644 --- a/LuckySpin.Test/SpinServiceTest.cs +++ b/LuckySpin.Test/SpinServiceTest.cs @@ -10,23 +10,38 @@ namespace LuckySpin.Test { public class SpinServiceTest { - [Fact] //BONUS: Refactor into a Theory that tests both a winning and losing spin - public void SpinService_CalculateAvgWins_WinningSpin() + [Theory] //BONUS: Refactor into a Theory that tests both a winning and losing spin + [InlineData(true)] + [InlineData(false)] + public void SpinService_CalculateAvgWins_WinningSpin(bool isWinning) { //Arrange - create your Mock elements; setup the mockRepo to return TestData var mockRepo = new Mock(); //TODO: Use the Setup() and Returns() methods of mockRepo // to arrange for a consistent, expected output based on TestData + mockRepo.Setup(r => r.GetSpins()).Returns(SpinListData.GetSpins()); + mockRepo.Setup(r => r.GetCount()).Returns(SpinListData.GetCount()); + var service = new SpinService(mockRepo.Object); + service.IsWinning = isWinning; //Act - run the method that you are testing and get a result double result = service.CalculateAvgWins(); //Assert - compare the expected output from TestData to the method result // TODO: check the repo data for the number of previous spins and wins, add one winning spin - double wins = 1/*???*/, count=1/*???*/; - double expected = wins / count; + double count = 10 + 1; + double wins; + if (isWinning) + { + wins = 4 + 1; + } + else + { + wins = 4; + } + double expected = wins / count; Assert.Equal(expected, result); } } diff --git a/LuckySpin/Services/SpinService.cs b/LuckySpin/Services/SpinService.cs index 9ad3c3d..5b505a3 100644 --- a/LuckySpin/Services/SpinService.cs +++ b/LuckySpin/Services/SpinService.cs @@ -1,12 +1,14 @@ using System; +using System.Linq; using LuckySpin.Models; using LuckySpin.Repositories; + namespace LuckySpin.Services { public class SpinService : ISpinService //Makes this class extend the Interface ISpinService { Random random = new Random(); - + public bool IsWinning; private ISpinRepository spinRepository; //Constructor with Dependency Injection public SpinService(ISpinRepository sr) @@ -19,7 +21,9 @@ public SpinService(ISpinRepository sr) public double CalculateAvgWins() { //TODO: Write logic to use the "real" spinRepository NOT the test data - return .1; + double wins = spinRepository.GetSpins().Count(s => s.IsWinning) + (IsWinning ? 1 : 0); + double spins = spinRepository.GetCount() + 1; + return wins / spins; } public Spin SpinIt(int luck) @@ -28,13 +32,13 @@ public Spin SpinIt(int luck) a = random.Next(1, 10); b = random.Next(1, 10); c = random.Next(1, 10); - + IsWinning = (a == luck || b == luck || c == luck); return new Spin() { A = a, B = b, C = c, - IsWinning = (a == luck || b == luck || c == luck), + IsWinning = IsWinning, Luck = luck }; }