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
14 changes: 8 additions & 6 deletions LuckySpin.Test/SpinServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ public void SpinService_CalculateAvgWins_WinningSpin()
var mockRepo = new Mock<ISpinRepository>();
//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()
{
}

} */
}
}
}
16 changes: 8 additions & 8 deletions LuckySpin/Controllers/SpinnerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SpinnerController : Controller
{
private ISpinService spinService;
private ISpinRepository spinRepository;

/***
* Controller Constructor with Dependency Injection of a SpinRepository object
*/
Expand All @@ -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");
Expand All @@ -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())
Expand All @@ -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);
Expand All @@ -86,12 +87,11 @@ public IActionResult SpinIt(int luck)
* ListSpins Action
**/

public IActionResult LuckList()
public IActionResult LuckList()
{
ViewBag.Balance = 0;
return View(spinRepository.GetSpins());
}

}
}

4 changes: 4 additions & 0 deletions LuckySpin/LuckySpin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="4.14.1" />
</ItemGroup>

<ItemGroup>
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="Models\Spin.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
4 changes: 2 additions & 2 deletions LuckySpin/Services/ISpinService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace LuckySpin.Services
{
public interface ISpinService
{
public Double CalculateAvgWins();
public Double CalculateAvgWins(bool IsWinning);
public Spin SpinIt(int Lucky);
}
}
}
17 changes: 12 additions & 5 deletions LuckySpin/Services/SpinService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -39,4 +46,4 @@ public Spin SpinIt(int luck)
};
}
}
}
}