Below is fluent interface:
public interface IReporter<in T,out TResult>
{
IReporter<T, TResult> Add(T seed);
TResult Prepare();
}
Using in the code as:
string errorReport = ErrorReporter.Add(exception).Prepare();
Mock test case:
With.Mocks(mockRepository)
.Expecting(() =>
{
Expect.Call(errorReporter.Add(null)).IgnoreArguments();
Expect.Call(errorReporter.Prepare()).Return(string.Empty);
Expect.Call(notifier.Notify(null)).IgnoreArguments().Return(true);
})
.Verify(() =>
{
ITransporter transporter = new Transporter
{
ExpectedArgsLength = 1,
Notifiers = notifiers,
ErrorReporter = errorReporter
};
transporter.Run(new string[] { });
});
Error:
Rhino.Mocks.Exceptions.ExpectationViolationException : IReporter`2.Prepare(); Expected #1, Actual #0.
If I comment Expect.Call(errorReporter.Prepare()).Return(string.Empty); then it works which doesn't make sense to me.
Am I missing something? Please help!
Expect.Call(errorReporter.Add(null)).IgnoreArguments().Return(errorReporter);
you need to tell the mock object to return the object you expect from the call to Add in order to chain these calls together. honestly, i'm suprised it doesn't fail with a nullreferenceexception when Add returns null and Prepare is called on a null reference.