Testing ViewModel PropertyChanged Objects

I am a "newbie" in TDD, and something I'm trying to understand is like unit test viewmodels ...

I want to make sure that the object's 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

... the code snippet in the viewmodel proti that I'm testing looks like this ...

public decimal Radius
{
    get { return _radius; }
    set
    {
        _radius = value;
        OnPropertyChanged("Radius");
    }
}
+5
source share
4 answers

, . , ( ) . , / . , .

+4

"" . , , , , .

+1

:

    [TestMethod]
    public void ChangeTrackingModelBase_BasicFunctionalityTest()
    {
        var person = new ChangeTrackingPerson();
        var eventAssert = new PropertyChangedEventAssert(person);

        Assert.IsNull(person.FirstName);
        Assert.AreEqual("", person.LastName);

        eventAssert.ExpectNothing();

        person.FirstName = "John";

        eventAssert.Expect("FirstName");
        eventAssert.Expect("IsChanged");
        eventAssert.Expect("FullName");

        person.LastName = "Doe";

        eventAssert.Expect("LastName");
        eventAssert.Expect("FullName");

        person.InvokeGoodPropertyMessage();
        eventAssert.Expect("FullName");

        person.InvokeAllPropertyMessage();
        eventAssert.Expect("");

    }

http://granite.codeplex.com/SourceControl/list/changesets

MSTest, NUnit.

+1

, : github

It uses reflection to determine if an event with a changed property was raised when the value is set public.

Example:


[TestMethod]
public void Properties_WhenSet_TriggerNotifyPropertyChanged()
{
    new NotifyPropertyChangedTester(new FooViewModel()).Test();
}
0
source

All Articles