c# - Am I testing My repository the right way? -



c# - Am I testing My repository the right way? -

i have generic repository this:

public interface irepository<tentity> tentity :class { ienumerable<tentity> searchfor(expression<func<tentity, bool>> filter); tentity getbyid(int id); void insert(tentity entity); void delete(tentity entity); void update(tentity entity); } public class genericrepository<tentity> : irepository<tentity> tentity: class { private dbset<tentity> _dbset; // set entity specified in place of tentity in d dbset can query entity e.g school entity private naijaschoolscontext _naijaschoolscontext; public genericrepository(naijaschoolscontext context) { _naijaschoolscontext = context; _dbset = _naijaschoolscontext.set<tentity>(); //return entity specified in tentity , set in dbset } public ienumerable<tentity> searchfor(system.linq.expressions.expression<func<tentity, bool>> filter) { homecoming _dbset.where(filter); } public tentity getbyid(int id) { homecoming _dbset.find(id); } public void insert(tentity entity) { _dbset.add(entity); } public void delete(tentity entity) { _dbset.remove(entity); } public void update(tentity entity) { _dbset.addorupdate(entity); } }

i have uow this:

public interface iunitofwork : idisposable { void save(); } public class unitofwork : iunitofwork { naijaschoolscontext naijaschoolscontext = new naijaschoolscontext(); private genericrepository<school> schoolrepository; private bool isdisposed = false; public genericrepository<school> schoolrepository { { if (schoolrepository == null) { schoolrepository = new genericrepository<school>(naijaschoolscontext); } homecoming schoolrepository; } } public void save() { naijaschoolscontext.savechanges(); } public void dispose(bool disposing) { if (!isdisposed) { if (disposing) { naijaschoolscontext.dispose(); } } } public void dispose() { dispose(true); gc.suppressfinalize(this); } }

my test class looks this:

[testfixture] public class when_working_with_school_repository { } public class and_saving_a_school : when_working_with_school_repository { private school _returnedschool; private school _school; private mock<irepository<school>> _repository; private exception _result; [setup] private void setup() { _repository = new mock<irepository<school>>(); _school = new school(); } [test] public void then_a_valid_school_should_be_saved() { _repository.setup(s => s.insert(_school)); //_returnedschool = _schoolrepository.save(_school); } [test] public void should_throw_an_exception_when_no_school_is_saved() { seek { _repository.setup(s => s.insert(null)); } grab (exception exception) { _result = exception; } } [test] public void should_notify_user_if_school_name_already_exists() { //bool exists = _schoolrepository.isexist(_school.name); } }

my tests passes concern that

am not supposed mock unitofwork class. when tried mocking it, couldn't studentrepository class. in using code without tests, have instantiate uow perform actions, why asked if supposed mock uow. if mock how can that? please help me if test right or need take course of study of action.

i think you're improve of mocking irepository. but, test pass because not asserting anything.

i think need setup your mock inorder desired result. here modified example:

[test] public void then_a_valid_school_should_be_saved() { var _school = new school { .... }; var expected = _school.id; _repository.setup(s => s.insert(_school)); _repository.setup(s => s.getbyid(_school.id)).returns(_school);; _repository.insert(_school); var actual = _repository.getbyid(_school.id); assert.equal(expected, actual); }

that should it. create test fail seek putting different id expected result , verify works. can utilize improve upon other test.

c# unit-testing tdd

Comments