Aligning methods using the expression <Func <T, bool >> using Moq

I want to mock this interface using Moq

IInterfaceToBeMocked { IEnumerable<Dude> SearchDudeByFilter(Expression<Func<Dude,bool>> filter); } 

I thought of doing something like

 _mock.Setup(method => method.SearchDudeByFilter( x=> x.DudeId.Equals(10) && X.Ride.Equals("Harley"))). Returns(_dudes);// _dudes is an in-memory list of dudes. 

When I try to debug a unit test where I need this mockery, it says that the expression is not allowed, pointing to a lambda. If that matters, I use xUnit as a testing framework.

+4
source share
1 answer

The following works fine for me with 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 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 in the argument argument of the actual method call, the test will no longer pass, because the prepared 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") ); 

Note: bullying methods that accept lambda expressions are a new feature not available in previous versions, where we need to use the It.Is<SomeType> and It.IsAny<SomeType> parameter restrictions.

+4
source

All Articles