c# - Use of rhino mocks to return specific results based on a provided Expression -
i use generic repository access data in c#, method signature of this:
public virtual ienumerable<tentity> get( expression<func<tentity, bool>> filter = null, func<iqueryable<tentity>, iorderedqueryable<tentity>> orderby = null, string includeproperties = "")
this used in mvc controller (for example) in following ways
var users = unitofwork.userrepository.get( u => u.id == userid, null, "createdemployer,createdemployer.createdbyuser"); var checkuser = unitofwork.userrepository.get(u => u.username == email) .firstordefault();
i want able mock these calls using rhinomocks , receive specific results based on expression called. pseudo code below demonstrates want achieve e.g.
unitofwork.userrepository.expect( u => u.get(arg<expression<func<user, bool>>>.matches(u.username == "jim"))) .return(new list<user>() { userjim }); unitofwork.userrepository.expect( u => u.get(arg<expression<func<user, bool>>>.matches(u.username == "jo left ages ago"))) .return(new list<user>() );
i have tried using .whencalled not seem have behaviour want when matching provided exact provided expression, return specific data:
.whencalled(invocation => { var predicate = invocation.arguments.first() expression<func<user, bool>>; ... here })
i've been trawling internet while on have not managed dig decent solution, of solutions consist of ignoring arguments works long call repo once in method e.g.
userrepository.expect(u => u.get()).ignorearguments() .return(new list<user> { currentuser });
this has proved inadequate , need bit more robust , specific. interested in first argument. need solution above if solution not exist rhinomocks i'd interested in mocking framework testing expressions easier achieve.
i have answered similar question using moq. can use same approach rhino.
the key use real (or mocked) data set, , run expressions against it.
Comments
Post a Comment