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
23 changes: 19 additions & 4 deletions LuckySpin.Test/SpinServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,38 @@ namespace LuckySpin.Test
{
public class SpinServiceTest
{
[Fact] //BONUS: Refactor into a Theory that tests both a winning and losing spin
public void SpinService_CalculateAvgWins_WinningSpin()
[Theory] //BONUS: Refactor into a Theory that tests both a winning and losing spin
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.

Good work refactoring the Test to consider both cases

[InlineData(true)]
[InlineData(false)]
public void SpinService_CalculateAvgWins_WinningSpin(bool isWinning)
{
//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());
mockRepo.Setup(r => r.GetCount()).Returns(SpinListData.GetCount());


var service = new SpinService(mockRepo.Object);

service.IsWinning = isWinning;
//Act - run the method that you are testing and get a result
double result = service.CalculateAvgWins();

//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 expected = wins / count;
double count = 10 + 1;
double wins;
if (isWinning)
{
wins = 4 + 1;
}
else
{
wins = 4;
}
Comment on lines +35 to +43
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.

Consider using the ternary conditional operator ?: to assign the wins value - it takes a boolean and returns either the first value, if true or second value if not. So the 9 lines above end up as...
double wins = isWinning ? 5 : 4;

double expected = wins / count;
Assert.Equal(expected, result);
}
}
Expand Down
12 changes: 8 additions & 4 deletions LuckySpin/Services/SpinService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Linq;
using LuckySpin.Models;
using LuckySpin.Repositories;

namespace LuckySpin.Services
{
public class SpinService : ISpinService //Makes this class extend the Interface ISpinService
{
Random random = new Random();

public bool IsWinning;
private ISpinRepository spinRepository;
//Constructor with Dependency Injection
public SpinService(ISpinRepository sr)
Expand All @@ -19,7 +21,9 @@ public SpinService(ISpinRepository sr)
public double CalculateAvgWins()
{
//TODO: Write logic to use the "real" spinRepository NOT the test data
return .1;
double wins = spinRepository.GetSpins().Count(s => s.IsWinning) + (IsWinning ? 1 : 0);
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.

Nice work on this

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

public Spin SpinIt(int luck)
Expand All @@ -28,13 +32,13 @@ public Spin SpinIt(int luck)
a = random.Next(1, 10);
b = random.Next(1, 10);
c = random.Next(1, 10);

IsWinning = (a == luck || b == luck || c == luck);
return new Spin()
{
A = a,
B = b,
C = c,
IsWinning = (a == luck || b == luck || c == luck),
IsWinning = IsWinning,
Luck = luck
};
}
Expand Down