none
How to remove stations from a line and re-added them on the line? RRS feed

  • Question

  • I have interface ILine that is inherited by the class Line. Iline has following properties and methods: 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.

    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. However, the unit test breaks at the line 7 where I throw exception. 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();
        
    }
        
    How can I define the method void remove correctly? Mainly where I am going wrong in my logic?


           
    Tuesday, January 7, 2020 5:15 PM

Answers

All replies

  • Hi BarbraFleg,

    Thank you for posting here.

    There are some custom classes in your code (ICity, IStation, CityFactory), I'm afraid we cannot perform actual tests without these information, could you please provide these information for us to test?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 8, 2020 9:44 AM
  • Already resolved. Thank you.
    • Marked as answer by BarbraFleg Thursday, January 9, 2020 9:58 AM
    Wednesday, January 8, 2020 10:42 AM
  • Hi BarbraFleg,

    I am very happy to hear the news.

    If it is possible, could you please publish your solution and mark it as answer? This will greatly help users with similar problems.

    Of course, it doesn't matter if it is inconvenient for some reason.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 9, 2020 1:30 AM