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

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

问题描述

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

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?

推荐答案

对象模拟用于将依赖项保留在单元测试之外. 有时您会遇到"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天全站免登陆