[英]Unit testing classes inheriting abstract class
I have an abstract class like that: 我有一个像这样的抽象类:
public abstract class Field<T>
{
private int _length;
public int Length
{
get
{
return _length;
}
protected set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException("The length must be greater than 0");
}
else
{
_length = value;
}
}
}
private T _value;
public T Value
{
get
{
if (_value == null) throw new ArgumentException("Field does not have any value set");
return _value;
}
set
{
//here obviously I have some code to check the value and assign it to _value
//I removed it though to show what the problem is
throw new NotImplementedException();
}
}
public Field(int length, T value)
{
Length = length;
Value = value;
}
public Field(int length)
{
Length = length;
}
//some abstract methods irrelevant to the question...
}
Then I have a class that inherits Field<> 然后我有一个继承Field <>的类
public class StringField : Field<string>
{
public StringField(int length, string value)
: base(length, value)
{ }
public StringField(int length)
: base(length)
{ }
//implementation of some abstract methods irrelevant to the question...
}
When I run a test like this, it is fine and passes (constructor throws a correct exception): 当我运行这样的测试时,它就可以通过(构造函数抛出正确的异常):
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Constructor_LengthOnly_LengthZero_ShouldThrowArgumentOutOfRangeException()
{
int length = 0;
StringField sf = new StringField(length);
//should throw exception
}
But when I run this test, the constructor does not throw, even though it should throw NotImplementedException: 但是,当我运行此测试时,即使应该抛出NotImplementedException,构造函数也不会抛出该异常:
[TestMethod]
[ExpectedException(typeof(NotImplementedException))]
public void Constructor_LengthAndValue_ValidLength_TextTooLong_ShouldThrowNotImplementedException()
{
int length = 2;
string value = "test";
StringField sf = new StringField(length, value);
//should throw exception
}
Am I doing something wrong? 难道我做错了什么? I don't think I am missing something, am I? 我不认为我缺少什么,是吗? Thanks. 谢谢。
--EDIT-- - 编辑 -
Turns out everything was ok, here what was happening: 事实证明一切都很好,这里发生了什么:
- in the Field
I had another property and constructor like so: -在Field
我有另一个属性和构造函数,如下所示:
enprivate string _format;
public string Format
{
get
{
return _format;
}
protected set
{
_format = value;
}
}
public Field(int length, string format)
{
Length = length;
Format = format;
}
- since the derived class was replacing T
with string
, I thought that by calling the base like I showed in my original message, I am calling the constructor that takes Value
, but I was calling the one taking Format
... -由于派生类将T
替换为string
,因此我认为通过像我在原始消息中显示的那样调用基数,可以调用采用Value
的构造函数,但是可以采用采用Format
的构造函数...
- to fix this, in my StringField
class I replaced a call to the base constructor to look like this: -要解决此问题,在我的StringField
类中,我替换了对基本构造函数的调用,如下所示:
public StringField(int length, string value)
: base(length, value: value)
{ }
An intresting case of type conflicts while using generics :) 使用泛型时类型冲突的一个有趣案例:)
I copy pasted you code, and for me the test executes as expected: the code throws a new NotImplementedException() and the test is Passed. 我复制粘贴的代码,对我来说测试将按预期执行:代码将引发新的NotImplementedException()并且测试通过。
Perhaps you are executing some old dll:s? 也许您正在执行一些旧的dll:s? Or that some of the code before the "throw new NotImplementedException()" is causing the problem? 还是“引发新的NotImplementedException()”之前的某些代码导致了问题? Can you post those lines of code? 您可以发布这些代码行吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.