从 NUnit 测试代码创建 iOS UI 组件 [英] Creating iOS UI components from NUnit test code

查看:58
本文介绍了从 NUnit 测试代码创建 iOS UI 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
    }

这是测试方法:

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

为了让 MonoTouch.UIKit 在真实应用程序之外工作,我想我需要在环境方面使用一些魔法.任何提示?(如果不可能,建议的替代方法?)

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

推荐答案

目前很确定根本问题是,如果您不是在 iPhone 或 iPhone 模拟器中运行,则无法调用必要的用于实例化组件的原生 API.

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.

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

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

这篇关于从 NUnit 测试代码创建 iOS UI 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆