如何从多个叶子类之一中的基本mixin类中实现TDD功能? [英] How to TDD functionality in a base mixin class from one of many leaf classes?
问题描述
接着是这个问题(开发为TDD接口) ,我仍然遇到一些问题.
following on from this question (Developing to an interface with TDD), I'm still having some issues.
我测试了两个类的存在,它们最终共享了一些相同的功能.我将一个通用的基类重构为存在,并且所有测试仍然通过.到目前为止,太奇妙了.
I test-drove two classes into existence, both of which ended up shared some identical functionality. I refactored a common base class into existence, and all the tests still passed. So far, so tdd-tastic.
我需要一个第三类来实现基类,因此将一些测试复制到一个新的固定装置中,并使每个测试依次编译并变为绿色,直到我拥有一个功能齐全的第三类.这种方法可以争论,因为如果我没有正确地复制一项测试,或者没有成功更改一项测试以支持新课程,那我会很麻烦,但这不是主要问题.
I needed a third class to implement the base class, so copied some tests into a new fixture and made each one compile and go green in turn, until I had a fully-functional third class. This approach can be debated, because if I didn't copy one test across correctly, or didn't change one successfully to support the new class, I'd be in trouble, but that's not the main problem.
我现在遇到的问题是我想向基类添加功能.不能单独实例化它,因此必须通过叶子类之一进行实例化.但是,如果我忘记将测试复制到其他类中,那么我将拥有不受支持的功能.看来这不是软件工程师设计的方式,我想知道我要去哪里出错了.
The problem I have now is that I want to add functionality to the base class. It can't be instantiated on its own, so it will have to be through one of the leaf classes. However if I forget to copy the tests across to the other classes, I'll have unsupported functionality. It doesn't seem a very software-engineer-y way of doing things, and I wanted to know where I was going wrong.
这是我的设计问题吗?我应该以其他方式布置测试吗?还是我什么都不担心?
Is this a problem with my design? Should I lay my tests out in a different way? Or am I worrying about nothing?
谢谢.
推荐答案
即使您不能直接创建基类的实例,您仍然可以通过派生可以测试的特定子类来对其进行单元测试经过测试.
Even though you can't create an instance of your base class directly, you can still unit test it by deriving a test-specific subclass that can be tested.
假设您有一个名为MyBase的抽象类.显然,您不能直接创建MyBase的实例,但是在您的单元测试项目中,您可以创建MyBase的特定于测试的专业化名称,称为TestableBase或其他名称.
Assume that you have an abstract class called MyBase. Obviously, you can't create an instance of MyBase directly, but in your unit test project, you can create a test-specific specialization of MyBase called TestableBase or something else.
因此,假设您要测试这样的内容:
So let's assume that you want to test something like this:
public class MyBase
{
public abstract void DoStuffCore();
public void DoStuff()
{
// Do something interesting first
this.DoStuffCore();
}
}
您要测试DoStuff是否正确调用了DoStuffCore,可以创建如下代码:
And you want to test that DoStuffCore was correctly invoked by DoStuff, you can create something like this:
public class Spy : MyBase
{
public bool CoreInvoked { get; private set; }
public override void DoStuffCore()
{
this.CoreInvoked = true;
}
}
这将允许您创建Spy的新实例并调用其DoStuff方法,然后随后验证CoreInvoked属性为true.
This would allow you to create a new instance of Spy and call its DoStuff method and then subsequently verify that the CoreInvoked property is true.
这篇关于如何从多个叶子类之一中的基本mixin类中实现TDD功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!