chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I'm fairly new to TypeMock and have found it pretty easy to get going but have gotten stuck.

I'm trying to write tests for derived service objects that use lambda expressions. I want to verify the method is called with a specific lambda expression and returns the value.

I can't figure out how to test that a specific lambda expression is used:
item => item.Name == "MyBusinessUnit"

Any ideas?

Test:
[TestMethod]
[VerifyMocks]
public void Test()
{
    BusinessUnit myBusinessUnit = new BusinessUnit{Name="MyBusiness"};
    BusinessUnitService businessUnitService = new BusinessUnitService();
    Isolate.WhenCalled(() => businessUnitService.GetObject(null)).WillReturn(myBusinessUnit);

    businessUnitService.GetBusinessUnitByName("MyBusiness");

    Isolate.Verify.WasCalledWithExactArguments(() => businessUnitService.GetObject(item => item.Name == "MyBusinessUnit"));
}


The method under test:
public BusinessUnit GetBusinessUnitByName(string businessUnitName)
{
    return GetObject(item => item.Name == businessUnitName);
}
asked by jberd126 (760 points)

1 Answer

0 votes
Hi
As far as I know when using lambda expression the compiler generates anonymous type to wrap the lambda so the what happens is that the lambda
in the verification line:
Isolate.Verify.WasCalledWithExactArguments(() => businessUnitService.GetObject(item => item.Name == "MyBusinessUnit"));

is a different type from the one inside GetBusinessUnitByName method.

Hope the explanation is clear enough 8)
:idea: For better understanding you can use reflector to check the generated code.

How about checking the return value from GetBusinessUnitByName?
answered by ohad (35.4k points)
...