Rhino Mocking和TDD与遗留代码 [英] Rhino Mocking and TDD with legacy code

查看:85
本文介绍了Rhino Mocking和TDD与遗留代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要说的是我正在使用旧版代码.因此可以进行一些更改,但不能进行大的更改.

First of all let me say I am working from legacy code. So some changes can be made but not drastic ones.

我的问题是我有一个车辆"对象,这很简单,但是没有接口或其他任何东西.这个项目是在TDD真正开始成为主流之前创建的.无论如何,我需要添加一种新方法来更改车辆的起步里程.我以为这是尝试TDD和Mocking的良好开端,因为这是我的新手.我的问题是我需要创建一个车辆进行一些检查,其中包括检查数据库.抱歉,如果我的问题不是100%清楚,那是为什么我要发布,因为我有点困惑Rhino Mocks所在的位置(如果我需要它!).

My problem is I have a "Vehicle" object, it is pretty simple but has no interfaces or anything on it. This project was created before TDD really started to become more main stream. Anyway, I need to add a new method to change the starting mileage of the vehicle. I thought this would be a good start to try TDD and Mocking as I am new it all. My problem is I need to create a vehicle do some checks which involve going to the database. Sorry if my question is not 100% clear, is why I am posting as I am a bit confused where Rhino Mocks fits in (and if I need it!).

推荐答案

是否容易创建Vehicle类型(对象)的实例,然后调用您的方法进行测试?如果是,那么您可能不需要模拟.

Is it easy to create an instance of the Vehicle type (an object) and then invoke your method for a test ? If yes, then chances are you don't need a mock.

但是,如果您的Vehicle类型具有执行您要测试的动作所需的依赖项(例如数据库访问对象),那么您想使用一个模拟数据库访问对象,该对象将返回测试的固定值,因为您希望您的单元测试能够快速运行.

However if your Vehicle type has dependencies (like a Database access object) that it needs to perform the action that you want to test, then you would like to use a mock database access object that returns canned values for the test since you want your unit tests to run fast.

Vehicle  [depends On>] OwnerRepository [satisfied By] SQLOwnerRepository

因此,您引入了一个接口(例如,一个OwnerRepository以获取所有者的详细信息),以分隔两者之间的DB Interaction(定义合同).使您的真实依赖项(此处为SQLOwnerRepository)实现此接口.还应设计代码,以便可以注入依赖项,例如

So you introduce an interface (an OwnerRepository to get details of the owner, let's say) to separate the DB Interaction (define a contract) between the two. Make your real dependency (here SQLOwnerRepository) implement this interface. Also design your code such that dependencies can be injected e.g.

public Vehicle (OwnerRepository ownerRepository) 
{  _ownerRepository = ownerRepository;  // cache in member variable }

现在在测试代码中,

Vehicle [depends On >] OwnerRepository [satisifed By] MockOwnerRepository 

您有给定接口的框架,该框架将创建该接口的模拟实现(请参见Rhino/Moq框架).因此,您不再需要实际的数据库连接来测试您的Vehicle类. 模拟用来提取耗时/不可控的依赖项,以保持单元测试快速/可预测.

You have frameworks that given an interface would create a mock implementation of it (See Rhino/Moq frameworks). So you no longer need an actual DB connection to test your Vehicle class. Mocks are used to abstract away time consuming / uncontrollable dependencies in order to keep your unit tests fast / predictable.

我建议您阅读依赖注入",以更好地了解何时以及为何使用模拟.

I'd recommend reading up on "Dependency Injection" to have a better understanding of when and why to use mocks.

这篇关于Rhino Mocking和TDD与遗留代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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