c# - Unit testing strategies for NHibernate Repository -
i'm using nhibernate , repository pattern on large project , trying establish service layer unit testing strategy , having problems getting in head. it's possible i'm approaching unit testing incorrectly, , it's possible i'm approaching repository pattern incorrectly, i'm not sure which.
a simplified subset of scenario looks this:
public class userrepository : repositorybase, irepository { public userrepository() {} public userrepository( isession sessionparam ) { session = sessionparam; // member of repository base } public string getusernamefromemail( string emailaddress ) { return session.queryover<members>().list().where( u => u.emailaddress.tolowerinvariant() == emailadrress.tolowerinvariant() ).firstordefault().username; } }
my unit testing concept would fake nhibernate's isession , pass in 1 returned list of users fit scenario i'm trying test (for instance, email address case-insensitive) (i can't work fakeiteasy, that's question, should proceed down path). keeping in mind should not fake objects don't own, can see logic of not wanting fake isession, , i've been reading lot how 1 shouldn't test repository - that's far down unit tests.
but in basic case there's logic in repository unit test. other repository methods have potentially more logic (data validation , like, instance). know can use sqllite or similar build relatively fast integration tests reporitory, still seems me logic should unit tested.
relying on repository there (wcf) service tier that's being consumed asp.net mvc4 site.
in best of worlds build unit tests in wcf tier , fake away irepository testing, don't see how can move logic service tier without getting of users repository , returning them service tier, seems ridiculous.
so question is: piece of overall architecture here have fundamentally wrong in head?
edit
in response @wiktor-zychla's answer, here's logic regarding why wanted fake isession. in thinking specific test, want have repository use implementation of isession returns single user mixed-case email address , specfic username, pass in lower-case email address, , have return value username instructed fake use. way testing logic against know value - whether or not that's how nhibernate operate in real world isn't concern here - testing logic in repository method is. , again, know trivial , naive example solved in many other ways - it's standing in more complicated functionality want able test later.
i don't think should try fake isession. doesn't make sense - nhibernate repository concrete implementation of abstract concept , want test whether or not concrete implementation ok rather abstract further , test what? different, fake linq implementation , pretend nh follows it?
your unit tests should involve real database then, set before test starts inject temp isession repository still isession points real database.
on other hand, valid have yet implementation of repository injected in service layer when test service uses repository.
Comments
Post a Comment