public void GetPersonNoTracking_ReturnsThePersonWithTheGivenId()
        {
            // Arrange

            // first create the collection of data. It no longer has to be an IQueryable
            var data = new List <Person>
            {
                new Person {
                    Id = 1, FirstName = "BBB"
                },
                new Person {
                    Id = 2, FirstName = "ZZZ"
                },
                new Person {
                    Id = 3, FirstName = "AAA"
                },
            };

            // create the mock DbSet using the helper method
            var mockSet = NSubstituteUtils.CreateMockDbSet(data);
            // do the wiring between DbContext and DbSet
            var mockContext = Substitute.For <IPeopleDbContext>();

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

            // Act
            var secondPerson = service.GetPersonNoTracking(2);

            // Assert
            Assert.That(secondPerson.Id, Is.EqualTo(2));
            Assert.That(secondPerson.FirstName, Is.EqualTo("ZZZ"));
        }
Beispiel #2
0
        public async void MockDbSetCanBeEnumeratedMoreThanOnce_Asynchronously()
        {
            var data = new List <Person>
            {
                new Person {
                    Id = 1, FirstName = "BBB"
                },
                new Person {
                    Id = 2, FirstName = "ZZZ"
                },
                new Person {
                    Id = 3, FirstName = "AAA"
                },
            };

            var mockSet     = NSubstituteUtils.CreateMockDbSet(data);
            var mockContext = Substitute.For <IPeopleDbContext>();

            mockContext.People.Returns(mockSet);

            var people1 = await mockContext.People.ToArrayAsync();

            var people2 = await mockContext.People.ToArrayAsync();

            Assert.That(people1.Length, Is.EqualTo(3));
            Assert.That(people2.Length, Is.EqualTo(3));
        }
 public ActorsRepositoryTest()
 {
     this._Actors            = new MockActors();
     this._mockDbSet         = NSubstituteUtils.CreateMockDbSet(_Actors.Actors);
     this._mockSakilaContext = Substitute.For <IsakilaContext>();
     _mockSakilaContext.Actor.Returns(_mockDbSet);
     this._actors = new ActorsRepository(_mockSakilaContext);
 }
        public async Task AddOrUpdatePerson_CallsAddOrUpdateFromDbSet()
        {
            // Arrange
            var mockSet     = NSubstituteUtils.CreateMockDbSet <Person>();
            var mockContext = Substitute.For <IPeopleDbContext>();

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

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

            // Assert
            // verify that DbSet.AddOrUpdate has been called once
            mockSet.Received(1).AddOrUpdate(Arg.Any <Person>());
            await mockContext.Received(1).SaveChangesAsync();
        }
Beispiel #5
0
        public Action <ContainerBuilder> SetupEnvironment()
        {
            _mockDbContext.Vendors = NSubstituteUtils.CreateMockDbSet(new[]
            {
                new  Vendor
                {
                    Id             = 1,
                    Name           = "NGN",
                    PrimaryContact = "primary",
                    Projects       = new List <Project>
                    {
                        new Project
                        {
                            Code      = "PP123456",
                            CodeDrops =
                                new List <CodeDrop>
                            {
                                new CodeDrop
                                {
                                    ActualDate   = DateTime.Now.AddDays(5),
                                    Id           = 1,
                                    TargetedDate = DateTime.Now,
                                    VendorName   = "NGN"
                                }
                            }, SoftwareRelease = new SoftwareRelease()
                        }
                    },
                    SecondaryContact = "secondary"
                }
            });

            _mockDbContextFactory.DbContext.Returns(_mockDbContext);

            //Action
            //Register all mocked objects
            Action <ContainerBuilder> autofacOverrides = builder =>
            {
                builder.RegisterInstance(_mockDbContextFactory).As <IDbContextFactory>();
            };

            return(autofacOverrides);
        }
        public async Task GetPersonAsync_ReturnsThePersonWithTheGivenId()
        {
            // The above setup for one single test looks too much
            // let's encapsulate all that code inside the
            // NSubstituteUtils.CreateMockDbSet static method

            // Arrange

            // first create the collection of data. It no longer has to be an IQueryable
            var data = new List <Person>
            {
                new Person {
                    Id = 1, FirstName = "BBB"
                },
                new Person {
                    Id = 2, FirstName = "ZZZ"
                },
                new Person {
                    Id = 3, FirstName = "AAA"
                },
            };

            // create the mock DbSet using the helper method
            var mockSet = NSubstituteUtils.CreateMockDbSet(data);
            // do the wiring between DbContext and DbSet
            var mockContext = Substitute.For <IPeopleDbContext>();

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

            // Act
            var secondPerson = await service.GetPersonAsync(2);

            // Assert
            Assert.That(secondPerson.Id, Is.EqualTo(2));
            Assert.That(secondPerson.FirstName, Is.EqualTo("ZZZ"));
        }
        public void RemovePerson_CallsRemoveAndSaveFromDbSet()
        {
            // Let's also make sure that our static helper method
            // works well for non-read operations, where no
            // initial data is required

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

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

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

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