简体   繁体   English

遵循 C# 中的给定单元测试时,如何正确删除从 void functionRemove 中的列表 ILine 继承接口的对象?

[英]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 ILineclass 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.

相关问题 在C#中,将实现接口X的对象传递给从接口X继承的接口Y时出错。 - Error when passing an object that implements interface X to an interface Y that inherits from interface X. in C# 如何初始化List继承自List C#? - How to initialize List inherits from List C#? C# class 继承自接口并且类型继承自接口类型方法时出现错误 CS0738 - C# Error CS0738 when class inherits from interface and with type that inherits from interfce type method 如何在继承自另一个COM接口的C#上声明和实现COM接口? - How to declare and implement a COM interface on C# that inherits from another COM interface? 当对象从列表继承时序列化对象 - Serialize object when the object inherits from list 如何从C#中的列表中删除对象 - how to remove an object from a list in C# 如何从 C# 程序中的给定列表中删除所有 1? - How to remove all 1 from given list in C# program? C#泛型和接口继承接口 - C# Generics and Interface Inherits Interface 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) C#如何在没有实现的情况下对接口方法进行单元测试 - C# How to unit test an interface method without implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM