コード例 #1
0
        public async Task Details_RetunrsViewResult_WhenRacaExists()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseSqlite(connection)
                          .Options;

            using (var context = new ApplicationDbContext(options))
            {
                context.Database.EnsureCreated();
                context.Raca.Add(new Raca {
                    Designacao = "Labrador"
                });
                context.SaveChanges();
            }
            using (var context = new ApplicationDbContext(options))
            {
                var controller = new RacaController(context);

                var result = await controller.Details(1);

                var viewResult = Assert.IsType <ViewResult>(result);
                var model      = Assert.IsAssignableFrom <Raca>(viewResult.ViewData.Model);
                Assert.Equal(1, model.RacaId);
            }
        }
コード例 #2
0
        public async Task Details_ReturnsNotFoundResult_WhenIdIsNull()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseSqlite(connection)
                          .Options;

            using (var context = new ApplicationDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SaveChanges();
            }
            using (var context = new ApplicationDbContext(options))
            {
                var controller = new RacaController(context);

                var result = await controller.Details(null);

                Assert.IsType <NotFoundResult>(result);
            }
        }
コード例 #3
0
        public async Task Index_CanLoadFromContext()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseSqlite(connection)
                          .Options;

            using (var context = new ApplicationDbContext(options))
            {
                context.Database.EnsureCreated();
                context.Raca.AddRange(
                    new Raca {
                    Designacao = "Labrador"
                },
                    new Raca {
                    Designacao = "Pug"
                },
                    new Raca {
                    Designacao = "Husky"
                });
                context.SaveChanges();
            }
            using (var context = new ApplicationDbContext(options))
            {
                var controller = new RacaController(context);

                var result = await controller.Index();

                var viewResult = Assert.IsType <ViewResult>(result);
                var model      = Assert.IsAssignableFrom <IEnumerable <Raca> >(
                    viewResult.ViewData.Model);
                Assert.Equal(3, model.Count());
            }
        }