How to ignore PropertyInfo data used for Mocking objects in unit tests.

How to ignore PropertyInfo data used for Mocking objects in unit tests.

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


jamie.clayton posted on Wednesday, September 26, 2012

I just want the communities feedback on a unit test issue I have.

Use Case

So I figured  I can just override IsValid and IsSavable as I've done bellow.

        ''' <summary>
        ''' Ensure that this object is valid and ignore any extended propertyInfo classes that are used in calculations but not saved to the database.        
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>In this case we are passing in mocked Product rule CSLA object to this class for unit testing, and it is incomplete (invalid). If ByBase.IsValid is used, then that invalid property is detected and it blocks saving the record.</remarks>
        Public Overrides ReadOnly Property IsValid() As Boolean
            Get
                Return MyBase.IsSelfValid       'ByBase.IsValid checks all child object validity (which for product member contains incomplete payment rules for unit tests)
            End Get
        End Property
 
        ''' <summary>
        ''' We want to ignore one of the PropertyInfo that includes data that doesn't need to be saved.
        ''' During unit testing these products supplied to the class don't exist and aren't valid. We want to ignore that data during the validation checks. 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property IsSavable As Boolean
            Get
                Return MyBase.IsSelfValid
            End Get
        End Property

Questions

  1. Can you use Attributes, DataAnnotations or PropertyInfo({options...} to effectively ignore a CSLA backing property?
  2. Is Overriding the IsValid, IsSavable methods the easiest approach?
  3. Long term, how easy is it to mock a CSLA business object at the object level, rather than the DataPortal level, without resorting to a Mocking framework? Would love to hear about which of the mocking frameworks work well with CSLA.

 

 

JonnyBee replied on Thursday, September 27, 2012

Hi Jamie,

If you have Rockys Using CSLA 4 Objects (book 2)  then look up

Understanding how to handle these relationship types is important for your application and some times also performance and memory consumption.

Basically - if you have an object that is not a child (owning) reference then you must use private backing fields.

Private backing fields are not automatically checked by IsValid/IsDirty etc (you must override and add these checks when applicable).

And private backing fields may also have attributes like

1. You cannot use attributes to ignore a managed backing field.
    Private backing fields is not automatically checked and you can add attributes to exclude a private field from snapshot and serialization.

2. I would change ProductProperty to use a private field - and thus it is excluded from IsDirty/IsValid.

3. I prefer to use "fake" objects of the real type.
We typically use public constructor (due to limitations in SL/WinRT) and so it is easy to create fake objects with a "given" state. You can also create an ObjectAccessor class derived from ObjectFactory to get access to f.ex BypassPropertyChecks/LoadProperty etc and load property values without triggering rules.

Copyright (c) Marimer LLC