如何验证两个 类 分配给同一个城市?后续问题

How can I verify that two classes are assigned to the same city? Follow-up issues

有三个 类 具有接口火车:ITTrain、线路:Iline 和城市:ICity。通常,我改进了代码并解决了测试 3 和 4,但引起了新问题。

我有以下问题: 1. 第 19 行的测试 1:"Sequence contains no elements" 2. 第 38 行测试 2:"Sequence contains no elements" 3. 第 90 行测试 5:"Expected value to be 2, but found 0"

我猜我错过了 Assignment 和 Assignment & Train & Line 之间递增的增量和正确的分配。但是,我不知道如何解决这个问题。

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);

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

我假设以下意思:每当火车被分配到 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);

要求没有指定是否允许这样做。