-
Notifications
You must be signed in to change notification settings - Fork 14
All done #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: tdd-setup
Are you sure you want to change the base?
All done #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| [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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using the ternary conditional operator |
||
| double expected = wins / count; | ||
| Assert.Equal(expected, result); | ||
| } | ||
| } | ||
|
|
||
| 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) | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -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 | ||
| }; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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