To avoid making common mistakes, let's fix some unit tests Stringie developers wrote. This time, they turned the Console project from Lesson 1 into a real test project and wrote some unit tests. But, they forgot to follow naming conventions and made other mistakes along the way. You will find the solution with the tests they wrote, next to this file.
Your mission, Jim, should you decide to accept it, is to fix the unit test Stringie developers wrote. You will find the instructions to proceed below. This repo will self-destruct in five seconds. Good luck, Jim.
- Clone this repo
- Does the test project compile? Are all tests passing? If that's not the case, make them pass
- Does the test project follow a naming convention?
- Does the test project name have a
Tests
orUnitTests
suffix?
- Does the test project name have a
- Are the tests organized in the appropiate files?
- Do the test names follow a naming convention? For example: sentence, UnitOfWork_Scenario_Result, GivenWhenThen
- Do the tests follow Arrange/Act/Assert?
- Is the code inside each test separated with line breaks to show each part?
- Do the tests use an Assertion library? If that's not the case, rewrite those test to use MSTest
- Do the tests use the appropiate Assert methods? For example:
AreNull()
instead ofAreEqual()
with anull
value,Assert.AreEqual()
instead ofIsTrue()
with a comparison - Do the tests have a single assertion per test? It that's not the case, use Parameterized tests
- Do the assertions repeat the logic under test?
For more examples of mistakes when writing unit tests, check 4 common mistakes when writing your first unit tests.
You can take a look at the README file from Lesson 1 to check all the Stringie available methods.
To write a parameterized test, follow these steps:
- Instead of using
[TestMethod]
attribute, use[DataTestMethod]
- Use a
[DataRow]
attribute for each set of test values - Add the appropriate parameters to your test. One paramater for each value inside the
[DataRow]
attribute - Use the input parameters in your tests to Arrange, Act or Assert
For example,
[DataTestMethod]
[DataRow("HELLO")]
[DataRow("Hello")]
[DataRow("HeLLo")]
public void Remove_SubstringWithDifferentCase_RemovesSubstring(string substringToRemove)
{
var str = "Hello, world!";
var transformed = str.RemoveAll(substringToRemove).IgnoringCase();
Assert.AreEqual(", world!", transformed);
}