Rhino Mock: How to test called function with Lambda Expression

77 views Asked by At

I've a classic BLL and DAL injected in costructor. This is the code:

public interface IAbilitazioneLogic
{
    IEnumerable<myEntity> CercaByNumerica(Guid id);
}
public class AbilitazioneLogic : IAbilitazioneLogic
{
    private readonly INumeroVerdeDAL _dal;

    public AbilitazioneLogic(INumeroVerdeDAL dal)
    {
        _dal = dal;
    }

    public IEnumerable<myEntity> CercaByNumerica(Guid id)
    {
        return _dal.SearchAbilitazioni(a => a.new_numericaid.Id.Equals(id));
    }
}

public interface INumeroVerdeDAL
{
    IEnumerable<myEntity> SearchAbilitazioni(Expression<Func<myEntity, bool>> predicate);
}
public class AbilitazioneDAL : INumeroVerdeDAL
{
    public IEnumerable<myEntity> SearchAbilitazioni(Expression<Func<myEntity, bool>> predicate)
    {
        context.myEntitySet.Where(predicate);
    }
}

I want to test that BLL call right DAL method, and call that method with right Expression. I try this:

[TestMethod]
public void CercaByNumerica_CallCorrectDalMethod()
{
    //ARRANGE
    INumeroVerdeDAL _dalStrinctMock = MockRepository.GenerateStrictMock<INumeroVerdeDAL>();
    Guid guid = DataFactory.GetFixedGuid();
    Expression<Func<myEntity, bool>> expr = a => a.new_numericaid.Id.Equals(guid);

    _dalStrinctMock.Expect(d => 
                d.SearchAbilitazioni(Arg<Expression<Func<myEntity, bool>>>.Is.Equal(expr))
        ).Repeat.Times(1);

    //ACT
    IAbilitazioneLogic SUT = new AbilitazioneLogic(_dalStrinctMock);
    SUT.CercaByNumerica(guid);

    //ASSERT
    _dalStrinctMock.VerifyAllExpectations();
}

But this not work. This is the error:

INumeroVerdeDAL.SearchAbilitazioni(a => a.new_numericaid.Id.Equals(value(Reply.Seat.CrmLoyalty.AbilitazioneNumeroVerde.BL.AbilitazioneLogic+<>c__DisplayClass0).id)); Expected #0, Actual #1.
INumeroVerdeDAL.SearchAbilitazioni(equal to a => a.new_numericaid.Id.Equals(value(Reply.Seat.CrmLoyalty.AbilitazioneNumeroVerde.BL.Tests.AbilitazioneLogicTests+<>c__DisplayClass4).guid)); Expected #1, Actual #0.

At this line: SUT.CercaByNumerica(guid);

what is the right way to do this test?

EDIT: I do not think that this is a duplicate, because even if the problem is similar, the solution I request is not the same. The proposed solution refers to a link where you read a discussion that partially solves the problem: https://groups.google.com/forum/#!topic/rhinomocks/ozsWXBb8SO4 This is the proposed solution:

I think you need to re-think how to test this situation. In writing a unit test for Handler.HandleSomethingHappened(), you'll just want to make sure that it calls IList.ForEachEntry().

I have already performed this test, I have already verified that the correct method is called. The problem is that it is passed a "lambda" as a parameter to the called method. I have to check whether the execution of this lambda returns the correct result.

I explain in more detail: I've already performed that BLL's Method "AbilitazioneLogic.CercaByNumerica(Guid id)" calls DAL's method "_dal.SearchAbilitazioni()". Now I've to check that parameter DAL's methos is called with correct lambda and returns the correct result.

How can I do this?

0

There are 0 answers