Beispiel #1
0
        public async Task <IActionResult> Create(PeopleCreateVM p)
        {
            if (!ModelState.IsValid)
            {
                return(View(p));
            }

            await service.AddPersonAsync(p);

            return(RedirectToAction(nameof(Index)));
        }
        public void AddPerson_AddsThePersonAndCallsSaveChanges()
        {
            // Mocking DbSet for "Add" operations is quite straight-forward

            // Arrange
            var mockSet     = Substitute.For <DbSet <Person> >();
            var mockContext = Substitute.For <IPeopleDbContext>();

            mockContext.People.Returns(mockSet);
            var service = new PeopleService(mockContext);

            // Act
            service.AddPersonAsync(new Person {
                FirstName = "John", LastName = "Doe"
            });

            // Assert
            // verify that DbSet.Add has been called once
            mockSet.Received(1).Add(Arg.Any <Person>());
            // verify that DbContext.SaveChangesAsync has been called once
            mockContext.Received(1).SaveChangesAsync();
        }