OK, I have a solution, it involves doing matching on property values that are inside the criteria. The method CreateCriteriaMatcher does that work...
Here is a full example:
Code:
using System;
using NUnit.Framework;
using NHibernate;
using NHibernate.Expression;
using NMock2;
using NMock2.Matchers;
using System.Collections;
namespace Foo
{
[TestFixture]
public class SearcherTest
{
[Test]
public void SearcherAppliesCorrectCriteria()
{
IList irrelevantList = null;
Mockery mocks = new Mockery();
ISession mockSession = (ISession)mocks.NewMock(typeof(ISession));
ICriteria mockCrit = (ICriteria)mocks.NewMock(typeof(ICriteria));
NMock2.Expect.Once.On(mockSession).Method("CreateCriteria").With(typeof(Bar)).Will(Return.Value(mockCrit));
NMock2.Expect.Once.On(mockCrit).Method("Add").With(CreateCriteriaMatcher(Expression.Eq("Prop1", "adam")));
NMock2.Expect.Once.On(mockCrit).Method("List").WithNoArguments().Will(Return.Value(irrelevantList));
Searcher searcher = new Searcher(mockSession);
IList result = searcher.DoStuff("adam");
Assert.AreEqual(irrelevantList, result);
}
private Matcher CreateCriteriaMatcher(SimpleExpression expression)
{
Matcher valMatch = new PropertyMatcher("Value", new EqualMatcher(expression.Value));
Matcher nameMatch = new PropertyMatcher("PropertyName", new EqualMatcher(expression.PropertyName));
Matcher andMatcher = new AndMatcher(valMatch, nameMatch);
return andMatcher;
}
}
public class Searcher
{
ISession session;
public Searcher(ISession session)
{
this.session = session;
}
public IList DoStuff(string name)
{
ICriteria crit = session.CreateCriteria(typeof(Bar));
crit.Add(Expression.Eq("Prop1", name));
return crit.List();
}
}
public class Bar
{
}
}
[/code]