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
21 changes: 21 additions & 0 deletions ClassLibrary1/Entities/Menu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using WhatShouldITest.Interfaces;
using WhatShouldITest.Utilities;

namespace WhatShouldITest.Entities
{
public class Menu
{
private readonly IMenuItemProvider _menuItemProvider;

public Menu(IMenuItemProvider menuItemProvider)
{
_menuItemProvider = menuItemProvider;
DailySpecial = MenuUtilities.GetDailySpecial(DateTime.Now.DayOfWeek);
Copy link
Contributor Author

@psiberknetic psiberknetic Dec 12, 2019

Choose a reason for hiding this comment

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

The test we've written is actually testing line 15 in the code above. The only thing that is happening in this line is an assignment. We're just testing that the .NET assignment operator is working. We don't need to test that. If that's not working, we are going to be having MUCH larger problems. We don't need to cover this line. There is only one path this code can take. There is no branching logic.

Some folks would say that we could rewrite the test to ensure that the CORRECT special is being stored, but this isn't the place for that. The predicate isn't here. Where is it?

}

IEnumerable<MenuItem> Items { get; }
public string DailySpecial { get; }
}
}
12 changes: 12 additions & 0 deletions ClassLibrary1/Entities/MenuCategory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace WhatShouldITest.Entities
{
public enum MenuCategory
{
Appetizer,
Soup,
Salad,
Entree,
Dessert,
Drink
}
}
14 changes: 14 additions & 0 deletions ClassLibrary1/Entities/MenuItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace WhatShouldITest.Entities
{
public class MenuItem
{
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public MenuCategory Category { get; set; }
}
}
10 changes: 10 additions & 0 deletions ClassLibrary1/Interfaces/IMenuItemProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using WhatShouldITest.Entities;

namespace WhatShouldITest.Interfaces
{
public interface IMenuItemProvider
{
IEnumerable<MenuItem> GetMenuItems();
}
}
32 changes: 32 additions & 0 deletions ClassLibrary1/Utilities/MenuUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Linq;

namespace WhatShouldITest.Utilities
{
public static class MenuUtilities
{
public static string GetDailySpecial(DayOfWeek dayOfWeek)
{
var _dailySpecials = new[]{
new {DayOfWeek = DayOfWeek.Monday,
Special = "Half price tacos!" },
new {DayOfWeek = DayOfWeek.Tuesday,
Special = "50 cent wings"},
new {DayOfWeek = DayOfWeek.Wednesday,
Special = "2-for-1 Desserts"},
new {DayOfWeek = DayOfWeek.Thursday,
Special = "5$ Burgers"},
new {DayOfWeek = DayOfWeek.Friday,
Special = "All you can eat ribs" },
new {DayOfWeek = DayOfWeek.Saturday,
Special = "Special Brunch" },
new {DayOfWeek = DayOfWeek.Sunday,
Special = "Sunday, funday"}
};

return _dailySpecials.Where(s => s.DayOfWeek == dayOfWeek)
.Select(s => s.Special)
.First();
Comment on lines +27 to +29
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here it is. In the GetDailySpecial method in the MenuUtilities class. specifically the s.DayOfWeek == dayOfWeek portion of the code. That is the expression that will return either true or false, and therefore where the flow of the code can branch.

This is the logic that should be tested. Where the predicate is.

}
}
}
7 changes: 7 additions & 0 deletions ClassLibrary1/WhatShouldITest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
7 changes: 6 additions & 1 deletion GettingStarted/1_SimpleStarterTests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
Scenario: Add two numbers - no variables
Given I have entered the numbers 1 and 2
When I add 1 and 2 together
Then the result should be 3
Then the result should be 3

Scenario: Add two different numbers - no variables
Given I have entered the numbers 3 and 5
When I add 3 and 5 together
Then the result should be 8
40 changes: 40 additions & 0 deletions GettingStarted/1_SimpleStarterTests.feature.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions GettingStarted/2_TestAddingNumbersWithVariables.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Feature: TestAddingNumbersWithVariables
In order to avoid writing a lot of extra testing code
I want to be able to use variables in my SpecFlow tests

@mytag
Scenario: Add 1 and 2
Given I have the numbers 1 and 2
When I add them together
Then they should add up to 3

Scenario: Add 3 and 4
Given I have the numbers 3 and 4
When I add them together
Then they should add up to 7

Scenario: Add 101 and 1332
Given I have the numbers 101 and 1332
When I add them together
Then they should add up to 1433
Loading