That specifies behavior on the dependency of type TDependency as a lambda expression in the Action / Func block. The statement cannot stand alone as it does not apply any behavior. The following behaviors can be applied the Given.That statement.


Given.That<IDependency>(x => x.Method(...)).Returns(Value);

Returns specifies which value is returned by the method when called during the test. If the method takes any parameters just pass in null, as Test By Wire does not use the parameters for specifying behavior. The Value is not type safe - it is an object and has to match the method's signature. Failing to match the signature will result in a runtime exception. To get strong typing use the That overload with a return type generic parameter.


The Repeat method can be tailed to a Return statement specifying how many times a value should be returned. Consider the following test.

public void ExplicitRepeatsArePreserved()
    Given.That<IValueFetcher>(x => x.GetValue()).Returns(1); // Repeated once
    Given.That<IValueFetcher>(x => x.GetValue()).Returns(3).Repeat(2); // Repeated once
    Given.That<IValueFetcher>(x => x.GetValue()).Returns(5); // Repeated forever

    var total = When.Calling.Iterate(5);

    Assert.AreEqual(17, total);

The first time GetValue is called the code returns the value 1. The next two times the GetValue method is called it returns the value 3. The last Repeat statement specifying to return the value 5 will be returned for all calls to GetValue from then on.

A value is always returned forever unless another Return statement is added. Then the first statement is only returned once or as many times as the repeat statement states.

That<TDependency, TReturnType>(Func)

The That statement with the return type specified accommodates stubbing properties. Since C# does not support partial inference, we need a construct like this to make the compiler see our intention.

Last edited Sep 12, 2011 at 7:35 PM by RasmusTherkelsen, version 4


No comments yet.