Posts   
 
    
bosefus
User
Posts: 15
Joined: 19-Mar-2004
# Posted on: 14-Feb-2005 16:28:57   

We are starting to implement NUnit to do our unit testing. I'm having a bit of a problem though. I've read on the forum that to test with the generated code I have to have a database connection. Problem is the tests I am working on now are doing transformations to and from the generated objects to our coarse grained webservice object. My intent is not to hit the database at this point since the idea behind the tests is to test the transformation and not database connectivity or anything to do with the database. Is there any way to get around this functionality?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 14-Feb-2005 17:07:46   

You only need a database connection if you fetch data. You can of course perfectly instantiate entity objects yourself manually.

Frans Bouma | Lead developer LLBLGen Pro
bosefus
User
Posts: 15
Joined: 19-Mar-2004
# Posted on: 14-Feb-2005 17:41:08   

What I was attempting to do was make an in memory representation of a database object like so:

Ref_nav_sidebarCollection theCollection = new Ref_nav_sidebarCollection(); Ref_nav_sidebarEntity entity = new Ref_nav_sidebarEntity();

entity.Sidebar_id = sidebar_id; entity.Name = name; entity.Help_text = help_text; entity.Description = description; entity.Update_date = update_date;

Ref_nav_sidebar_itemEntity item = new Ref_nav_sidebar_itemEntity(); item.Item_id = itemCollection[0].Item_id; item.Sidebar_id = sidebar_id; item.Display_order = 1;

entity.Ref_nav_sidebar_item.Add( item );

theCollection.Add( entity ); return theCollection;

When I call entity.Ref_nav_sidebar_item.Add( item ) a call is made to the database. The transformation being tested is testing the parent and the child elements. I found a way around it but isn't the way I would do it in a perfect world. Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 15-Feb-2005 11:10:12   

Hmm, indeed that code will trigger lazy loading.

In my unittests for selfservicing, to test if lazy loading worked, I used private member reflection to set a private member to false/true or grab the actual collection instead of the collection returned by the property. What you should do with that technique is to set _alreadyFetchedRef_nav_sidebar_item to true, before you add the entity to the collection.

You can access these members using code like: bool alreadyFetchedEmployees = (bool)customers[i].GetType().BaseType.InvokeMember("_alreadyFetchedEmployees", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, customers[i], new object [] {});

This way you can also set a private member to true/false for example, making your tests work.

Frans Bouma | Lead developer LLBLGen Pro