什么是对象模拟,我什么时候需要它? [英] What is Object Mocking and when do I need it?

查看:30
本文介绍了什么是对象模拟,我什么时候需要它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多人在编写单元测试时使用 Mock Objects.什么是模拟对象?为什么我需要一个?我需要模拟对象框架吗?

Many people use Mock Objects when they are writing unit tests. What is a Mock Object? Why would I ever need one? Do I need a Mock Object Framework?

推荐答案

Object Mocking 用于将依赖项排除在单元测试之外.有时你会有一个像SelectPerson"这样的测试,它会从数据库中选择一个人并返回一个 Person 对象.

Object Mocking is used to keep dependencies out of your unit test. Sometimes you'll have a test like "SelectPerson" which will select a person from the database and return a Person object.

要做到这一点,您通常需要依赖于数据库,但是通过对象模拟,您可以使用模拟框架模拟与数据库的交互,因此它可能会返回一个看起来像从数据库返回的数据集,而您然后可以测试您的代码以确保它处理将数据集转换为人员对象,而不是使用它来测试与数据库的连接是否存在.

To do this, you would normally need a dependency on the database, however with object mocking you can simulate the interaction with the database with a mock framework, so it might return a dataset which looks like one returned from the database and you can then test your code to ensure that it handles translating a dataset to a person object, rather than using it to test that a connection to the database exists.

这篇关于什么是对象模拟,我什么时候需要它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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