public async Task GetAllActiveGetsOnlyActiveChores()
        {
            using (var dbInfo = Utility.CreateSeededTestDatabase())
                using (var context = new EntitiesContext(dbInfo.ConnectionString))
                    using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
                    {
                        IChoreService    service    = new ChoreService(unitOfWork.RepositoryAsync <Chore>());
                        ChoresController controller = new ChoresController(unitOfWork, service, new FakeTimeService(new DateTime(2015, 11, 1)));

                        controller.SetControllerContext(HttpMethod.Post, "http://localhost/RowanAdams/api/chores");
                        await controller.Post(new Chore { Name = "Test Chore 1", Value = 1 });

                        await controller.Post(new Chore { Name = "Test Chore 2", Value = 2 });

                        await controller.Post(new Chore { Name = "Test Chore 3", Value = 3 });

                        await controller.Post(new Chore { Name = "Test Chore 4", Value = 4 });

                        var chore = context.Chores.First();
                        chore.Active = false;

                        controller.SetControllerContext(HttpMethod.Put, "http://localhost/RowanAdams/api/chores");
                        await controller.Put(chore);

                        controller.SetControllerContext(HttpMethod.Get, "http://localhost/RowanAdams/api/chores/active");
                        var chores = await controller.GetActive();

                        Assert.AreEqual(3, chores.Count());
                    }
        }
        public async Task EnsurePutUpdatesExistingChoreAndSetsHttpStatus()
        {
            using (var dbInfo = Utility.CreateSeededTestDatabase())
                using (var context = new EntitiesContext(dbInfo.ConnectionString))
                    using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
                    {
                        IChoreService    service    = new ChoreService(unitOfWork.RepositoryAsync <Chore>());
                        ChoresController controller = new ChoresController(unitOfWork, service, new FakeTimeService(new DateTime(2015, 11, 1)));

                        controller.SetControllerContext(HttpMethod.Post, "http://localhost/RowanAdams/api/chores");
                        var result = await controller.Post(new Chore { Name = "Test Chore 1", Value = 10 });

                        var chore = context.Chores.Single();
                        chore.Active = false;

                        controller.SetControllerContext(HttpMethod.Put, "http://localhost/RowanAdams/api/chores");
                        result = await controller.Put(chore);

                        var response = await result.ExecuteAsync(new CancellationToken());

                        var chore2 = context.Chores.Single();
                        Assert.AreEqual(chore.Id, chore2.Id);
                        Assert.IsFalse(chore.Active);
                        Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
                        Assert.AreEqual($"/api/chores/{chore2.Id}", response.Headers.Location.AbsolutePath);
                    }
        }
예제 #3
0
        public void Initialize()
        {
            dbInfo = Utility.CreateSeededTestDatabase();

            using (var context = new EntitiesContext(dbInfo.ConnectionString))
                using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
                {
                    IChoreService    service    = new ChoreService(unitOfWork.RepositoryAsync <Chore>());
                    ChoresController controller = new ChoresController(unitOfWork, service, new FakeTimeService(new DateTime(2015, 11, 1)))
                                                  .SetControllerContext(HttpMethod.Post, "http://localhost/RowanAdams/api/chores");

                    var result = controller.Post(new Chore {
                        Name = "Test Chore 1", Value = 10
                    });
                    result.Wait();
                }
        }
        public async Task EnsurePostReturnsErrorWhenInvalidModel()
        {
            using (var dbInfo = Utility.CreateSeededTestDatabase())
                using (var context = new EntitiesContext(dbInfo.ConnectionString))
                    using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
                    {
                        IChoreService    service    = new ChoreService(unitOfWork.RepositoryAsync <Chore>());
                        ChoresController controller = new ChoresController(unitOfWork, service, new FakeTimeService(new DateTime(2015, 11, 1)));

                        controller.ModelState.AddModelError("TestError", new Exception());
                        controller.SetControllerContext(HttpMethod.Post, "http://localhost/RowanAdams/api/chores");
                        var result = await controller.Post(new Chore { });

                        var response = await result.ExecuteAsync(new CancellationToken());

                        Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
                    }
        }
예제 #5
0
        public async void PostChore_ShouldReturnCreated_WhenSuccessful()
        {
            mockMapper.Setup(m => m.Map <Chore>(mopViewModel))
            .Returns(mop);
            mockMapper.Setup(m => m.Map <ChoreViewModel>(mop))
            .Returns(mopViewModel);
            mockRepo.Setup(r => r.SaveAllAsync())
            .Returns(Task.FromResult(true));

            var result = await controller.Post(mopViewModel);

            var createdResult = result.Should().BeAssignableTo <CreatedAtRouteResult>().Subject;

            createdResult.Value.Should().Be(mopViewModel);
        }
        public async Task EnsurePutReturnsNotFoundWhenChoreDoesNotExist()
        {
            using (var dbInfo = Utility.CreateSeededTestDatabase())
                using (var context = new EntitiesContext(dbInfo.ConnectionString))
                    using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(context))
                    {
                        IChoreService    service    = new ChoreService(unitOfWork.RepositoryAsync <Chore>());
                        ChoresController controller = new ChoresController(unitOfWork, service, new FakeTimeService(new DateTime(2015, 11, 1)));

                        controller.SetControllerContext(HttpMethod.Post, "http://localhost/RowanAdams/api/chores");
                        var result = await controller.Post(new Chore { Name = "Test Chore 1", Value = 10 });

                        controller.SetControllerContext(HttpMethod.Put, "http://localhost/RowanAdams/api/chores");
                        result = await controller.Put(new Chore()
                        {
                            Id = Guid.NewGuid(), Name = "Test Chore 1"
                        });

                        var response = await result.ExecuteAsync(new CancellationToken());

                        Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
                    }
        }