I have code under test that basically looks like this (the specific code isn't important to the question. It is just here for explanatory purposes):
public ICollection<Product> GetByCategory(string category, ISession session)
{
return session
.CreateCriteria(typeof(Product))
.Add(Restrictions.Eq("Category", category))
.List<Product>();
}
This uses method chaining (and the solution I'm looking for would also apply to fluent interface syntax).
I am not interested in finding solutions for just this specific example, I am interested in solving a more general problem. In this example, I'd like to only add an expectation for CreateCriteria. If I do this, though, I get a NullReferenceException, even if I have the CreateCriteria return a stub, because the Add method returns null.
I'd like my test to continue to work, even if additional methods are chained, or the Add method gets removed.
Is there a general trick to lowering the number of test doubles/expected calls to just the ones I want to assert against, when using method chaining?
A solution I can think of would be to make a T4 template that enumerates all methods on a type, and creates a stub with expectations that give a different default return values. But I am wondering if there are simpler options.
I am using Rhino.Mocks, but a general solution would be even more appreciated.