A constraint can be specified on properties to achieve to same constraint or a weaker constraint that would otherwise be specified using the equality constraint.
This has the draw back of being string based but sometimes the equality constraint is too strong.
For example:
[TestFixture]
public class SampleFixture
{
[Test]
public void RhinoConstraintsWithProperties()
{
MockRepository mockRepository = new MockRepository();
Service service = mockRepository.CreateMock<Service>();
service.DoStuff(null);
LastCall.Constraints(Property.Value("A", "1") & Property.Value("B", "2"));
mockRepository.ReplayAll();
Client client = new Client(service);
client.DoClientStuff();
mockRepository.VerifyAll();
}
[Test]
public void RhinoConstraintsWithEquals()
{
MockRepository mockRepository = new MockRepository();
Service service = mockRepository.CreateMock<Service>();
service.DoStuff(null);
Foo expectedFoo = new Foo();
expectedFoo.A = "1";
expectedFoo.B = "2";
LastCall.Constraints(Is.Equal(expectedFoo));
mockRepository.ReplayAll();
Client client = new Client(service);
client.DoClientStuff();
mockRepository.VerifyAll();
}
}
public class Client
{
private readonly Service service;
public Client(Service service)
{
this.service = service;
}
public void DoClientStuff()
{
Foo f = new Foo();
f.A = "1";
f.B = "2";
service.DoStuff(f);
}
}
public interface Service
{
void DoStuff(Foo foo);
}
public class Foo
{
private string a, b;
public override bool Equals(object obj)
{
if (this == obj) return true;
Foo foo = obj as Foo;
if (foo == null) return false;
return Equals(a, foo.a) && Equals(b, foo.b);
}
public override int GetHashCode()
{
return (a != null ? a.GetHashCode() : 0) + 29*(b != null ? b.GetHashCode() : 0);
}
public string B
{
get { return b; }
set { b = value; }
}
public string A
{
get { return a; }
set { a = value; }
}
}