public void AddCountry_SuccessfullyAddsACountry_WhenAllValuesAreCorrect()
        {
            // Arrange
            var countries = new List <Country>().AsQueryable();

            var countryMockDbSet = new Mock <DbSet <Country> >();

            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.Provider).Returns(countries.Provider);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.Expression).Returns(countries.Expression);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.ElementType).Returns(countries.ElementType);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.GetEnumerator()).Returns(countries.GetEnumerator());

            var mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(m => m.Countries).Returns(countryMockDbSet.Object);

            var countryBusiness = new CountryBusiness(mockContext.Object);

            // Act
            countryBusiness.Add("TestCountry", 1);

            // Assert
            countryMockDbSet.Verify(m => m.Add(It.IsAny <Country>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
示例#2
0
        public Country Add(Country country)
        {
            try
            {
                var bc = new CountryBusiness();
                return(bc.Add(country));
            }
            catch (Exception ex)
            {
                var httpError = new HttpResponseMessage()
                {
                    StatusCode   = (HttpStatusCode)422,
                    ReasonPhrase = ex.Message
                };

                throw new HttpResponseException(httpError);
            }
        }
        public void AddCountry_ThrowsArgumentException_WhenIdIsDuplicate()
        {
            // Arrange
            int duplicateId = 1;
            var countries   = new List <Country>
            {
                new Country {
                    Name = "TestCountry1", Id = duplicateId
                },
            }.AsQueryable();

            var countryMockDbSet = new Mock <DbSet <Country> >();

            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.Provider).Returns(countries.Provider);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.Expression).Returns(countries.Expression);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.ElementType).Returns(countries.ElementType);
            countryMockDbSet.As <IQueryable <Country> >().Setup(m => m.GetEnumerator()).Returns(countries.GetEnumerator());

            var mockContext = new Mock <EazyCartContext>();

            mockContext.Setup(c => c.Countries).Returns(countryMockDbSet.Object);
            mockContext.Setup(c => c.SaveChanges()).Throws(new Exception());
            var countryBusiness = new CountryBusiness(mockContext.Object);

            // Act & Assert
            try
            {
                countryBusiness.Add("TestCountry1", duplicateId);
                Assert.Fail("No exception was thrown");
            }
            catch (ArgumentException exc)
            {
                string expectedMessage = string.Format("Country with ID {0} already exists.", duplicateId);
                Assert.AreEqual(expectedMessage, exc.Message, "Wrong exception is thrown");
            }
        }