c# - Moq Mock lambda expression -
i trying mock repository on lambda expression executed. method want execute looks this:
public selectlistitem getprotocolbyid(int protocolid) { var protocol = unitofwork.protocolrepository.findall(s => s.protocol_id == protocolid).firstordefault(); return new selectlistitem() { text = protocol.user_defined_id, value = protocolid.tostring() }; }
i created mockrepository in class
protected mockrepository mockrepository = new mockrepository(mockbehavior.default);
i have unitofwork , protocols repositories , mock them this:
var protocolrepositorymock = mockrepository.create<irepository<protocol>>(); var unitofworkrbmmock = mockrepository.create<iunitofworkrbm>();
the findall method irepository looks :
ienumerable<tentity> findall(expression<func<tentity, bool>> where, params expression<func<tentity, object>>[] includes);
and definition of irepository in iunitofworkrbm one:
irepository<protocol> protocolrepository { get; set; }
i have tried mock unitofworkmock able execute lambda expression this:
unitofworkrbmmock.setup(s => s.protocolrepository.findall(it.isany<expression<func<protocol, bool>>>())) .returns(new func<expression<func<protocol, bool>>, ienumerable<protocol>>( expr => protocolslist.where(expr.compile())));
but when try call getprotocolbyid
method, protocol comes null.
any ideas on doing wrong here ?
the value of expression provided can accessed in returns
method. use filter fake list
review following minimal example used solve problem.
[testmethod] public void _mytestmethod() { //arrange var expectedprotocolid = 1; var protocolslist = new list<protocol> { new protocol { protocol_id = expectedprotocolid, user_defined_id = "some user defined id" }, new protocol { protocol_id = 2, user_defined_id = "some other user defined id" } }; var unitofworkrbmmock = new mock<iunitofworkrbm>(); unitofworkrbmmock .setup(_ => _.protocolrepository.findall(it.isany<expression<func<protocol, bool>>>(), it.isany<expression<func<protocol, object>>[]>())) .returns<expression<func<protocol, bool>>, expression<func<protocol, object>>[]>((expr, includes) => protocolslist.where(expr.compile())); var sut = new sut(unitofworkrbmmock.object); //act var result = sut.getprotocolbyid(expectedprotocolid); //assert assert.isnotnull(result); } class sut { private iunitofworkrbm unitofwork; public sut(iunitofworkrbm unitofworkrbm) { this.unitofwork = unitofworkrbm; } public selectlistitem getprotocolbyid(int protocolid) { var protocol = unitofwork.protocolrepository.findall(s => s.protocol_id == protocolid).firstordefault(); return new selectlistitem() { text = protocol.user_defined_id, value = protocolid.tostring() }; } } public interface iunitofworkrbm { irepository<protocol> protocolrepository { get; set; } } public interface irepository<tentity> { ienumerable<tentity> findall(expression<func<tentity, bool>> where, params expression<func<tentity, object>>[] includes); } public class protocol { public int protocol_id { get; set; } public string user_defined_id { get; set; } }
Comments
Post a Comment