从 NUnit 测试代码创建 iOS UI 组件
我正在尝试为一些以编程方式创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在可以肯定的是,根本问题是,如果您不是在 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...
我假设您已将 NUnit 项目添加到您的单触摸解决方案中,并让它引用单触摸项目。
它似乎不知道 Monotouch .dll 在哪里,因此将它们添加为对 NUnit 的引用。这些可以在以下位置找到:
这应该可以解决您的问题。
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:
That should solve your problem.
这样的 运行程序 for iOS 现已存在,甚至随最新的 MonoTouch 5.1.x(测试版)版本一起提供。
Such a runner for iOS now exists and it even ships with the latest MonoTouch 5.1.x (beta) releases.