コード例 #1
0
        public async Task GetDataByIdAsync()
        {
            //Arrange
            var data = new List <DefaultModel>()
            {
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 1, Field3 = "default2", Field4 = 2
                },
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 2, Field3 = "default2", Field4 = 3
                }
            };

            var dbSet   = NSubstituteUtils.CreateMockDbSet(data);
            var context = Substitute.For <DefaultContext>();

            context.DefaultModels = dbSet;

            var controller = new DefaultController(context);

            //Act
            var result = await controller.GetDataByIdAsync(data[1].Id);

            //Assert
            Assert.IsInstanceOf(typeof(OkObjectResult), result);
            var resultValue = result as OkObjectResult;

            Assert.AreEqual(data[1], resultValue.Value);
        }
コード例 #2
0
        public async Task GetDataById_NotFoundResultAsync()
        {
            //Arrange
            var data = new List <DefaultModel>()
            {
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 1, Field3 = "default2", Field4 = 2
                },
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 2, Field3 = "default2", Field4 = 3
                }
            };

            var dbSet   = NSubstituteUtils.CreateMockDbSet(data);
            var context = Substitute.For <DefaultContext>();

            context.DefaultModels = dbSet;

            var controller = new DefaultController(context);

            //Act
            var result = await controller.GetDataByIdAsync(Guid.Empty);

            //Assert
            Assert.IsInstanceOf(typeof(NotFoundResult), result);
        }
コード例 #3
0
        public async Task DeleteDataById()
        {
            //Arrange
            var data = new List <DefaultModel>()
            {
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 1, Field3 = "default2", Field4 = 2
                },
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 2, Field3 = "default2", Field4 = 3
                }
            };

            var dbSet   = NSubstituteUtils.CreateMockDbSet(data);
            var context = Substitute.For <DefaultContext>();

            context.DefaultModels = dbSet;

            var controller = new DefaultController(context);

            //Act
            var result = await controller.DeleteDataById(data[0].Id);

            //Assert
            Assert.IsInstanceOf(typeof(OkObjectResult), result);
            dbSet.Received(1).Remove(Arg.Any <DefaultModel>());
            await context.Received(1).SaveChangesAsync();
        }
コード例 #4
0
        public void GetAllData()
        {
            //Arrange
            var data = new List <DefaultModel>()
            {
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 1, Field3 = "default2", Field4 = 2
                },
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 2, Field3 = "default2", Field4 = 3
                }
            };

            var dbSet   = NSubstituteUtils.CreateMockDbSet(data);
            var context = Substitute.For <DefaultContext>();

            context.DefaultModels = dbSet;

            var controller = new DefaultController(context);

            //Act
            var result = controller.GetAllData().ToList();

            //Assert
            Assert.AreEqual(data, result);
        }
コード例 #5
0
        public async Task UpdateDataById_ModelStateError()
        {
            //Arrange
            var data = new List <DefaultModel>()
            {
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 1, Field3 = "default2", Field4 = 2
                },
                new DefaultModel {
                    Id = Guid.NewGuid(), Field1 = "default", Field2 = 2, Field3 = "default2", Field4 = 3
                }
            };

            var dbSet   = NSubstituteUtils.CreateMockDbSet(data);
            var context = Substitute.For <DefaultContext>();

            context.DefaultModels = dbSet;

            var controller = new DefaultController(context);

            controller.ModelState.AddModelError("test", "Error");

            var newValue = new DefaultModel {
                Id = data[0].Id, Field1 = "default", Field2 = 5, Field3 = "default2", Field4 = 7
            };

            //Act
            var result = await controller.UpdateDataByIdAsync(newValue);

            //Assert
            Assert.IsInstanceOf(typeof(BadRequestObjectResult), result);
        }