Creating iOS interface components from NUnit test code

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

Here is 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 is 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 guess there is some kind of magic that I need to do from an environmental point of view in order to get MonoTouch.UIKit to work outside of the real application. Any hints? (And if this is not possible, alternative approaches are proposed?)

+6
uikit uibutton nunit
source share
3 answers

Quite precisely, the main problem is that if you are not working on the iPhone or in the iPhone simulator, there is no way to call the necessary native APIs to instantiate the components.

Maybe someday someone will compile NUnit for Monotouch and write an iOS test runner ...

+1
source share

I'm going to assume that you added the NUnit project to your monotouch solution and referenced the monotouch project.

He does not seem to know where the Monotouch.dll files add them as a reference to your NUnit. They can be found at:

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

This should solve your problem.

0
source share

Maybe someday someone will compile NUnit for Monotouch and write an iOS test runner ...

Such a runner for iOS now exists, and it even comes with the latest MonoTouch 5.1.x (beta) releases.

0
source share

All Articles