Theories

This is great! Things are going quite smoothly. Hopefully, you're starting to get the hang of Test-Driven Development. Now, let's look into a slightly more advanced test method using the Theory and InlineData attributes.

Looking back at our tests, we see that we have a test method named Given15ThenFizzBuzz. While this is fine, it's a little too specific. Remember, our requirement was that, if the number is divisible by 3 and 5, then we should return FizzBuzz. Let's ensure we didn't take too big a leap in logic by writing a new test. This time, we’ll supply a number of values, expecting the same results:

[Theory]
[InlineData(0)]
[InlineData(15)]
[InlineData(30)]
[InlineData(45)]
public void GivenDivisibleBy3And5ThenFizzBuzz(int number)
{
// Arrange
// Act
var result = FizzBuzz(number);

// Assert
Assert.Equal("FizzBuzz", result);
}

When you run the test suite, you should now see four new passed test results. If you do experience a failure, the results pane in the Test Explorer window should provide a detailed explanation as to which test failed.

Now, do the same thing for Fizz and Buzz by creating two more test cases using Theories and InlineData. Go ahead and add GivenDivisibleBy3ThenFizz, GivenDivisibleBy5ThenBuzz, and GivenNotDivisibleBy3or5ThenNumber. Be sure to run your test suite after adding each test and InlineData value, fixing any failures along the way.

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

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