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
16 changes: 7 additions & 9 deletions LuckySpin.Test/SpinServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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()); // 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()
{

}
}
}
}
2 changes: 1 addition & 1 deletion LuckySpin/Controllers/SpinnerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion 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);
}
}
18 changes: 15 additions & 3 deletions LuckySpin/Services/SpinService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using LuckySpin.Models;
using LuckySpin.Repositories;
namespace LuckySpin.Services
Expand All @@ -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++;
}
Comment on lines +28 to +32
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works - good job. Consider refactoring using the Count() method from Lambda Extension methods


return winningCount / spinCounts;
}

public Spin SpinIt(int luck)
Expand Down