예제 #1
0
        public void Devices_AddPartThrowsException_WhenPartNotCompatible(string type)
        {
            switch (type)
            {
            case "Laptop":
                var laptop = new Laptop("make");

                Assert.Throws <InvalidOperationException>(() => laptop.AddPart(phonePart))
                .Message.Equals($"You cannot add {phonePart.GetType().Name} to {this.GetType().Name}!");

                Assert.Throws <InvalidOperationException>(() => laptop.AddPart(pcPart))
                .Message.Equals($"You cannot add {pcPart.GetType().Name} to {this.GetType().Name}!");
                break;

            case "PC":
                var pc = new PC("make");

                Assert.Throws <InvalidOperationException>(() => pc.AddPart(laptopPart))
                .Message.Equals($"You cannot add {laptopPart.GetType().Name} to {this.GetType().Name}!");

                Assert.Throws <InvalidOperationException>(() => pc.AddPart(phonePart))
                .Message.Equals($"You cannot add {phonePart.GetType().Name} to {this.GetType().Name}!");
                break;

            case "Phone":
                var phone = new Phone("make");

                Assert.Throws <InvalidOperationException>(() => phone.AddPart(pcPart))
                .Message.Equals($"You cannot add {pcPart.GetType().Name} to {this.GetType().Name}!");

                Assert.Throws <InvalidOperationException>(() => phone.AddPart(laptopPart))
                .Message.Equals($"You cannot add {laptopPart.GetType().Name} to {this.GetType().Name}!");
                break;
            }
        }
        public void TestAddPartLaptopWithNonLaptopPart()
        {
            IPart  part = new PCPart("GPU", 150, false);
            string expectedExceptionMessage = $"You cannot add {part.GetType().Name} to {this.laptop.GetType().Name}!";

            Assert.That(() => this.laptop.AddPart(part), Throws.InvalidOperationException.With.Message.EqualTo(expectedExceptionMessage));
        }
        public void TestAddPartPhoneWithNonPhonePart()
        {
            IPart part  = new PCPart("GPU", 150, false);
            Phone phone = new Phone("Samsung");

            string expectedExceptionMessage = $"You cannot add {part.GetType().Name} to {phone.GetType().Name}!";

            Assert.That(() => phone.AddPart(part), Throws.InvalidOperationException.With.Message.EqualTo(expectedExceptionMessage));
        }