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
Post a Comment