コード例 #1
0
        public void getPhoneById()
        {
            var   mock   = new Mock <IRepository <Phone> >();
            Phone phone1 = new Phone {
                Id = 1, Name = "Samsung S9", Price = 200
            };

            mock.Setup(m => m.FindById(1)).Returns(phone1);
            IPhoneCW _phoneWorker = new PhoneCW(mock.Object);


            Assert.IsNotNull(_phoneWorker.getPhoneByIdentifier(1)); //Returns phone
            Assert.IsNull(_phoneWorker.getPhoneByIdentifier(0));    //Returns null
        }
コード例 #2
0
        public void getPricesByIdsNotFound()
        {
            var        mock = new Mock <IRepository <Phone> >();
            List <int> ids  = new List <int> {
                1, 2
            };

            mock.Setup(m => m.FindAll(It.IsAny <Expression <Func <Phone, bool> > >())).Returns(new List <Phone>());

            IPhoneCW _phoneWorker = new PhoneCW(mock.Object);
            var      result       = _phoneWorker.getPricesByIdentifiers(ids);

            Assert.IsFalse(result.Any());
        }
コード例 #3
0
        public void getPhones()
        {
            var   mock   = new Mock <IRepository <Phone> >();
            Phone phone1 = new Phone {
                Id = 1, Name = "Samsung S9", Price = 200
            };

            mock.Setup(m => m.FindAll()).Returns(new List <Phone> {
                phone1
            });
            IPhoneCW _phoneWorker = new PhoneCW(mock.Object);

            List <PhoneDTO> phones = _phoneWorker.getPhones();

            Assert.AreNotEqual(0, phones.Count);
        }
コード例 #4
0
        public void getPricesByIdsWithZeroPrice()
        {
            var   mock   = new Mock <IRepository <Phone> >();
            Phone phone1 = new Phone {
                Id = 1, Name = "Samsung S9", Price = 0
            };
            Phone phone2 = new Phone {
                Id = 2, Name = "Samsung S8", Price = 150
            };
            List <Phone> mockResult = new List <Phone> {
                phone1, phone2
            };
            List <int> ids = new List <int> {
                1, 2
            };

            mock.Setup(m => m.FindAll(It.IsAny <Expression <Func <Phone, bool> > >())).Returns(mockResult);

            IPhoneCW _phoneWorker = new PhoneCW(mock.Object);
            var      result       = _phoneWorker.getPricesByIdentifiers(ids);

            Assert.IsTrue(result.Any(x => x.PhonePrice == 0));
        }