Friday, 15 August 2014

c# - Moq object not working when executing a secondary command -



c# - Moq object not working when executing a secondary command -

hi attempting write unit test class commandbusiness:

public class commandbusiness { irepository<command> _repository; public commandbusiness(irepository<command> repository) { _repository = repository; } public list<command> getcommandsfromprojectid(int id) { homecoming _repository.searchfor(command => command.projectid == id); } public list<command> getall() { homecoming _repository.getall(); } public void delete(int id) { entities.command command = _repository.getbyid(id); _repository.delete(command); } } public interface irepository<t> t : new() { void delete(t entity); list<t> searchfor(expression<func<t, bool>> predicate); list<t> getall(); t getbyid(int id); } public class ormliterepository<t> : irepository<t> t : new() { public idbconnectionfactory dbfactory { get; private set; } string connectionstring = ""; public ormliterepository() { dbfactory = null; // ormliteconnectionfactory(configurationmanager.appsettings["connectionstring"].tostring(), sqlserverdialect.provider); } public list<t> searchfor(expression<func<t, bool>> predicate) { using (idbconnection db = dbfactory.opendbconnection()) { homecoming db.select(predicate); } } public list <t> getall() { using (idbconnection db = dbfactory.opendbconnection()) { homecoming db.select<t>(); } } public void delete(t entity) { using (idbconnection db = dbfactory.opendbconnection()) { db.delete<t>(entity); } } public t getbyid(int id) { using (idbconnection db = dbfactory.opendbconnection()) { homecoming db.getbyid<t>(id); } } } public class command { public int id { get; set; } public string commandtext { get; set; } public bool appendtonextline { get; set; } public int projectid { get; set; } public int sortorder { get; set; } } public class testing { [testmethod] public void testmethod() { list<command> commandsx = new list<command> { new command { appendtonextline = false, commandtext = "sometext", id = 1, projectid = 1 , sortorder = 1}, new command { appendtonextline = false, commandtext = "sometext", id = 2, projectid = 1 , sortorder = 2}, new command { appendtonextline = false, commandtext = "sometext", id = 3, projectid = 2 , sortorder = 3}, new command { appendtonextline = false, commandtext = "sometext", id = 4, projectid = 2 , sortorder = 4}, new command { appendtonextline = false, commandtext = "sometext", id = 5, projectid = 3 , sortorder = 5}, new command { appendtonextline = false, commandtext = "sometext", id = 6, projectid = 3 , sortorder = 6}, new command { appendtonextline = false, commandtext = "sometext", id = 7, projectid = 3 , sortorder = 7} }; mock<irepository<command>> mockproductrepository = new mock<irepository<command>>(); mockproductrepository.setup(mpr => mpr.getall()).returns(commandsx); var combusiness = new commandbusiness(mockproductrepository.object); var comsoriginal = combusiness.getall(); combusiness.delete(3); comsoriginal = combusiness.getall(); } }

when effort run above code comsoriginal out holding 7 items, next execute delete command , requery combusiness.getall , 1 time again count of 7, have expected count of 6. thoughts????

you code commandbusiness.getall() this:

public list<command> getall() { homecoming _repository.getall(); }

you create mock repository homecoming fixed list:

mock<irepository<command>> mockproductrepository = new mock<irepository<command>>(); mockproductrepository.setup(mpr => mpr.getall()).returns(commandsx);

regardless of how many times phone call commandbusiness.getall() fixed list. mocks do.

in actuality want test commandbusiness not number of entities returned correct, interacts dependecies (in case irepository) correctly. commandbusiness unit not have responsibility deleting commands, delegates command irepository. assuming repository job of deleting correctly functionality of class phone call right method on irepository. true unit test should create mock irepository , set expectation delete value 3 called, phone call delete value 3 on commandbusiness , verify expected phone call indeed happen.

imho these tests not provide important value testing implementation of commandbusiness , not behavior. these tests brittle , need alter every time implementation changes.

i prefer test interacts repository (like 1 have written) (even if slower) tests behaviour, regardless of how implemented.

you create test tests combination of commandbusiness , irepository creating testrepository class wraps simple collection. utilize existing test, pass testrepository in instead of current mock irepository , test should pass, commandbusiness have actual repository beingness affected delete method getall method.

alternatively don't have mock or testrepository, utilize real ormlightrepository , spin new db test. validate works in real situation.

the best of worlds create repository configurable in tests. of time utilize standard in memory collection respository. mean tests fast. can't sure didn't utilize linq command won't supported actual db, on ci server utilize config switch tests real db , verify things work in real life.

c# design-patterns repository moq

No comments:

Post a Comment