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
6 changes: 4 additions & 2 deletions LuckySpin.Test/SpinServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ 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);
}
Expand Down
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);
}
}
15 changes: 11 additions & 4 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;
Comment on lines +21 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using the conditional operator ?: to assign the wins start value which takes a boolean and returns either the first if true or second value if not.
double wins = winning ? 1 : 0;

foreach (Spin s in spinRepository.GetSpins())
{
if (s.IsWinning == true)
wins++;
}
Comment on lines +24 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using the Lamda Extension method Count() on the spins collection returned by GetSpins()

return wins / (spinRepository.GetCount() + 1);
}

public Spin SpinIt(int luck)
Expand Down