使用通用抽象类型参数进行.net单元测试链接
对采用泛型类型参数(其中类型也是抽象类)的抽象类进行单元测试的正确方法是什么?例如)
public abstract class BaseClass<T> where T : BaseT { ... }
我必须创建一个扩展 BaseT 的测试类,对吧?
如果 BaseClass 支持方法链接,会发生什么情况,
public abstract class BaseClass<T> where T : BaseClass<T> { ... } ??
谢谢。
更新了第二种情况的具体类示例:
public class ConcreteClass : BaseClass<ConcreteClass>
{
public ConcreteClass Method1()
{
return this as ConcreteClass;
}
}
What would be the proper way to unit test an abstract class which takes generic type parameter where the type is also an abstract class? E.g)
public abstract class BaseClass<T> where T : BaseT { ... }
I'll have to create a test class which extends BaseT, right??
and what happen if the BaseClass support method chaining such that
public abstract class BaseClass<T> where T : BaseClass<T> { ... } ??
thanks.
updated concrete class sample for the 2nd case:
public class ConcreteClass : BaseClass<ConcreteClass>
{
public ConcreteClass Method1()
{
return this as ConcreteClass;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在不使用 Rhino Mocks 实现虚拟实现类的情况下执行此操作: http:// /www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx
不幸的是,Rhino Mocks 要求您使用带有部分模拟的记录/播放语法,而不是 AAA 语法。
给定以下抽象类:
您可以通过以下方式测试 Concrete 的实现:
本质上,您为基类创建部分模拟,设置对抽象方法的期望,然后调用被测试的具体方法。泛型只是另一个模拟,如果需要的话,另一个部分。
这样做的缺点显然是对记录/回放语法的要求。我不知道其他模拟框架是否可以为您提供更多帮助,但我通常发现,如果您有这样的高级用例,Rhino Mocks 是您可以使用的模拟框架。
You can do this without implementing a dummy implementation class using Rhino Mocks: http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx
Unfortunately, Rhino Mocks requires you to use the record/playback syntax with partial mocks instead of the AAA syntax.
Given the following abstract classes:
You can test the implementation of Concrete in the following way:
In essence, you create a partial mock for the base class, set expectations on the abstract methods, then call the concrete method under test. The generic is simply another mock, another partial if necessary.
The downside of this is obviously the requirement for the record/playback syntax. I don't know if other mocking frameworks can help you out more here, but I've generally found that Rhino Mocks is the mocking framework you go to if you have an advanced use case like this.
在不了解更多细节的情况下,我猜测您正在尝试测试在抽象泛型类中实现的一些代码。例如,您可以测试在基类中实现且未标记为抽象的公共方法或属性。
因此,您必须创建一个扩展通用基类 BaseClass< 的类。 T>
以及扩展 BaseT 的不同类。我们将这些称为 ConcreteClass< T>和 ConcreteT 分别。
然后,实例化一个 ConcreteClass< 类型的类。具体T>。您现在可以在此实例上运行测试,从而测试抽象类的逻辑。
Without knowing more details, I'm guessing that you are trying to test some code that is implemented in the abstract generic class. For example, you might test a public method or property that is implemented in the base class and isn't marked as abstract.
So, you will have to create a class that extends the generic base class BaseClass< T >
and a different class that extends BaseT. Let's call these ConcreteClass< T > and ConcreteT respectively.
Then, instantiate a class of type ConcreteClass< ConcreteT >. You can now run tests on this instance, allowing you to test the abstract class' logic.
没有办法测试抽象类,你不能创建它的实例:)
There is no way to test an abstract class, you cannot create an instance of :)