Tuesday, 15 February 2011

c# - Nunit --Data insert test issue -



c# - Nunit --Data insert test issue -

i new nunit testing. trying write test method method inserts info in database.

my code like:

using system; using system.collections.generic; using system.linq; using system.web; using nunit.framework; using mbt.entities; [testfixture] public class tagstoretest { private tag testtag; public tagstoretest() { this.testtag = new tag(); } [test] public void inserttagtest() { tagstore tagst = new tagstore(); bool isinserted = tagst.inserttag(this.testtag); assert.istrue(isinserted); } [setup] public void setup() { this.testtag.tagname = "testtagthroughnunit"; } [teardown] public void teardown() { } }

and actual tagstore code like

using system; using system.collections.generic; using system.linq; using system.web; using mbt.datastore; using system.data; using mbt.entities; public class tagstore { private string _connectionstring = string.empty; public tagstore() { this._connectionstring = sqlhelper.getconnectionstring(); } public bool inserttag(tag tag) { bool isinserted = false; using (databasehelper helper = utility.dbhelper) { int tagsadded = helper .addinputparameter("@tag", tag.tagname) .addinputparameter("@parenttagid", tag.parenttagid) .executenonquery("proc_inserttags", commandtype.storedprocedure); if (tagsadded > 0) { isinserted = true; } } homecoming isinserted; } }

when run test error: tagstoretest.inserttagtest: system.nullreferenceexception : object reference not set instance of object.

and code line tagstore tagst = new tagstore(); highlighted in red.

i dont know whats going wrong because project builds error when run test.

i see tagstore constructor setting connectionstring member. seek debug returned

sqlhelper.getconnectionstring();

your sqlhelper playing tricks.

i suggest seek using sort of mocking technique isolate tests. e.g. can mock sqlhelper object returns desired connectionstring. there plenty of framework out there e.g. moq.

c# asp.net unit-testing nunit

No comments:

Post a Comment