c# - Can a unit test perform double-duty by accepting a boolean value as a switch, or should I write two separate tests that duplicate the test code? -
i have method operations depend on dependents, below. still value doing unit test? because unit test not testing business logic, rather mocks.
unit test below:
please note method's opetions determined expectedcustomervalidality
, setup test. mostly, logic determined mocks (e.g. setup(c => c.isvalid())
.
[test] [testcase(true)] [testcase(false)] public void addcustomer(bool expectedcustomervalidality) { //using moq companyrepositorymock.setup(c => c.getbyid(it.isany<int>())).returns(new company()); customervalidatormock.setup(c => c.isvalid(it.isany<customer>())).returns(expectedcustomervalidality); var customer = new customer { firstname = "firstname", surname = "surname", company = new company { id = 1 } }; var addcustomer = customerservicesut.addcustomer(customer); assert.areequal(expectedcustomervalidality,addcustomer); }
production code below:
public class customerservice : icustomerservice { private icompanyrepository companyrepository; private icustomerrepository customerrepository; private icustomervalidator customervalidator; public customerservice(icompanyrepository companyrepository, icustomerrepository customerrepository, icustomervalidator customervalidator) { this.companyrepository = companyrepository; this.customerrepository = customerrepository; this.customervalidator = customervalidator; } public bool addcustomer(customer customer) { customer.company = companyrepository.getbyid(customer.company.id); ; if (customervalidator.isvalid(customer)) { customerrepository.addcustomer(customer); return true; } return false; } }
questions:
- is addcustomer() required unit-testing?
if so, current unit test performing correct way?
1 if not, proper way unit test it?
i don't this. here's why: test method names should reflect method under test, preconditions of test are, , expect result of test be:
public bool addcustomer_customerisvalid_shouldreturntrue() public bool addcustomer_customerisinvalid_shouldreturnfalse()
now if want to, can refactor core testing logic own method eliminate code duplication, , call method 2 above methods. refactored method not test case; it's helper method actual test cases.
example:
[test] public void addcustomer_customerisvalid_shouldreturntrue() { var result = addcustomertest(true); assert.istrue(result); } [test] public void addcustomer_customerisinvalid_shouldreturnfalse() { var result = addcustomertest(false); assert.isfalse(result); } public void addcustomertest(bool expectedcustomervalidality) { //using moq companyrepositorymock.setup(c => c.getbyid(it.isany<int>())).returns(new company()); customervalidatormock.setup(c => c.isvalid(it.isany<customer>())).returns(expectedcustomervalidality); var customer = new customer { firstname = "firstname", surname = "surname", company = new company { id = 1 } }; var result= customerservicesut.addcustomer(customer); return result; }
Comments
Post a Comment