CSLA unit testing MVC no DAL

CSLA unit testing MVC no DAL

Old forum URL: forums.lhotka.net/forums/t/11556.aspx


zbend posted on Tuesday, August 28, 2012

I'm new to CSLA, and also unit testing in general, yet I'm newly assigned to a project that uses CSLA and they want to now write unit tests for it, and I'm lost.

The project is a ASP.NET MVC 3 (c#) application, with a csla library that has umm inline? dataportal methods. The actual data fetching is done in the csla business object itself, there is no DAL, just calls to SQL stored procedures right in the object that then call LoadProperty. It seems like this is not possible to unit test I can't mock the csla objects and I have no DAL, is this true does it need to be totally refactored\rewritten? Or am I missing some approach? To get a bit more specific, I have an HTML helper method that takes one of my csla objects and returns and MVCHtmlString, I want to test what the helpers is doing how can I accomplish this? Any help, or samples you can post me to is appreciated.

 

------

Not to get off topic but I'm also failing to see the point of using CSLA in a scenario like this where it will always be an asp.net web app connecting to a MSSQL database, its not using any of the power of the dataportal and its ntier-ed abbstraction-ness right? It could use some of the authorization stuff or validation rules of CSLA (but it doesn't appear to be) I feel like if I'm allowed to refactor\rewrite it I would be better off not using CSLA at least for this app and given my unfamiliarity with CSLA, thoughts? Thanks.

 

 

RockfordLhotka replied on Tuesday, August 28, 2012

You are correct that you'll need to refactor the DataPortal_XYZ methods to invoke a DAL so you can create a mock DAL for testing. That is usually not terribly difficult, but it does require repetative effort.

In terms of the value of CSLA, there are a few primary benefits. CSLA

I recognize that many of those benefits are long-term. Personally I'm OK with that, because the real cost of software isn't in its original creation. It is in the cost of maintaining and updating the software over the typical 7-15 year timeframe during which that software is in use.

zbend replied on Tuesday, August 28, 2012

Thanks very much for the reply!,

Still a little unclear how I would test my HTML Helper example thouhg. My CSLA obejct would be using the mock DAL thats cool, but how would I get an instance of my CSLA object for the helper or should I rework the helper to take a DTO? I could make an instance with the 'top' object, but it has children objects that don't really  have a creation method, if that makes sense. So my top object has a topObject.NewTop(); but its children I just call topObject.Childrens.AddNew(), so I'd have to create a top object then call its .Childrens.AddNew() to get my child csla instance. Is that the right approach or should I use the DTO?

Thanks.

 

JonnyBee replied on Wednesday, August 29, 2012

Hi,

You may want to look at the MEF Sample projects in CslaContrib that show sample on how to plug in DataAccess in either DataPortal_XYZ methods or use the ObjectFactory pattern. This sample shows usage of Repository pattern and ObjectFactory pattern. Goto source code and download the latest source code.

In order to achieve this - be prepared to refactor the SQL code to another layer and just return a IDataReader - that can be mock'ed to return fake data.You can create a mockable datalayer by using IDataReader as a contract to the database. (This is not shown in the MEF sample tho' - but search the forum as there has been a few threads on this.)

You can get a child object also by either using DataPorta.CreateChild with parameters or add public static factory methods or public constructor. CSLA has typically had private constructor to force the use of factory methods but with the limits imposed from SL we now tend to make all the constructors public.

 

 

zbend replied on Wednesday, August 29, 2012

Great, thanks for all the help,  it sounds like I have a fair amount of work to do, simple and repetative as it may be, but it makes sense to me now, Thanks!

 

Copyright (c) Marimer LLC