コード例 #1
0
        public void GetShouldReturnNullIfRepositoryThrowsExceptions()
        {
            // Arrange
            var mock   = new Mock <IDeliveryPersonRepository>();
            var mockId = Guid.NewGuid();

            mock.Setup <DeliveryPerson>(_ => _.GetDeliveryPersonById(mockId))
            .Throws <Exception>();
            Repository = mock.Object;
            Service    = new DeliveryPersonService(Repository);

            // Act
            var result = Service.Get(mockId);

            // Assert
            Assert.AreEqual(null, result);
        }
コード例 #2
0
        public void GetShouldReturnTheSameDataIfRepositoryReturnsData()
        {
            // Arrange
            var mock     = new Mock <IDeliveryPersonRepository>();
            var mockId   = Guid.NewGuid();
            var mockData = Builder <DeliveryPerson> .CreateNew().Build();

            mock.Setup <DeliveryPerson>(_ => _.GetDeliveryPersonById(mockId))
            .Returns(mockData);
            Repository = mock.Object;
            Service    = new DeliveryPersonService(Repository);

            // Act
            var  result = Service.Get(mockId);
            bool same   = mockData.FirstName == result.FirstName &&
                          mockData.LastName == result.LastName;

            // Assert
            Assert.IsTrue(same);
        }