Click here to Skip to main content
15,886,857 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Unit testing INotifyPropertyChanged [tip]

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 May 2015CPOL 10.7K   1   2
How to unit test a class that implements INotifyPropertyChanged

Introduction

One common mistake I make in programming classes that implement INotifyPropertyChanged is to forget to raise the change event in the property setter.  Therefore I added this to the list of things I commonly unit test.

For example, given the following class that implements INotifyPropertyChanged:

VB.NET
Public NotInheritable Class AggregateModel
    Implements System.ComponentModel.INotifyPropertyChanged

    <DataMember(Name:=NameOf(Name))>
    Private ReadOnly m_name As String
    ''' <summary>
    ''' The unique name by which the event sourcing model is known
    ''' </summary>
    ''' <remarks>
    ''' This is used to refer to this model in code and also in the default filename
    ''' </remarks>
    <XmlAttribute(AttributeName:="AggregateName")>
    Public ReadOnly Property Name As String
        Get
            Return m_name
        End Get
    End Property


    <DataMember(Name:=NameOf(KeyName))>
    Private m_keyName As String = "Key"
    ''' <summary>
    ''' The name by which the property which provides the unique key for this aggregate is known
    ''' </summary>
    ''' <remarks>
    ''' This defaults to "Key" if there is no meaningful business key name
    ''' </remarks>
    <XmlAttribute(AttributeName:="KeyName")>
    Public Property KeyName As String
        Get
            Return m_keyName
        End Get
        Set(value As String)
            If Not (m_keyName.Equals(value)) Then
                m_keyName = value
                OnPropertyChanged(NameOf(KeyName))
            End If
        End Set
    End Property    
    
End Class    

A unit test would look like

 

VB.NET
<TestMethod> 
Public Sub Keyname_ChangeNotification_TestMethod() 
     
     Dim expected As String = "KeyName"
     Dim actual As String = "Not Set"
     
     Dim testObj As New AggregateModel("TestObject")
     
     AddHandler testObj.PropertyChanged, New PropertyChangedEventHandler( 
         Sub(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) 
            actual = e.PropertyName 
        End Sub ) 
        
    testObj.KeyName = "My key"
    testObj = Nothing 
    
    Assert.AreEqual(expected, actual) 
    
End Sub 

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
QuestionDataMember on private Pin
Sacha Barber11-May-15 5:08
Sacha Barber11-May-15 5:08 
AnswerRe: DataMember on private Pin
Duncan Edwards Jones11-May-15 8:43
professionalDuncan Edwards Jones11-May-15 8:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.