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

120CHAPTER 5 Isolation (mock object) frameworks

you know the exact values of each constraint.

the Equals() method is implemented correctly on the two objects being compared. (It’s usually bad practice to rely on the out-of-the- box implementation of object.Equals().)

5.6.3Executing callbacks for parameter verification

The Is.Matching<T>(Predicate<T>) constraint is a powerful feature that allows the developer to test whatever he wants against the passedin parameter, and return true or false based on complex rules.

For example, assume that the IWebService interface has a method that expects to take in a TraceMessage object, which in turn has a property that holds an object you’d like to check. If we had a ComplexTraceMessage class with an InnerMessage property and a complex verification on it, it might look like listing 5.15.

Listing 5.15 Using an anonymous delegate to verify a parameter

LastCall.Constraints(

Is.Matching<ComplexTraceMessage>( delegate(ComplexTraceMessage msg)

{

if (msg.InnerMessage.Severity < 50

&& msg.InnerMessage.Message.Contains("a"))

{

return false;

}

return true;

}));

In listing 5.15, we’re creating a delegate that holds the logic to verify the complex parameter structure.

Instead of using a delegate, we could create a method with the same signature that does the same thing, as shown in listing 5.16.

Listing 5.16 Using a regular method instead of an anonymous delegate

[Test]

public void ComplexConstraintsWithCallbacks()

{

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