Friday 15 February 2013

c# - Creating Unit Tests for first time -



c# - Creating Unit Tests for first time -

i creating new project using asp.net mvc , entity framework 6 , visual studio 2013. create tests straight start, else never created, unsure when , test.

in solution have next projects

data - ef context , configuration files map entities database entities - poco objects services - contains business logic each entity. loads , saves info ef context web - webapi , mvc website

now need add together test project info project? part of me says "yes of course" because want create sure info beingness saved in right place, part of me thinks repeated in services test project?

here basic test set together, test, or pointless?

[testclass] public class companytest { private mycontext _context; [testinitialize] public void initialize() { // set our context _context = new mycontext(); // add together test info var companies = gettestdata(); // save info companies.foreach(s => _context.companies.add(s)); _context.savechanges(); } [testcleanup] public void cleanup() { var companies = _context.companies.tolist(); // remove companies companies.foreach(s => _context.companies.remove(s)); _context.savechanges(); } [testmethod] public void addcompanies_shouldreturntwo() { // info var companies = _context.set<company>().tolist(); // check values assert.areequal(2, companies.count); } [testmethod] public void getcompany_checkdataineachfield() { var companies = _context.set<company>().tolist(); company company; // first company company = companies[0]; assert.areequal("abc construction", company.name); assert.areequal("abc001", company.reference); assert.areequal("10-15 templatestreet", company.street); assert.areequal("brierley hill", company.town); assert.areequal("somerset", company.county); assert.areequal("england", company.country); assert.areequal("ty1 1ab", company.postcode); // sec company company = companies[1]; assert.areequal("baseline design", company.name); assert.areequal("bad0023", company.reference); assert.areequal("23-25 hedlyn road", company.street); assert.areequal("oxford", company.town); assert.areequal("oxford", company.county); assert.areequal("england", company.country); assert.areequal("ox1 4fg", company.postcode); } private list<company> gettestdata() { // companies homecoming new list<company> { new company{ name="abc construction", reference="abc001", street="10-15 templatestreet", town="brierley hill", county="somerset", country="england", postcode="ty1 1ab",dateadded=datetime.now}, new company{ name="baseline design", reference="bad0023", street="23-25 hedlyn road", town="oxford", county="oxford", country="england", postcode="ox1 4fg",dateadded=datetime.now} }; } }

so need test projects services , web?

if knows of reference material explain when , how should setup tests, please allow me know well.

your question wide elaborate answer, personal approach test every layer of application, sure info , service layer. in service layer goal should test business logic correctness, info should mocked or faked, isolating layer. mentioned before there lot of ways test application, if want test javascript, give thought take @ series of post:

generic repository , unit of work pattern, entity framework, unit testing, autofac ioc container , asp.net mvc - part 1 generic repository , unit of work pattern, entity framework, unit testing, autofac ioc container , asp.net mvc - part 2 generic repository , unit of work pattern, entity framework, unit testing, autofac ioc container , asp.net mvc - part 3 generic repository , unit of work pattern, entity framework, unit testing, autofac ioc container , asp.net mvc - part 4

or start msdn :

chapter 13: unit testing web applications

there lot of books , videos on topic, take time, it's not easy learning curve, must have knowledge on developer tool belt

c# asp.net-mvc entity-framework unit-testing testing

No comments:

Post a Comment