c# - Why this mock with Expression<Func<T,bool> is not matching? -


i new on mock , trying mock example :

repository.cs

public class repository : irepository {     public list<person> getforexpression(expression<func<person,bool>> expression  )     {         ... //get person orm using expression     } } 

personservice.cs

public class personservice {     private irepository _repository;     public personservice(irepository repository)     {         _repository = repository;     }      public list<person> getallpersonswith18more()     {                     var result = _repository.getforexpression(x => x.age > 18);          return result;      } } 

test:

[testclass] public class unittest1 {     [testmethod]     public void testmethod1()     {         var moqrepository = new mock<irepository>();         var service = new personservice(moqrepository.object);         expression<func<person, bool>> criteria = y => y.age  18;         moqrepository.setup(x => x.getforexpression(it.is<expression<func<person, bool>>>(y => y == criteria))).returns(new list<person>());         service.getallpersonswith18more();         moqrepository.verifyall();     } } 

if use setup works: moqrepository.setup(x => x.getforexpression(it.isany>>())).returns(new list());

but want use more specific criteria, 1 example demonstrate need.

this example not matching, can understand why , solve problem?

if needed test delegates apply them test object. in order test personservice i'd rather use following code contains examples of 2 tests.

[testclass] public class unittest2 {     private mock<irepository> moqrepository;     private personservice service;      [testinitialize]     public void testinitialize()     {         moqrepository = new mock<irepository>();         service = new personservice(moqrepository.object);     }      [testmethod]     public void testmethod3()     {         // arrange         var persons = new list<person>         {             new person { age = 0 },             new person { age = 1 },             new person { age = 17 },             new person { age = 18 },             new person { age = 19 },             new person { age = 100 }         };         moqrepository             .setup(x => x.getforexpression(it.isany<expression<func<person, bool>>>()))             .returns<expression<func<person, bool>>>(expr => persons.where(expr.compile()).tolist());          // act         var result = service.getallpersonswith18more();          // assert         moqrepository.verifyall();         result.should().beequivalentto(persons.where(x => x.age > 18));     }      [testmethod]     public void testmethod4()     {         // arrange         expression<func<person, bool>> criteria = x => x.age > 18;         moqrepository             .setup(x => x.getforexpression(it.isany<expression<func<person, bool>>>()))             .returns(new list<person>())             .callback<expression<func<person, bool>>>(expr =>             {                 var persons = new list<person>                 {                     new person { age = 0 },                     new person { age = 1 },                     new person { age = 17 },                     new person { age = 18 },                     new person { age = 19 },                     new person { age = 100 }                 };                 persons.where(expr.compile()).should().beequivalentto(persons.where(criteria.compile()));             });          // act         service.getallpersonswith18more();          // assert         moqrepository.verifyall();     } } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -