0

有三个类的接口 Train:ITrain,Line:Iline 和 City:ICity。通常,我在代码中进行了改进并解决了 TEST 3 和 4,但导致了新问题。

我有以下问题: 1. 第 19 行的测试 1:“序列不包含元素” 2. 第 38 行的测试 2:“序列不包含元素” 3. 第 90 行的测试 5:“预期值为 2,但发现0"

我猜我错过了分配和火车和线路之间越来越多的分配和正确分配。但是,我不知道如何解决这个问题。

internal class Line : ILine
{
    private List<Station> _stations;

    internal IEnumerable<Train> _trains;

    internal Line(string name, City city)
    {
        this.Name = name;
        this.City = city;
        _stations = new List<Station>();
        _trains = new List<Train>();

    }
    public string Name
    {
        get;
    }

    public ICity City
    {
        get;
    }

    public IEnumerable<IStation> Stations
    {
        get
        {

            return _stations;
        }
    }

    public IEnumerable<ITrain> Trains
    {
        get
        {
            return _trains;
        }
    }
 }

internal class Train : ITrain
{
    private List<Line> _lines;
    internal Train(string name, Company company)
    {
        this.Name = name;
        this.Company = company;
        _lines = new List<Line>();
    }
    public string Name
    {
        get;
    }
    public ICompany Company
    {
        get;
    }
    public ILine Assignment
    {
        get;
        private set;
    }
    public void AssignTo(ILine l)
    {
        if (!(Assignment == null || l == null || Assignment.City == l.City))
        {
            throw new ArgumentException();
        }
        Assignment = l;
        foreach (var line in _lines)
        {
            //verify that LINE is not null
            if (Assignment != null && Assignment.Trains.Contains(this))
            {
                //need to remove this from l1.trains
                ((List<Train>)Assignment.Trains).Remove(this);
                ((List<Train>)l.Trains).Add(this);
            }
            if (Assignment == null && Assignment.Trains.Contains(this))
            {
                //need to remove this from l1.trains
                ((List<Train>)l.Trains).Remove(this);
            }
        }

    }
}

//TESTs are not passing on line 19, 38, 46
// Only test T3 and T4 function
[Test]
public void T1_trains_can_be_assigned_to_a_line()
{
  ICity s = CityFactory.CreateCity("Paris");
  ICompany c = s.AddCompany("SNCF");
  ITrain t1 = c.AddTrain("RER1");
  ITrain t2 = c.AddTrain("RER2");
  t1.Assignment.Should().BeNull();
  t2.Assignment.Should().BeNull();
  ILine l = s.AddLine("RER A");
  t1.AssignTo(l);
  t1.Assignment.Should().BeSameAs(l);
  t2.Assignment.Should().BeNull();

  //Error:Sequence contains no elements
  l.Trains.Single().Should().BeSameAs(t1);
}
[Test]
public void T2_when_a_train_is_assigned_to_a_line_he_losts_its_previous_line()
{
  ICity s = CityFactory.CreateCity("Paris");
  ICompany c = s.AddCompany("SNCF");
  ILine l1 = s.AddLine("RER A");
  ILine l2 = s.AddLine("RER B");
  ITrain t1 = c.AddTrain("RER1");
  t1.AssignTo(l1);
  t1.Assignment.Should().BeSameAs(l1);
  t1.AssignTo(l2);
  t1.Assignment.Should().BeSameAs(l2);
  l1.Trains.Count().Should().Be(0);
  //Error:Sequence contains no elements
  l2.Trains.Single().Should().BeSameAs(t1);
}
[Test]
public void T5_line_can_have_mutiple_trains()
{
  ICity s1 = CityFactory.CreateCity("Paris");
  ICompany c1 = s1.AddCompany("SNCF");
  ILine l1 = s1.AddLine("RER A");
  ITrain t1 = c1.AddTrain("RER1");
  ITrain t2 = c1.AddTrain("RER2");
  t1.AssignTo(l1);
  t2.AssignTo(l1);
  //Error: Expected value to be 2, but found 0 
  l1.Trains.Count().Should().Be(2);
  l1.Trains.Should().Contain(t1);
  l1.Trains.Should().Contain(t2);
  t1.AssignTo(null);
  l1.Trains.Single().Should().BeSameAs(t2);
}

void AssignTo(ILine l);

第三个测试是检查火车和线路是否属于同一个城市。

4

1 回答 1

0

我假设以下含义:每当将火车分配给 Line1 然后分配更改为 Line2 时,Line2 必须与 Line1 在同一个城市。

这可以通过以下方式捕获:

if (!(Assignment.City == l.City))  throw new ArgumentException(...);

由于似乎也允许空值,因此更完整的检查将是

if (!(Assignment == null || l == null || Assignment.City == l.City)) throw...

请注意,这将允许通过以下方式将火车传送到另一个城市

train1.AssignTo(lineInCity1);
train1.AssignTo(null);
train1.AssignTo(lineInCity2);

要求没有具体说明是否应该允许这样做。

于 2019-12-23T15:04:15.847 回答