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

发布于 2024-09-26 02:08:42 字数 957 浏览 4 评论 0原文

我正在尝试为一些以编程方式创建 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;
    }

这是测试方法:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在夏天 2024-10-03 02:08:43

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

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

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

帅气尐潴 2024-10-03 02:08:43

我假设您已将 NUnit 项目添加到您的单触摸解决方案中,并让它引用单触摸项目。

它似乎不知道 Monotouch .dll 在哪里,因此将它们添加为对 NUnit 的引用。这些可以在以下位置找到:

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

这应该可以解决您的问题。

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.

在梵高的星空下 2024-10-03 02:08:43

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

这样的 运行程序 for iOS 现已存在,甚至随最新的 MonoTouch 5.1.x(测试版)版本一起提供。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文