The following works fine for me with the Moq 4.0 Beta:
public class Dude
{
public int DudeId { get; set; }
public string Ride { get; set; }
}
public interface IInterfaceToBeMocked
{
IEnumerable<Dude> SearchDudeByFilter(Expression<Func<Dude,bool>> filter);
}
and the unit test:
[TestMethod]
public void TestDudes()
{
// arrange
var expectedDudes = new[]
{
new Dude(), new Dude()
};
var mock = new Mock<IInterfaceToBeMocked>();
mock.Setup(method => method.SearchDudeByFilter(
x => x.DudeId.Equals(10) && x.Ride.Equals("Harley"))
).Returns(expectedDudes);
// act
// Remark: In a real unit test this call will be made implicitly
// by the object under test that depends on the interface
var actualDudes = mock.Object.SearchDudeByFilter(
x => x.DudeId.Equals(10) && x.Ride.Equals("Harley")
);
// assert
Assert.AreEqual(actualDudes, expectedDudes);
}
Now if you change something into the argument of actual method call the test will no longer pass because the mocked method will return the expected result only if the argument is the same:
var actualDudes = mock.Object.SearchDudeByFilter(
x => x.DudeId.Equals(20) && x.Ride.Equals("Honda")
);
Remark: mocking methods that take lambda expressions is a new feature that was not available in previous versions where we need to use It.Is<SomeType>
and It.IsAny<SomeType>
parameter constraints.