Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking lambda in rhino mocks

I am trying to use Rhino Mocks to mock the following lambda, but keep hitting a brick wall

var result = rep.Find<T>(x => (x as IEntity).ID == (entity as IEntity).ID).FirstOrDefault();

Any ideas?

like image 443
Coppermill Avatar asked Dec 23 '22 04:12

Coppermill


2 Answers

In a unit test, you have the system under test (SUT) and its collaborators. The goal of mocking is to replace the collaborators by something that you have full control over. That way you can set up different test cases and you can focus on testing just the behavior of the system under test, and nothing else.

In this case, I assume the rep object is the SUT. The lambda that you pass to the SUT's Find method could be considered a collaborator. Since you already have full control over that lambda, it doesn't really make sense to try to mock it with Rhino Mocks.

I'll try to give an example of unit test involving Rhino Mocks and lambdas anyway ;-) This is an example test which creates a predicate stub that always returns false, and which verifies that the Find method actually invoked that predicate:

[Test]
public void Find_returns_nothing_if_predicate_always_false()
{
   var predicateStub = MockRepository.GenerateStub<Func<Entity,bool>>();
   predicateStub.Stub(x => x(Arg<Entity>.Is.Anything)).Return(false);

   var repository = new Repository();
   var entities = repository.Find(predicateStub);

   Assert.AreEqual(0, entities.Count(), 
      "oops, got results while predicate always returns false");
   predicateStub.AssertWasCalled(x => x(Arg<Entity>.Is.Anything));
}

Of course, as in your own example, you don't really need Rhino Mocks here. The whole point of the lambda syntax is to make it easy to provide an implementation in-place:

[Test]
public void Find_returns_nothing_if_predicate_always_false()
{
   bool predicateCalled = false;
   Func<Entity,bool> predicate = x => { predicateCalled = true; return false; };

   var repository = new Repository();
   var entities = repository.Find(predicate);

   Assert.AreEqual(0, entities.Count(), 
      "oops, got results while predicate always returns false");
   Assert.IsTrue(predicateCalled, "oops, predicate was never used");
}
like image 128
Wim Coenen Avatar answered Dec 31 '22 15:12

Wim Coenen


Found the answer I was after

repository.Expect(action => action.Find<Entity>(x => x.ID == 0))
          .IgnoreArguments()
          .Return(entities)
          .Repeat
          .Any();
like image 21
Coppermill Avatar answered Dec 31 '22 13:12

Coppermill