c# - Ways of unit testing data access layer -
i have trying effective way in unit testing info access layer in c#. i'm primary java developer , have used c# 6 months, in past i've used library called dbunit test against known state database. haven't been able find similar active library can used, closest seems ndbunit hasn't been active awhile now.
there seems lot of conflicting methods on how , why in c#. ideally want test info access layer using mocking without need connect database , unit test store procedure in separate set of tests.
in scheme i'm working on, info access layer utilize ado.net (without utilize of entity framework) phone call store procedures on sql server.
below sample code of have work with; go downwards mocking path, have able mock sqlcommand (using idbcommand) and/or mock sqlconnection.
so question seems best way (if there such thing) this? far way create proxy object passed constructor can homecoming mocked sql* objects testing.
i haven't had chance @ available c# mock libraries available yet.
public class customerrepository : icustomerrepository { private string connectionstring; public customerrepository (string connectionstring) { this.connectionstring = connectionstring; } public int create(customer customer) { sqlparameter paramoutid = new sqlparameter("@out_id", sqldbtype.int); paramoutid.direction = parameterdirection.output; list<sqlparameter> sqlparams = new list<sqlparameter>() { paramoutid, new sqlparameter("@name", customer.name) } sqlconnection connection = getconnection(); seek { sqlcommand command = new sqlcommand("store_proc_name", connection); command.commandtype = commandtype.storedprocedure; command.parameters.addrange(sqlparams.toarray()); int results = command.executenonquery(); homecoming (int) paramoutid.value; } { closeconnection(connection); } } }
it's unfortunate can't find tool puts database known state , lets run customerrepository against database test customerrepository. however, reply not start using mocks mock out of ado calls. doing that, end creating unit test doesn't test logic: it's testing code written way think should written.
let's end writing sql insert command create client in sql database. let's we're making alter client table has different fields (that breaks our insert command) , should using stored procure create customer. test mocks still pass, though implementation it's testing broken. additionally, if fixed implementation utilize stored procedures, unit tests fail. point of unit test if continues pass when should fail fail when fixed system?
see this question possible alternatives. looks marked reply end using dbunit in c# using ikvm.
so, there might alternative avenues go on explore, mocking ado calls going lead brittle tests don't test important.
c# unit-testing testing persistence data-access-layer
No comments:
Post a Comment