Number not found

To get started, a new test method is needed to verify that the Number not found message is returned:

[Fact]
public void GivenNonDivisibleGreaterThan1ThenNumberNotFound()
{
// Arrange
// Act
var result = FizzBuzz(2);
// Assert
Assert.Equal("Number not found", result);
}

Now, make the test pass by modifying the existing code:

private object FizzBuzz(int value)
{
if (value % 15 == 0)
return "FizzBuzz";
if (value % 5 == 0)
return "Buzz";
if (value % 3 == 0)
return "Fizz";
if (value == 2)
return "Number not found";
}

This covers the first instance. However, does this satisfy the new requirement? Create a Theory set to force the proper solution:

[Theory]
[InlineData(2)]
[InlineData(4)]
[InlineData(7)]
[InlineData(8)]
public void GivenNonDivisibleGreaterThan1ThenNumberNotFound(int number)
{
// Arrange
// Act
var result = FizzBuzz(number);
// Assert
Assert.Equal("Number not found", result);
}

Make the test pass, the right way. Modify the existing code so that the desired results are returned:

private object FizzBuzz(int value)
{
if (value % 15 == 0)
return "FizzBuzz";
if (value % 5 == 0)
return "Buzz";
if (value % 3 == 0)
return "Fizz";
return value == 1 ? (object)value : "Number not found";
}

Note that all the existing tests should continue to pass throughout this exercise. If you find a bug, write a new test to verify the scenario, and correct the code accordingly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.200.164