[英]How to correctly remove an object that inherits interface from a list ILine in the void functionR emove when following a given unit test in C#?
I have interface ILine
that is inherited by the class Line
.我有
interface ILine
由class Line
继承。 ILine
is defined like this: ILine
定义如下:
interface ILine {
string Name { get; }
ICity City { get; }
IEnumerable<IStation> Stations { get; }
IEnumerable<ITrain> Trains { get; }
IStation Previous(IStation s);
IStation Next(IStation s);
void AddBefore(IStation toAdd, IStation before = null);
void Remove(IStation s);
}
In the current context I am working with in the method void Remove (IStation s)
and I am trying to pass following unit test that tests stations can be removed and then re-added.在当前上下文中,我正在使用
void Remove (IStation s)
并且我正在尝试通过以下单元测试,即可以删除然后重新添加测试站。
The Unit Test:单元测试:
public void stations_can_be_removed()
{
ICity c = CityFactory.CreateCity("Paris");
ILine l = c.AddLine("RER B");
IStation s = c.AddStation("Opera", 0, 0);
Action action = (() => l.Remove(s));
action.ShouldThrow<ArgumentException>();
l.AddBefore(s);
action = (() => l.Remove(s));
action.ShouldNotThrow();
l.Stations.Count().Should().Be(0);
s.Lines.Count().Should().Be(0);
}
public void stations_can_be_removed_then_re_added()
{
ICity c = CityFactory.CreateCity("Paris");
ILine l = c.AddLine("RER B");
IStation s = c.AddStation("Opera", 0, 0);
l.AddBefore(s);
s.Lines.Count().Should().Be(1);
l.Remove(s);
s.Lines.Count().Should().Be(0);
Action action = (() => l.AddBefore(s));
action.ShouldNotThrow();
l.Stations.Count().Should().Be(1);
s.Lines.Count().Should().Be(1);
}
I have tried to do the following code to pass the test mainly in the function void Remove
.我尝试执行以下代码以通过主要在函数 void
Remove
的测试。 However, the unit test breaks at the line 7 where I throw exception.但是,单元测试在我抛出异常的第 7 行中断。 I guess my approach is wrong how to test this unit test mainly how I defined my exception by:
我想我的方法是错误的如何测试这个单元测试主要是我如何通过以下方式定义我的异常:
if (((List<ILine>)s.Lines).Remove(s))
{
throw new ArgumentException();
}
public IEnumerable<IStation> Stations
{
get{ return _stations; }
}
public IStation Next(IStation s)
{
if (!_stations.Contains(s))
throw new ArgumentException();
var index = _stations.IndexOf(s);
var isLast = index == _stations.Count -1;
if (isLast) { return null;}
return _stations[index + 1];
}
public IStation Previous(IStation s)
{
if (!_stations.Contains(s))
throw new ArgumentException();
var index = _stations.IndexOf(s);
var isFirst = index == 0;
if (isFirst) { return null;}
return _stations[index - 1];
}
public void AddBefore(IStation toAdd, IStation before = null)
{
if (toAdd == null || _stations.Contains(toAdd))
throw new ArgumentException();
((List<Line>)toAdd.Lines).Add(this);
var index = (before != null) ? _stations.IndexOf(before) : -1;
if (index > -1)
_stations.Insert(index, toAdd);
else
_stations.Add(toAdd);
}
public void Remove(IStation s)
{
_stations.Remove(s);
if (((List<ILine>)s.Lines).Remove(s))
throw new ArgumentException();
}
List<T>.Remove()
returns true
when it succeeds, therefore it throws the exception as your code instructs. List<T>.Remove()
在成功时返回true
,因此它会按照您的代码指示抛出异常。
You probably want to throw said exception if it doesn't succeed:你可能想扔说异常,如果它没有成功:
if (!((List<ILine>)s.Lines).Remove(s))
But this is about the tenth question about this design, five of which you or others have deleted, and as I've suggested under multiple previous invocations, you may want to scratch this design and start over.但这是关于此设计的第十个问题,您或其他人已删除其中的五个问题,正如我在之前多次调用中所建议的那样,您可能想要重新设计此设计并重新开始。 A
List<ILine>
cannot contain an IStation
, unless IStation
inherits ILine
, which is not logical (inheritance implies an is-a relation, and a station isn't a line). List<ILine>
不能包含IStation
,除非IStation
继承ILine
,这不合逻辑(继承意味着 is-a 关系,而站不是线)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.