在android系统模拟测试 [英] mock tests in android

查看:147
本文介绍了在android系统模拟测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么,我们模拟testing..How可以做模拟testing..how创建mocks..when使用它

what is mock testing..How can we do mock testing..how to create mocks..when to use it

推荐答案

基本上嘲笑让您更换一个具体实施与假实例类的来模拟你想测试特定的场景。

Mocks basically allow you to replace a concrete implementation of a class with a "fake" instance to simulate specific scenarios that you want to test.

例如,假设你有ClassA.DoSomething()方法接受一个ClassB的实例作为一个参数,并用它做一些工作。您可能希望测试时ClassB的距离ClassA.DoSomething()用于在特定情况下会发生什么(例如,当ClassB.HelperMethod()被调用时会发生什么,它返回一个空)。

For example, suppose you have ClassA.DoSomething() method that accepts a ClassB instance as a parameter and uses it to do some work. You may want to test what happens in specific scenarios when ClassB is used within ClassA.DoSomething() (for example, what happens when ClassB.HelperMethod() is called and it returns a null).

您可以提供一个嘲笑(假)ClassB的实例,并告诉它究竟是如何从测试表现。例如,你会告诉它期望HelperMethod一个电话,并为调用返回一个空值。

You can supply a "mocked" (fake) instance of ClassB, and tell it exactly how to behave from your test. For example, you would tell it to expect a call to "HelperMethod" and to return a null value for that call.

嘲弄是让你测试个人为单位,而不依赖于外部依赖非常有用。通过嘲讽外部组件,您从测试中删除就可以了依赖,这意味着你专注于你的单元测试你的分立元件,而不是做哪些测试2组件之间的融合集成测试上。一个很好的例子是使用一个数据访问层,从数据库中检索数据,然后在做一些数据操纵的类。您可以从实际需要走下车到数据库模拟数据访问层,以prevent的需要。所以,你可以专注于测试类的工作,而不是把重点放在建立所有测试数据的数据库,并测试与数据库的集成。这有太多加快你的测试的好处。

Mocks are useful for allowing you to test individual units without relying on external dependencies. By mocking the external component, you remove the dependency on it from your tests and it means you focus your unit testing on your discrete component instead of doing integration testing which tests the integration between 2 components. A good example is a class that uses a data access layer to retrieve data from a database and then does some manipulation on the data. You can mock the data access layer to prevent the need from actually needing to go off to the database. So you can focus on testing how the class works rather than having to focus on setting up the db with all required test data and testing the integration with the DB. This has the added benefit of speeding up your tests too.

维基百科具有嘲讽
的一个很好的概述
的Andr​​oid 上嘲笑支持文档。

Wikipedia has a good overview of mocking
Android documentation on support for mocks.

这篇关于在android系统模拟测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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