mardi 24 décembre 2019

How do we remove assigned value (assignment) if the current assigned value to a class Train is a null line?

How can we remove train's assignment when the train is assigned to a null line? I have tried by using if and remove but I am missing a piece of correct check when a train is assigned to a null line. My code is not passing the test which is in the second code snippet. Could you please explain me what am I doing wrong?

        public ILine Assignment
        {
            get;
            private set;
        }

        public void AssignTo(ILine l)
        {
            //Prior test: verify cities 
            if (!(Assignment == null || l == null || Assignment.City == l.City))
            {
                throw new ArgumentException();
            }

            //CURRENT TEST goes here that verifies things
            //if (Assignment == null)
            //{
            //    Assignment.Trains.Remove();
            //}

         

            //Prior test: 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);
            }

            //Prior test: need to change current line and add train to new line
            ((List<Train>)l.Trains).Add(this);
            Assignment = l;
           
        }
 public void T_assigning_a_train_to_a_null_line_removes_its_assignment()
        {
            ICity s1 = CityFactory.CreateCity("Paris");
            ICompany c1 = s1.AddCompany("SNCF");
            ILine l1 = s1.AddLine("RER A");
            ITrain t1 = c1.AddTrain("RER1");
            Action action1 = (() => t1.AssignTo(null));
            action1.ShouldNotThrow();
            t1.AssignTo(l1);
            t1.Assignment.Should().BeSameAs(l1);
            t1.AssignTo(null);
            t1.Assignment.Should().BeNull();
            l1.Trains.Count().Should().Be(0);
        }

Aucun commentaire:

Enregistrer un commentaire