From 4e334898cd1e914979bb738377ce98b32cc4d403 Mon Sep 17 00:00:00 2001 From: BlankaPeller Date: Sun, 1 May 2022 12:31:56 -0700 Subject: [PATCH] The test passes --- LuckySpin.Test/SpinServiceTest.cs | 14 ++++++++------ LuckySpin/Controllers/SpinnerController.cs | 16 ++++++++-------- LuckySpin/LuckySpin.csproj | 4 ++++ LuckySpin/Services/ISpinService.cs | 4 ++-- LuckySpin/Services/SpinService.cs | 17 ++++++++++++----- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/LuckySpin.Test/SpinServiceTest.cs b/LuckySpin.Test/SpinServiceTest.cs index f07f959..5539681 100644 --- a/LuckySpin.Test/SpinServiceTest.cs +++ b/LuckySpin.Test/SpinServiceTest.cs @@ -17,22 +17,24 @@ public void SpinService_CalculateAvgWins_WinningSpin() 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); //Act - run the method that you are testing and get a result - double result = service.CalculateAvgWins(); + double result = service.CalculateAvgWins(true); //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 = 5, count = 11/*???*/; double expected = wins / count; Assert.Equal(expected, result); } - [Fact] //BONUS: Refactor into a Theory that tests both a winning and losing spin + /*[Fact] //BONUS: Refactor into a Theory that tests both a winning and losing spin public void SpinService_CalculateAvgWins_LosingSpin() { - - } + + } */ } -} +} \ No newline at end of file diff --git a/LuckySpin/Controllers/SpinnerController.cs b/LuckySpin/Controllers/SpinnerController.cs index 92234c6..a36a10d 100644 --- a/LuckySpin/Controllers/SpinnerController.cs +++ b/LuckySpin/Controllers/SpinnerController.cs @@ -13,7 +13,7 @@ public class SpinnerController : Controller { private ISpinService spinService; private ISpinRepository spinRepository; - + /*** * Controller Constructor with Dependency Injection of a SpinRepository object */ @@ -36,7 +36,8 @@ public IActionResult Index() [HttpPost] public IActionResult Index(Player player) { - if (ModelState.IsValid) { + if (ModelState.IsValid) + { //Save the current player in the repository spinRepository.SetPlayer(player); return RedirectToAction("SpinIt"); @@ -46,9 +47,9 @@ public IActionResult Index(Player player) /*** * Spin Action - **/ - [HttpGet] - public IActionResult SpinIt(int luck) + **/ + [HttpGet] + public IActionResult SpinIt(int luck) { //Check if enough balance to play, if not drop out to LuckList if (!spinRepository.GetPlayer().ChargeSpin()) @@ -62,7 +63,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); @@ -86,7 +87,7 @@ public IActionResult SpinIt(int luck) * ListSpins Action **/ - public IActionResult LuckList() + public IActionResult LuckList() { ViewBag.Balance = 0; return View(spinRepository.GetSpins()); @@ -94,4 +95,3 @@ public IActionResult LuckList() } } - diff --git a/LuckySpin/LuckySpin.csproj b/LuckySpin/LuckySpin.csproj index 9114771..2f4cee2 100644 --- a/LuckySpin/LuckySpin.csproj +++ b/LuckySpin/LuckySpin.csproj @@ -4,6 +4,10 @@ netcoreapp3.1 + + + + PreserveNewest diff --git a/LuckySpin/Services/ISpinService.cs b/LuckySpin/Services/ISpinService.cs index 8b577ae..cf01257 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); } -} +} \ No newline at end of file diff --git a/LuckySpin/Services/SpinService.cs b/LuckySpin/Services/SpinService.cs index 9ad3c3d..1ccad32 100644 --- a/LuckySpin/Services/SpinService.cs +++ b/LuckySpin/Services/SpinService.cs @@ -15,11 +15,18 @@ public SpinService(ISpinRepository sr) } - - public double CalculateAvgWins() + public double CalculateAvgWins(bool winning) { - //TODO: Write logic to use the "real" spinRepository NOT the test data - return .1; + //TODO: Write logic to use the "real" spinRepository NOT the test data' + double wins = 0; + if (winning) + wins = 1; + foreach (Spin s in spinRepository.GetSpins()) + { + if (s.IsWinning == true) + wins++; + } + return wins / (spinRepository.GetCount() + 1); } public Spin SpinIt(int luck) @@ -39,4 +46,4 @@ public Spin SpinIt(int luck) }; } } -} +} \ No newline at end of file