有三个类的接口为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);
第三个测试是检查火车和线路是否属于同一城市。
我假设以下意思是:每当火车被辅助到1号线,然后分配更改为2号线时,2号线必须与1号线位于同一城市。
这可以通过以下方式捕获:
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);
这些要求没有具体说明是否应该允许这样做。