blog.kenne
January 24, 2010
Testing the PropertyChanged event

Testing a ViewModel and it’s properties may look quite easy at first. But ViewModels are quite treacherous to their nature. There’s no guarantee that the value of a property in a unit test is the same as what the view is displaying, because of NotifyPropertyChanged (if no PropertyChanged is fired, nothing is updated in the view, right?).

And really, what use do you have of a unit test that you can’t fully rely on?

I thought about solving this issue a few days back and came up with a small extension helper method that increases the test accuracy without increasing the complexity too much.

Given this Context:

    [TestFixture]

    public class When_updating_a_property_which_a_WpfControl_is_binding_to

    {

        private ViewModel _viewModel;

 

        [SetUp]

        public void Context()

        {

            _viewModel = new ViewModel();

            _viewModel.PropertyWithWPFBinding = 100;

        }

 

        [Test]

        public void It_should_display_the_correct_value_since_it_has_fired_a_propertyChanged_event()

        {

            Assert.That(_viewModel.PropertyWithWPFBinding, Is.EqualTo(100));

        }

    }


The test will certainly pass both with or without PropertyChanged (which is bad).
So with a small modification, and with the use of ShouldBeDisplayedAs():

    [TestFixture]

    public class When_updating_a_property_which_a_WpfControl_is_binding_to

    {

        private ViewModel _viewModel;

 

        [SetUp]

        public void Context()

        {

            _viewModel = new ViewModel();

            _viewModel.MakePropertyChangedAware();

            _viewModel.PropertyWithWPFBinding = 100;

        }

 

        [Test]

        public void It_should_display_the_correct_value_since_it_has_fired_a_propertyChanged_event()

        {

            _viewModel.ShouldBeDisplayedAs(x => x.PropertyWithWPFBinding, 100);

        }

    }

The test will fail if no PropertyChanged was fired. To avoid confusion I also added a more descriptive fail reason:

error message_1.png

Pretty sweet.

What the code really does is simply creating a listener for all ProperyChanged events that has been fired with the MakePropertyChangedAware() method. And records the values of the ViewModel only if a PropertyChanged is fired.

If you want a sample project and the source, download here.

/love Carl


Comments ( 0 ) :: Post A Comment! :: Permanent Link


January 17, 2010
Interesting session coming up

Ever wondered whats cooking on the darkside theese days? Renowed Rickard Öberg drops by the Stockholm office to show som new tricks from his Qi4j framework.  The session is on the 26th januari and free of charge (read more here).

Sounds really interesting and i can really recommend it, unfortunatley I wont be there since I already got a date with Jim Coplien that day.  

 

By the way:
Google just told me that Roger Alsing is working on a .Net port... awesome!

/love Carl


Comments ( 0 ) :: Post A Comment! :: Permanent Link


October 18, 2009
Exciting upcoming weeks - a prelude to Øredev

The upcoming weeks looks promising, full of interesting seminars and "geek gatherings" all over Stockholm. In other words, it it's the perfect warmup for the King of Developer Conferences - Øredev in the first week of November.


Oktober:
  Mo-day / 19th 
    -
 Entity Framework 4.0 (Dotway)
    - Using the cloud for testing (Jayway)

  Tu-day / 20th
    - Geek Meet with Molly and HTML 5 (Bwin)

  We-day / 21th
    - Limited WIP Society (Kanban) (Avega)

  Thu-day / 22th
    - Digital vs Fysiskt (multitouch) (Inuse)

  Mo-day / 26th
    - Det behövs mer än bara user stories  (Agical, GeekNight) 


November:
  Mo-day
-> Fri-day / 2th - 6th
    -
Øredev (Malmö)

 

And the best of all... They're all free ( ... well almost all of them :-) )

See you there
/love Carl


Comments ( 0 ) :: Post A Comment! :: Permanent Link


September 18, 2009
Cybercrime: The Devil in The Dark - 21/9

Security expert Andy Malone (whose name sounds like a chicago gangster from the fifties) drops by Stockholm on monday the 21th (september). Andy will hold a session about security for developers.

Sounds interesting (and it's of course free), I think I'll pop by.

read more and register here

/love Carl


Comments ( 0 ) :: Post A Comment! :: Permanent Link


September 13, 2009
INotifyPropertyChanged - A remedy for the plague

I stumble over this nice little extension class the other day while out surfing.

Instead of passing around a hard coded string for NotifyPropertyChanged like this:

private string firstName;

public string FirstName

{

    get { return firstName; }

    set

    {

        firstName = value;

        OnPropertyChanged("FirstName");

    }

}

 A lamda expression takes care of the dirty stuff:

PropertyChanged.Notify(()=>this.FirstName);

This magic is accomplished with a small extension class which can be downloaded at Einars blogg, over here

Pretty cool.

/love Carl


Comments ( 0 ) :: Post A Comment! :: Permanent Link


About Me

Right now:
WPF
Composite WPF
DDD
Clean Code


Recent Posts
Menu
Calendar
« February 2010 »
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728

Friends
    Links


    Page 1 of 4
    Last Page | Next Page