Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning.The.Art.of.Unit.Testing.with.Examples.in.dot.NET.Jun.2009.pdf
Скачиваний:
18
Добавлен:
21.03.2016
Размер:
9.67 Mб
Скачать

214

CHAPTER 7

The pillars of good tests

7.3.4Separating asserts from actions

This is a short section, but an important one nonetheless. For the sake of readability, avoid writing the assert line and the method call in the same statement.

Listing 7.27 shows a good example, and listing 7.28 shows a bad example.

Listing 7.27 Separating the assert from the thing asserted improves readability

[Test]

public void BadAssertMessage()

{

//some code here

int result= log.GetLineCount("abc.txt"); Assert.AreEqual(COULD_NOT_READ_FILE,result);

}

Listing 7.28 Not separating the assert from the thing asserted makes reading difficult

[Test]

public void BadAssertMessage()

{

//some code here

Assert.AreEqual(COULD_NOT_READ_FILE,log.GetLineCount("abc.txt"));

}

See the difference between the two examples? Listing 7.28 is much harder to read and understand in the context of a real test, because the call to the GetLineCount() method is inside the call to the assert message.

7.3.5Setting up and tearing down

Setup and teardown methods in unit tests can be abused to the point where the tests or the setup and teardown methods are unreadable. Usually the situation is worse in the setup method than the teardown method.

Let’s look at one possible abuse. If you have mocks and stubs being set up in a setup method, that means they don’t get set up in the actual test. That, in turn, means that whoever is reading your test may not even

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]