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?)
While unit testing seeks to create a rapid and regular feedback loop for developers to gain confidence in correctness of the code, UI testing validates the application as a whole from an end user's perspective to ensure that the final product performs as expected by users.
The easiest way to add a unit test target to your project is to select the Include Tests checkbox when you create the project. Selecting the checkbox creates targets for unit tests and UI tests. To add a unit test target to an existing Xcode project, choose File > New > Target.
Pretty sure at this point that the fundamental problem is, if you're not running on the iPhone or in the iPhone simulator, there's 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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With