Example #1
0
        public void AddingCustomerWithCidShouldFailAndThrowException()
        {
            //Given a BusinessPartner repository
            var numberOfCustomerBefore = DalService.CreateUnitOfWork().BusinessPartners.CountAsync().Result;
            //When Adding a BusinessPartner with a Cid (not null)
            var businessPartner = new BusinessPartner
            {
                Key  = "000",
                Name = "testName"
            };
            //Than the adding function will throw an exception
            BusinessPartner addedBusinessPartner = null;
            Action          comparison           = () =>
            {
                using var uow        = DalService.CreateUnitOfWork();
                addedBusinessPartner = uow.BusinessPartners.AddAsync(businessPartner).Result;
                uow.CompleteAsync().Wait();
            };

            comparison.Should().Throw <Exception>();

            //and the database will not affected
            DalService.CreateUnitOfWork().BusinessPartners.CountAsync().Result.Should().Be(numberOfCustomerBefore);

            //and the returned BusinessPartner need to be null
            (addedBusinessPartner == null).Should().BeTrue();
        }
Example #2
0
        public void AddCustomerListWithOneCustomerThatFailWillThrowExceptionAndRollback()
        {
            //Given a BusinessPartner repository
            var numberOfCustomerBefore = DalService.CreateUnitOfWork().BusinessPartners.CountAsync().Result;

            //When Adding a BusinessPartner list with one or more BusinessPartner that have a Cid (not null)
            var customerList = Enumerable.Range(1, 5)
                               .Select(i => new BusinessPartner {
                Name = "TESTAddCustomerListWithOneCustomerThatFailWillRollback " + i
            })
                               .ToList();

            customerList.ElementAt(3).Key = "000";
            customerList.ElementAt(4).Key = "000";
            Action comparison = () =>
            {
                using var uow = DalService.CreateUnitOfWork();
                uow.BusinessPartners.AddAsync(customerList).Wait();
                uow.CompleteAsync().Wait();
            };


            //Then action will throw an exception
            comparison.Should().Throw <Exception>();

            //and the number of elements in the repository will not affected - transaction will rollback
            var numberOfCustomerAfterTransaction = DalService.CreateUnitOfWork().BusinessPartners.CountAsync().Result;

            numberOfCustomerAfterTransaction.Should().Be(numberOfCustomerBefore);
        }
Example #3
0
        public void ActionWithAbility_WhenAbilityIsNotRegistered_ShouldThrow(System.Action <Actor, IFixture> action)
        {
            //arrange
            var fixture = new Fixture().Customize(new ActorCustomization()).Customize(new AutoMoqCustomization()
            {
                ConfigureMembers = true
            });
            var sut = fixture.Create <Actor>();

            System.Action testedAction = () => action(sut, fixture);
            //act and assert
            testedAction.Should().Throw <InvalidOperationException>().Where(ex => ex.Message.Contains(typeof(AbilityTest).Name));
        }
Example #4
0
        public void WithData_DataIsDifferentObject()
        {
            var          actualData    = new int[] { 10, 20, 30, 40 };
            var          differentData = new string[] { "10", "20", "30", "40" };
            ActionResult result        = new JsonResult {
                Data = actualData
            };
            var expectedMessage = string.Format(FailureMessages.CommonFailMessage, "JsonResult.Data", differentData, actualData);

            System.Action act = () => result.Should().BeJsonResult().WithData(differentData);

            act.Should().Throw <Exception>().WithMessage(expectedMessage);
        }
Example #5
0
        public void WithData_DataIsDifferentObjectOfDifferentValue()
        {
            var actualData      = new ObjectWithEquality("hello world");
            var expectedData    = new ObjectWithEquality("goodbye cruel world");
            var expectedMessage = string.Format(FailureMessages.CommonFailMessage, "JsonResult.Data", expectedData, actualData);

            ActionResult result = new JsonResult {
                Data = actualData
            };

            System.Action act = () => result.Should().BeJsonResult().WithData(expectedData);

            act.Should().Throw <Exception>().WithMessage(expectedMessage);
        }
Example #6
0
        public void WithDataPredicate_ShouldFail()
        {
            var          actualData = new int[] { 10, 20, 30, 40 };
            ActionResult result     = new JsonResult {
                Data = actualData
            };
            var expectedMessage = string.Format(FailureMessages.JsonResult_WithDataPredicate, "JsonResult.Data", actualData);

            System.Action act = () => result.Should()
                                .BeJsonResult()
                                .WithData(d => false);

            act.Should().Throw <Exception>().WithMessage(expectedMessage);
        }
Example #7
0
        public void WithContentResult_ShouldFail()
        {
            ActionResult result = new ContentResult {
                Content = "{ \"name\" : \"WithJson_GivenExpected_ShouldPass\" }"
            };

            // this is a bit of a hack to build the expected message.  There must be a better way?
            var messageFormat = ActionResultAssertions.Constants.CommonFailMessage
                                .Replace("{reason}", "")
                                .Replace("{", "\"{")
                                .Replace("}", "}\"");
            var failureMessage = String.Format(messageFormat, typeof(JsonResult).Name, result.GetType().Name);

            System.Action act = () => result.Should().BeJsonResult();

            act.Should().Throw <System.Exception>().WithMessage(failureMessage);
        }