Creating iOS UI components from NUnit test code

瘦欲@ 提交于 2019-12-10 13:16:40

问题


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?)


回答1:


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...




回答2:


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.




回答3:


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

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



来源:https://stackoverflow.com/questions/3877057/creating-ios-ui-components-from-nunit-test-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!