nunit - Unit Testing Insert Empty Record Should Throw Error -


i new unit testing , trying test new employee record should not inserted database. when call context.savechanges() not throw error in unit test, when try out in controller throws error expected.

i guessing employee entity isn't being added context in unit test when call savechanges() nothing being saved? appreciated!

unit test

[test] [expectedexception(typeof(dbentityvalidationexception))] public void shouldnotsaveemptyemployee() {     var mockset = new mock<dbset<employee>>();      var mockcontext = new mock<sqlcontext>();     mockcontext.setup(m => m.employees).returns(mockset.object);      var sut = new employeerepository(mockcontext.object);     sut.save(new employee()); } 

repository:

public void save(employee employee) {     if (employee.employeeid > 0)     {         context.entry(employee).state = entitystate.modified;     }     else     {         context.employees.add(employee);     }      context.savechanges(); } 

when running unit test, context inside employeerepository mocked object , there method savechanges has no implementation. when intiailizing employeerepository should give actual sqlcontext constraint checked. class under test here employeerepository seem testing functionality in sqlcontext. test doesn't seem belong here.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -