从NUnit测试代码创建iOS UI组件

Dav*_*les 6 nunit uibutton uikit xamarin.ios

我正在尝试编写一些以编程方式创建UIButton的代码的单元测试,但是当我从测试中调用此代码时,我得到了一个NullReferenceException.单步执行调试器,看起来像UIButton.FromType()返回null.

这是我正在测试的方法:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

这是测试方法:

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

我猜我需要做一些神奇的环境才能让MonoTouch.UIKit在真正的应用程序之外工作.任何提示?(如果不可能,建议采用其他方法?)

Dav*_*les 1

现在可以肯定的是,根本问题是,如果您不是在 iPhone 上或 iPhone 模拟器中运行,则无法调用必要的本机 API 来实例化组件。

也许有一天有人会为 Monotouch 编译 NUnit 并编写 iOS 测试运行程序......