I would like to mock this interface using Moq or RhinoMocks for the purpose of verifying that the correct expression is passed as a parameter (and willing to switch to any other open source mocking library that can support this):
Full source code:
public class Record
{
public int RecordId { get; set; }
}
public interface IRepository
{
void DeleteRecordsByFilter(Expression<Func<Record, bool>> filter);
}
public class MyClass
{
private readonly IRepository _repo;
public MyClass(IRepository repo)
{
_repo = repo;
}
public void DeleteRecords(int recordId)
{
_repo.DeleteRecordsByFilter(x => x.RecordId.Equals(recordId));
}
}
[TestFixture]
public class MyFixture
{
[Test]
public void DeleteRecordsShouldCallDeleteRecordsByFilterOnRepo()
{
const int recordId = 10;
var repo = new Mock<IRepository>();
repo.Setup(method => method.DeleteRecordsByFilter(x => x.RecordId.Equals(recordId)));
var sut = new MyClass(repo.Object);
sut.DeleteRecords(recordId);
repo.Verify(method => method.DeleteRecordsByFilter(x => x.RecordId.Equals(recordId)));
}
}
When I execute the unit test it fails with the error:
Moq.MockException : Expected invocation on the mock at least once, but was never performed: method => method.DeleteRecordsByFilter(x => x.RecordId.Equals(10))
Configured setups: method => method.DeleteRecordsByFilter(x => x.RecordId.Equals(10)), Times.Never
Performed invocations: IRepository.DeleteRecordsByFilter(x => x.RecordId.Equals(value(MyClass+<>c__DisplayClass0).recordId))