嘲笑了WifiManager为Android单元测试 [英] Mocking up WifiManager for Android Unit Testing

查看:109
本文介绍了嘲笑了WifiManager为Android单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一些单元测试的一对夫妇的依赖WifiManager和返回ScanResults类。我想要做的是能控制的,我接受,以测试各种不同的条件下ScanResults。

I'm trying to implement some unit tests for a couple of classes that rely on WifiManager and the returned ScanResults. What I'd like to do is be able to control the ScanResults that I'm receiving in order to test a variety of different conditions.

不幸的是它是相当困难的,我成功地模拟了WifiManager(虽然我想我可以通过我的MockWifiManager其构造空引用)。这仅是我的第一个问题,因为一旦我有一个MockWifiManager一起玩(如果这也可以!)我将不得不成功创建不具有公共构造函数(想象一下,它是由一些工厂创建的地方)我的测试ScanResults。

Unfortunately it's been quite difficult for me to successfully mock up WifiManager (though I suppose I can pass its constructor null references in my MockWifiManager). This will only be my first problem as once I have a MockWifiManager to play with (if this even works!) I will have to successfully create my test ScanResults which does not have a public constructor (Imagine it's created by some factory somewhere).

问题: 有了它,没有一个公共的构造函数,我可以甚至扩展呢?

Questions: With it not having a public constructor can I even extend it?

我要对这个都错了?我经常被问及如何做一个特定的任务,但实际上他们正在试图解决不同的问题走错了路的问题,也许这就是我在这里做什么?

Am I going about this all wrong? I often get asked questions about how to do a specific task but really they're trying to solve a different problem the wrong way, maybe that's what I'm doing here?

我很新至Android所以不必嘲笑了所有这些功能一直在努力,至少可以说。

I'm very new to android so having to mock up all of this functionality has been trying to say the least.

感谢您的投入!

编辑: 我在的时候实例化一个MockWifiManager以及地狱。该构造的WiFi经理期待一个IWifiManager它不会出现在Android SDK中存在的一种类型。

I'm having a hell of a time instantiating a MockWifiManager as well. The constructor for wifi manager is expecting an IWifiManager a type which does not appear to exist in the Android SDK.

推荐答案

创建一个围绕WifiManager抽象。使用此为您的嘲弄。惩戒你没有自己的东西又硬又脆。如果做得正确,你应该能够切换内部,再加上你将最终获得一个更好的mockable API。

Create an abstraction around WifiManager. Use this for your mocking. Mocking stuff you don't own is hard and brittle. If done right you should be able to switch the internals, plus you'll end up with a better mockable API.

有关您的测试,你可以存根/假的经理,你的心的内容。对于生产,你会传递一个具体的实例。

For your testing you can stub/fake the manager to you hearts content. For production you'll pass in a concrete instance.

至于你改变你的code点只是为了测试的这是不正确。在下面的文章中讨论首先你应该嘲笑的角色不是类型。谷歌获得更多信息。

With regards to your point about changing your code just to make it testable that is incorrect. Firstly you should mock roles not types as discussed in the paper below. Google for more info.

其次建立一个各地的第三方code抽象是由依赖倒置原则,稳如说是最佳做法。你应该总是依赖于抽象,而不是具体的实现无论你是单元测试与否。

Secondly creating an abstraction around third party code is a best practice as stated by the dependency inversion principle in SOLID. You should always depend on abstractions rather than concrete implementations whether you are unit testing or not.

http://www.objectme​​ntor.com/resources/articles/dip.pdf http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

http://www.objectmentor.com/resources/articles/dip.pdf http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

这篇关于嘲笑了WifiManager为Android单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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