Home > View Post

NullableExtender revisited

When I first wrote the post about DataBinding and Nullable types I never thought that it would become so popular. So without further ado and 'better late than never', here is the source code.

Whilst we're here, let's take the opportunity to look at how I 'unit' tested the NullableExtender.

Normally, I like my unit tests to be as close to the definition of pure unit test as possible, but in this case we're using a component that needs to play well with DataBinding so we're going to implement our test on an actual Windows Form to provide the necessary framework at minimum cost and minimum hassle.

So let's create a new Windows Form (in our test project) and add the NullableExtender component through the designer as normal.

And now for a little trick

To enable unit testing on your Form simply add the [TestClass] attribute
Note I'm using the Visual Studio Testing facility in this example, but you could equally use Nunit where you'd apply the [TestFixture] attribute instead.

// Turn your form into a Test.
public partial class NullableExtenderTest : Form

Now open the form and use the designer just as you would at any other time. In my example, I have added two TextBoxes, a BindingSource and a NullableExtender, of course.

Nullable Extender Test Form

To simulate the binding in the form the following code is run in the [TestInitialize] method:

public void TestInitialise()
    _entityMock = new EntityObject();
    entityObjectBindingSource.DataSource = _entityMock;
    // enable simulation of binding.

And finally, we need to test the Extender:

public void TestBindingBeforeNullable()
    // The different Focus calls simulates databinding.
    txtBindingBeforeNullable.Text = "1";
    Assert.AreEqual(1, _entityMock.NullableInt);
    txtBindingBeforeNullable.Text = string.Empty;
    Assert.AreEqual(null, _entityMock.NullableInt);

I like :D

Tags: WinForms.NET

Bruusi Post By Bruusi
10:07 AM
02 Nov 2006

» Next Post: How to test if a Type supports nulls
« Previous Post: DataBinding made simple

Comments are closed for this post.

© 2005 - 2022 Josh Twist - All Rights Reserved.