From 6dcb6d79954874bdccf1854f07ceec8888ccc9c5 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 7 May 2020 18:59:45 -0700 Subject: [PATCH] created spin win avg and test --- LuckySpin.Test/SpinServiceTest.cs | 16 +++++++--------- LuckySpin/Controllers/SpinnerController.cs | 2 +- LuckySpin/Services/ISpinService.cs | 2 +- LuckySpin/Services/SpinService.cs | 18 +++++++++++++++--- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/LuckySpin.Test/SpinServiceTest.cs b/LuckySpin.Test/SpinServiceTest.cs index f07f959..7c096e8 100644 --- a/LuckySpin.Test/SpinServiceTest.cs +++ b/LuckySpin.Test/SpinServiceTest.cs @@ -10,29 +10,27 @@ namespace LuckySpin.Test { public class SpinServiceTest { - [Fact] + [Fact]//bonus public void SpinService_CalculateAvgWins_WinningSpin() { //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()); // calculated based + mockRepo.Setup(r => r.GetCount()).Returns(SpinListData.GetCount()); var service = new SpinService(mockRepo.Object); //Act - run the method that you are testing and get a result - double result = service.CalculateAvgWins(); + bool isWinning = true; + double result = service.CalculateAvgWins(isWinning); //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 wins = 4+1/*???*/, count = SpinListData.GetCount() + 1 /*???*/; double expected = wins / count; Assert.Equal(expected, result); - } - [Fact] //BONUS: Refactor into a Theory that tests both a winning and losing spin - public void SpinService_CalculateAvgWins_LosingSpin() - { - - } + } } } diff --git a/LuckySpin/Controllers/SpinnerController.cs b/LuckySpin/Controllers/SpinnerController.cs index 92234c6..42a04d4 100644 --- a/LuckySpin/Controllers/SpinnerController.cs +++ b/LuckySpin/Controllers/SpinnerController.cs @@ -62,7 +62,7 @@ public IActionResult SpinIt(int luck) Spin spin = spinService.SpinIt(Luck); //Compute the average wins - spin.AverageWins = spinService.CalculateAvgWins(); + spin.AverageWins = spinService.CalculateAvgWins(spin.IsWinning); //Add to Spin Repository spinRepository.AddSpin(spin); diff --git a/LuckySpin/Services/ISpinService.cs b/LuckySpin/Services/ISpinService.cs index 8b577ae..2d9d9a4 100644 --- a/LuckySpin/Services/ISpinService.cs +++ b/LuckySpin/Services/ISpinService.cs @@ -4,7 +4,7 @@ namespace LuckySpin.Services { public interface ISpinService { - public Double CalculateAvgWins(); + public Double CalculateAvgWins(bool isWinning); public Spin SpinIt(int Lucky); } } diff --git a/LuckySpin/Services/SpinService.cs b/LuckySpin/Services/SpinService.cs index 9ad3c3d..0a3725d 100644 --- a/LuckySpin/Services/SpinService.cs +++ b/LuckySpin/Services/SpinService.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using LuckySpin.Models; using LuckySpin.Repositories; namespace LuckySpin.Services @@ -12,14 +13,25 @@ public class SpinService : ISpinService //Makes this class extend the Interface public SpinService(ISpinRepository sr) { spinRepository = sr; - } - public double CalculateAvgWins() + public double CalculateAvgWins(bool isWinning) { //TODO: Write logic to use the "real" spinRepository NOT the test data - return .1; + double winningCount = 0; + double spinCounts = spinRepository.GetCount() + 1; + + if (isWinning) + winningCount = 1; + + foreach (Spin spin in spinRepository.GetSpins()) + { + if (spin.IsWinning) + winningCount++; + } + + return winningCount / spinCounts; } public Spin SpinIt(int luck)