Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts Testing Checking Property Change Notifications YAC Software
  Back

List

Charsets

Charts

DBExpress

Delphi

HTML

Intraweb

MSTest

PHP

Programming

R

Rhino Mocks

Software

Testing

UI Testing

VB.NET

VCL

WPF

Checking Property Change Notifications
Software quality is not only about testing (be it unit, integration, system, usability, or any other kind). It's also about writing code that detects faults as early as possible.

For instance, one thing that worries me about data binding in WPF is the lack of type safety in property change notifications. It's so easy to misspell the name of a property, or even get the case of one of the letters incorrectly...

You won't get a compile-time error, or event a run-time error; you'll just get a program that doesn't work entirely correctly in some area of one of the windows (of course it's a window that you never test, your clients never use, except for this one client...).

It's just not that easy to find this problem when testing the application, or even if you find a problem, track it down to an invalid property name...

So, what we usually have is a class that implements INotifyPropertyChanged; something like this:
  Public Class BasePropertyChanged
    Implements INotifyPropertyChanged
  
    Public Event PropertyChanged(ByVal Sender As Object,
                                 ByVal Args As PropertyChangedEventArgs)
      Implements INotifyPropertyChanged.PropertyChanged
  
    Private Sub OnPropertyChanged(ByVal PropertyName As String)
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
    End Sub
  
  End Class
First, we define a base class that knows how to notify about property changes, and then we may have a class that uses that:
  Private Class Source
    Inherits BasePropertyChanged
  
    Private _title As String
  
    Public Property Title() As String
      Get
        Return _title
      End Get
      Set(ByVal value As String)
        _title = value
        OnPropertyChanged("Title")
      End Set
    End Property
  
  End Class
Obviously, you can implement the INotifyPropertyChanged interface in any other class (that doesn't necessarily descend from BasePropertyChanged). Well, at least until this stupid fashion of blocking multiple class inheritance finally passes. But I digress... :-)

If you misspell Title in the code above, or misspell it in the XAML file, or accidentally change it, something will brake; and this may remain broken for quite a while.

So, let's add some code that will at least check those property change notifications. This will be a run-time check, but (really) simple unit tests will take care of that. Or, even if you don't have unit tests, you'll get notified that a property notification is defined incorrectly (making debugging that much easier).
  Public Class BasePropertyChanged
    Implements INotifyPropertyChanged
  
    . . .
  
    Private Sub OnPropertyChanged(ByVal PropertyName As String)
      Dim objectType As Type = Me.GetType
      Dim propertyInfo As PropertyInfo =
        objectType.GetProperty(PropertyName, BindingFlags.Instance Or BindingFlags.Public)
      If propertyInfo Is Nothing Then
        Throw New ArgumentOutOfRangeException()
      End If
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
    End Sub
  
  End Class
Here, we take our class; then we check, using reflection, whether it has the mentioned property. And if it doesn't, it throws an exception.

You might prefer to Debug.Assert, for instance, instead. Or to wrap that verification code inside #If DEBUG Then ... #End If to exclude it from the release versions of the program. Whatever's your preference here...

Top

Comments
Alas!
No comments yet...

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top

Tags

Testing

VB.NET


Related pages

AssertWasCalled and Methods Called Multiple Times

AssertWas[Not]Called and Object Properties

Rhino Mocks's AssertWasNotCalled

Visual Studio - moving coded UI tests to a new / different project results in null reference exceptions

PrivateObject and Out/ByRef parameters

PrivateObject, WithEvents, and generics

Accessing private members of base classes

CA1800:DoNoCastUnnecessarily

PrivateObject and WithEvents

The creator of this fault did not specify a Reason.

Accessing private and protected members - PrivateObject and PrivateType

VS - Test Run Error - "COM object that has been separated from its underlying RCW cannot be used"

Saving / restoring window placements in .NET

Get the TreeViewItem for an Item in a WPF TreeView

Output in MSTest Tests (VS 2010)

Automated WPF tests and "Invalid URI: Invalid port specified."

Checking "Dangling" Event Handlers in Delphi Forms

Rhino Mocks's AssertWasCalled in VB.NET

First steps with Rhino Mocks (in VB.NET)

Meaningful identifiers

Public fields vs. properties

VS Pending Tests

Automated GUI Testing

Automated GUI Testing in VMs

Automated Testing of Window Forms

Detecting Memory Leaks with DUnit