如何使用抽象基类继承内部类?
我正在尝试创建一个测试类,它使用内部类组织其测试方法。我希望这个类是抽象的,能够设置静态属性,以便可以注入该属性。这是我正在谈论的示例:
[TestClass]
public abstract class BaseUnitTest
{
public static string InjectedProperty;
public static string GetInjectedString()
{
return InjectedProperty;
}
[TestClass]
public class WhenFoo
{
[TestMethod]
public void TestFoo()
{
string str = GetInjectedString();
}
}
}
[TestClass]
public class DeriverdUnitTest : BaseUnitTest
{
[ClassInitialize]
public void SetUp()
{
InjectedProperty = "Injected Property";
}
}
但是,我没有看到 DerivedUnitTest+WhenFoo+TestFoo() 类出现在我的单元测试视图中。我正在使用 Visual Studio 2010。我猜当我重写 BaseUnitTest 时,我也不会重写它的内部类。我想我可以使其内部类抽象并稍后覆盖它们,但随着我的测试类的复杂性增加,这会变得非常烦人。有人可以解释一下为什么会发生这种情况以及我该如何解决它吗?
谢谢。
编辑:
我觉得我需要更好地解释我想要这样做的原因。我们希望实施一个命名非常冗长的测试标准。因此,测试类看起来像这样:
[TestClass]
public abstract class BaseUnitTest
{
public static string InjectedProperty;
public static string GetInjectedString()
{
return InjectedProperty;
}
[TestClass]
public class WhenFooIsCalled
{
[TestClass]
public class AndTheArgumentIsNull
{
[TestMethod]
public void AnArgumentNullExceptionShouldBeThrown()
{
string str = GetInjectedString();
}
}
}
}
这样做的好处是,当您在 Visual Studio 中打开测试视图并显示方法名称和类名称列时,您会得到如下所示的内容:
BaseUnitTest+WhenFooIsCalled+AndTheArgumentIsNull AnArgumentNullExceptionShouldBeThrown()
这使得浏览起来更容易告诉数百个通过测试中失败的测试应该做什么。
我希望能够覆盖抽象 BaseUnitTest 的主要原因是,当我执行 BaseUnitTest 中包含的所有测试时,它们都会添加到 DerivedUnitTest 中并显示在 Visual Studio 的测试视图中。
再次感谢。
I'm trying to create a test class which organizes its test methods using inner classes. I would like for this class to be abstract with the ability to set a static property so this property can be injected. Here's an example of what I'm talking about:
[TestClass]
public abstract class BaseUnitTest
{
public static string InjectedProperty;
public static string GetInjectedString()
{
return InjectedProperty;
}
[TestClass]
public class WhenFoo
{
[TestMethod]
public void TestFoo()
{
string str = GetInjectedString();
}
}
}
[TestClass]
public class DeriverdUnitTest : BaseUnitTest
{
[ClassInitialize]
public void SetUp()
{
InjectedProperty = "Injected Property";
}
}
However, I don't see a DerivedUnitTest+WhenFoo+TestFoo() class show up in my unit test view. I'm using Visual Studio 2010. I'm guessing when I override BaseUnitTest, I don't override its inner classes as well. I suppose I could make its inner classes abstract and override them later, but as the complexity of my test class increases this will get really annoying. Could somebody please explain why this is occuring and how I can fix it?
Thanks.
Edit:
I feel like I need to better explain my reasons for wanting to do this. We'd like to implement a testing standard which is very verbose in its naming. Therefore a test class would look something like this:
[TestClass]
public abstract class BaseUnitTest
{
public static string InjectedProperty;
public static string GetInjectedString()
{
return InjectedProperty;
}
[TestClass]
public class WhenFooIsCalled
{
[TestClass]
public class AndTheArgumentIsNull
{
[TestMethod]
public void AnArgumentNullExceptionShouldBeThrown()
{
string str = GetInjectedString();
}
}
}
}
The advantage of this is when you open up the test view in Visual Studio and display the method name and class name columns you get something that looks like this:
BaseUnitTest+WhenFooIsCalled+AndTheArgumentIsNull AnArgumentNullExceptionShouldBeThrown()
This makes it a lot easier to glance to tell what a failing test among a few hundred pass tests is supposed to do.
The main reason I want to be able to override the abstract BaseUnitTest is because when I do all of the tests which were contained in the BaseUnitTest are all added to the DerivedUnitTest and show up in the Test View in Visual Studio.
Thanks again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 C# 语言中,嵌套类与嵌套它们的类没有特殊关系。这是完全不同的类型。您这样做的充分理由只有一个:您可以将该类声明为私有。这可以帮助您创建一个小工人类来代表外部类(一个从外部完全不可见的类)完成工作。非常有用,否则您不能在外部类范围内声明私有类,您能做的最好的事情就是在内部。
接下来就是它对外部类的继承没有任何作用。从外部派生的类对于基类内部的嵌套类根本不可见。这就是意图,将其声明为私有是首先将其嵌套的原因。
Punt:如果您需要派生类中的该类,只需将其声明为内部或公共。
In the C# language, nested classes have no special relationship with the class in which they are nested. It is a completely different type. There is only one good reason you'd ever do this: you can declare the class private. Which helps you to create a little worker class to get a job done on behalf of the outer class, a class that is completely invisible from the outside. Very useful, you cannot otherwise declare a private class at outer class scope, the best you can do is internal.
What follows is that it in no way plays a role in the inheritance of the outer class. A class you derive from the outer has no visibility to the nested class inside the base class at all. Which was the intention, declaring it private was the reason to nest it in the first place.
Punt: if you need that class in the derived one just declare it internal or public.
嵌套类型不是这样工作的。您不能“覆盖”类型。
目前尚不清楚您想在这里实现什么目标,但我认为这不会起作用。
Nested types don't work that way. You can't "override" types.
It's not clear what you're trying to achieve here, but I don't think it's going to work.
您可以使用 xUnit.NET 和 SubSpec 完成丰富、详细、BDD 风格的测试重写。如今,SubSpec 已包含在 xUnit.NET 附加下载中。您可以在以下文章中了解有关 SubSpec 和 BDD 测试的更多信息:
http: //haacked.com/archive/2008/08/24/introducing-subspec.aspx
You can accomplish the kind of rich, verbose, BDD-style test repriting with xUnit.NET and SubSpec. SubSpec is included in the xUnit.NET extras download these days. You can read more about SubSpec and BDD testing at the following article:
http://haacked.com/archive/2008/08/24/introducing-subspec.aspx
使用配置文件怎么样?例如
How about using a config file? For Example