partial class DaveKehring : IProgrammer { }  RSS 2.0
# Monday, October 06, 2008

If you have a class that contains an event, you'll want to write a unit test to test that the event gets fired at the right time and under the right conditions. The problem is that you would typically hook up an event handler in your unit test class that can run an assert to check if the event gets fired:

private bool _fired;

[Test]
public void CheckVisiblityEvent()
{
    _fired = false;
    ImageGroupListItemViewModel vm = ImageGroupListItemViewModelFactory.Build();
    vm.VisibilityChanged += new ImageGroupListItemViewModel.ImageGroupVisibilityChangedEventHandler(vm_VisibilityChanged);

vm.Visible = true;
Assert.IsTrue(_fired); } void vm_VisibilityChanged(object sender, ImageGroupVisibilityEventArgs args) { _fired = true; }

This is a bit sloppy. Instead, we could improve on this by using and anonymous method:

[Test]
public void CheckVisiblityEvent()
{
    bool fired = false;
    ImageGroupListItemViewModel vm = ImageGroupListItemViewModelFactory.Build();
    vm.VisibilityChanged += delegate(object sender, ImageGroupVisibilityEventArgs e)
    {
        fired = true;
    };
    vm.IsVisible = true;
    Assert.IsTrue(fired);
}

More compact and easier to follow. Plus, the test code is all in one place. We can improve this even more with a lambda expression:

[Test]
public void CheckVisiblityEvent()
{
    bool fired = false;
    ImageGroupListItemViewModel vm = ImageGroupListItemViewModelFactory.Build();
    vm.VisibilityChanged += (s, e) => fired = true;
    vm.IsVisible = true;
    Assert.IsTrue(fired);
}

 

Monday, October 06, 2008 10:35:17 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
NUnit | Unit Testing
# Tuesday, August 12, 2008

While trying to create unit tests for view models (using the Model-View-ViewModel pattern), I came across a problem while trying to execute unit tests from NUnit. The error I recevied was:

The calling thread must be STA, because many UI components require this.

A quick search on Google yielded an answer.

However, the configuration file in the post was incorrect and was noted by a commentor. For expediency, I have included the correct configuration text below:

<configSections>
   <
sectionGroup name="NUnit">
      
<section type="System.Configuration.NameValueSectionHandler" name="TestRunner"></section>
   
</sectionGroup>
</
configSections>

<NUnit>
   <
TestRunner>
      
<add value="STA" key="ApartmentState"></add>
   </
TestRunner>
</
NUnit>

 

Tuesday, August 12, 2008 8:52:59 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
M-V-VM | NUnit | Unit Testing | WPF
Navigation
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Dave Kehring
Sign In
Statistics
Total Posts: 11
This Year: 0
This Month: 0
This Week: 0
Comments: 2
Themes
Pick a theme:
All Content © 2012, Dave Kehring
DasBlog theme 'Business' created by Christoph De Baene (delarou)