views:

74

answers:

1

I'm trying to write a unit test for some code that programmatically creates UIButtons, but when I call this code from the test, I get a NullReferenceException. Stepping through in the debugger, it looks like UIButton.FromType() returns null.

Here's the method I'm testing:

    public UIButton makeButton (String title, Action<IWelcomeController> action)
    {
        UIButton button = UIButton.FromType (UIButtonType.RoundedRect);
        // button is null here
        button.SetTitle(title, UIControlState.Normal);
        button.TouchUpInside += (sender, e) => {
            action(controller);
        };
        return button;
    }

And here's the test method:

    [Test()]
    public void TestMakeButtonTitle ()
    {
        String title = "Elvis";
        UIButton button = GetFactory().makeButton(title, delegate(IWelcomeController w) {});
        Assert.AreEqual(title, button.Title(UIControlState.Normal));
    }

I'm guessing there's some magic I need to do environment-wise in order to get MonoTouch.UIKit to work outside of a real application. Any hints? (And if it isn't possible, suggested alternative approaches?)

A: 

I'm going to assume you've added the NUnit project to your monotouch solution and had it reference the monotouch project(s).

It seems it doesn't know where the Monotouch .dlls are so add them as a reference to your NUnit. These can be found at:

~/Developer/Monotouch/usr/lib/mono/<version>

That should solve your problem.

Luke
If that were the case, I would expect the code not to compile, or to blow up when I try to load the UIButton class, or call FromType(). I wouldn't expect the FromType() call to succeed but return null.
David Moles