Search code examples
c#unit-testinginotifypropertychangedrhino-mocks

Unit Test INotifyPropertyChanged using Rhino Mocks


I have a class that implements INotifyPropertyChanged and I need to test if this interface is implemented correctly. I want to do this using a Rhino Mock object.

class MyClass : INotifyPropertyChanged
{
    public int X
    {
        get => ...;
        set => ... // should check if value changes and raise event PropertyChanged
    }
}

What I want to test, is that when X changes value, that event PropertyChanged is called exactly once, with the proper parameters.

MyClass testObject = new MyClass();

// the mock:
PropertyChangedEventHandler a = MockRepository.GenerateMock<PropertyChangedEventHandler>();
testObject.PropertyChanged += a;

// expect that the mock will be called exactly once, with the proper parameters
a.Expect( (x) => ???)
 .Repeat()
 .Once();

// change X, and verify that the event handler has been called exactly once
testObject.X = testObject.X + 1;

a.VerifyAllExpectations(); ???

I think I'm on the right path, but I can't get it working.


Solution

  • Sometimes, there really is no need to use a mock if there are not knock-on effects of using the real thing.

    The following simple example creates an instance of the delegate and verifies the expected behavior

    What I want to test, is that when X changes value, that event PropertyChanged is called exactly once, with the proper parameters.

    [TestClass]
    public class MyClassTests {
        [TestMethod]
        public void Should_Call_PropertyChanged_Once() {
            //Arrange            
            //Store calls
            IDictionary<string, int> properties = new Dictionary<string, int>();
            PropertyChangedEventHandler handler = new PropertyChangedEventHandler((s, e) => {
                if (!properties.ContainsKey(e.PropertyName))
                    properties.Add(e.PropertyName, 0);
    
                properties[e.PropertyName]++;
            });
    
            MyClass testObject = new MyClass();
            testObject.PropertyChanged += handler;
    
            string expectedPropertyName = nameof(MyClass.X);
            int expectedCount = 1;
    
            //Act
            testObject.X = testObject.X + 1;
    
            //Assert - using FluentAssertions
            properties.Should().ContainKey(expectedPropertyName);
            properties[expectedPropertyName].Should().Be(expectedCount);
        }
    
        class MyClass : INotifyPropertyChanged {
            public event PropertyChangedEventHandler PropertyChanged = delegate { };
    
            void raisePropertyChanged([CallerMemberName]string propertyName = null) {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            int x;
            public int X {
                get => x;
                set {
                    if (value != x) {
                        x = value;
                        raisePropertyChanged();
                    }
                }
            }
        }
    }