コード例 #1
0
        public async System.Threading.Tasks.Task GetUserIDTestAsync()
        {
            var options = new DbContextOptionsBuilder <UsersDBContext>()
                          .UseInMemoryDatabase(databaseName: "User")
                          .Options;

            // Insert seed data into the database using one instance of the context
            using (var context = new UsersDBContext(options))
            {
                context.Add(new User()
                {
                    Address = "142345678910111213141516171819a", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test"
                });
                context.Add(new User()
                {
                    Address = "142345678910111213141516171819a", DateOfBith = DateTime.Now, FirstName = "Correct", LastName = "Test1234"
                });
                context.Add(new User()
                {
                    Address = "142345678910111213141516171819a", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test"
                });
                context.SaveChanges();

                var Service     = new GetUserService(context);
                var cancelToken = new CancellationToken();


                var response = await Service.Handle(new GetUserRequest()
                {
                    Id = 1
                }, cancelToken).ConfigureAwait(false);


                Assert.IsNotNull(response);
                Assert.IsTrue(response.Id == 1);
            }
        }
コード例 #2
0
        public void BasicFindingCorrectByIdUserTest()
        {
            var options = new DbContextOptionsBuilder <UsersDBContext>()
                          .UseInMemoryDatabase(databaseName: "User")
                          .Options;

            // Insert seed data into the database using one instance of the context
            using (var context = new UsersDBContext(options))
            {
                context.Add(new User()
                {
                    Address = "12345", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test"
                });
                context.Add(new User()
                {
                    Address = "12345", DateOfBith = DateTime.Now, FirstName = "Correct", LastName = "Test1234"
                });
                context.Add(new User()
                {
                    Address = "12345", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test"
                });
                context.SaveChanges();
            }

            // Use a clean instance of the context to run the test
            using (var context = new UsersDBContext(options))
            {
                var user = context.Users.FirstOrDefault(s => s.Id == 3);
                Assert.IsNotNull(user);
                Assert.IsFalse(string.IsNullOrWhiteSpace(user.FirstName));
                Assert.IsFalse(string.IsNullOrWhiteSpace(user.LastName));
                Assert.IsFalse(string.IsNullOrWhiteSpace(user.Address));
                Assert.IsTrue(user.DateOfBith > DateTime.Now.AddDays(-1));
                Assert.IsTrue(user.LastName.Equals("Test1234", StringComparison.InvariantCultureIgnoreCase));
            }
        }
コード例 #3
0
        public void BasicAddingUserTest()
        {
            var options = new DbContextOptionsBuilder <UsersDBContext>()
                          .UseInMemoryDatabase(databaseName: "User")
                          .Options;

            // Insert seed data into the database using one instance of the context
            using (var context = new UsersDBContext(options))
            {
                context.Add(new User()
                {
                    Address = "12345", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test"
                });
                context.SaveChanges();
            }

            // Use a clean instance of the context to run the test
            using (var context = new UsersDBContext(options))
            {
                List <User> users = context.Users.Select(s => s).ToList();
                Assert.AreEqual(1, users.Count);
            }
        }
コード例 #4
0
        public void TestDbValidationOfDataModel()
        {
            var options = new DbContextOptionsBuilder <UsersDBContext>()
                          .UseInMemoryDatabase(databaseName: "User")
                          .Options;

            try
            {
                // Insert seed data into the database using one instance of the context
                using (var context = new UsersDBContext(options))
                {
                    context.Add(new User()
                    {
                        Address = "12345", DateOfBith = DateTime.Now, FirstName = "", LastName = "Test"
                    });
                    context.SaveChanges();
                }
            }
            catch (ValidationException ex)
            {
                Assert.IsTrue(ex.Message.Contains("The FirstName field is required"));
            }
        }