Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing ViewModel PropertyChanged Events

I am a "beginner" at TDD, and something I am trying to figure is how to unit test viewmodels...

I am wanting to make sure that a property ProeprtyChanged event is fired... I have the following test using nunit.

[Test]        
public void Radius_Property_Changed()
{
    var result = false;
    var sut = new MainViewModel();
    sut.PropertyChanged += (s, e) =>
    {
        if (e.PropertyName == "Radius")
        {
            result = true;
        }
    };

    sut.Radius = decimal.MaxValue;
    Assert.That(result, Is.EqualTo(true));
}

Is this the cleanest way to do this, or is there a better way to test this property

... snippet of code in the viewmodel of the propety I am testing looks like this...

public decimal Radius
{
    get { return _radius; }
    set
    {
        _radius = value;
        OnPropertyChanged("Radius");
    }
}
like image 800
Mark Pearl Avatar asked Mar 14 '12 18:03

Mark Pearl


1 Answers

This is pretty much how you do that. There's not much else to do here given it's pretty simple (and boring) code. It might be worth to wrap that in your own reusable library/tool. Or even better, use existing code.

like image 84
k.m Avatar answered Oct 01 '22 22:10

k.m