Exemplo n.º 1
0
        public async Task GetMutant_0003_Wolverine()
        {
            // arrange
            var options = new DbContextOptionsBuilder <MutantsContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetMutant_0003_Wolverine))
                          .Options;

            using (var context = new MutantsContext(options))
            {
                context.Mutants.Add(new Mutant
                {
                    Name       = "Wolverine",
                    RealName   = "Logan",
                    Superpower = "Invulnerability, Claws"
                });
                context.SaveChanges();
            }

            var currentfactory = factory
                                 .WithWebHostBuilder(builder => builder.ConfigureTestServices(
                                                         services => services.AddSingleton(new MutantsContext(options))));

            var client = currentfactory.CreateClient();

            // act
            var response = await client.GetAsync("api/mutants/Wolverine");

            // assert
            response.EnsureSuccessStatusCode();
            var result = await response.Content.ReadAsAsync <Mutant>();

            Assert.Equal("Wolverine", result.Name);
            Assert.Equal("Logan", result.RealName);
            Assert.Equal("Invulnerability, Claws", result.Superpower);
        }
Exemplo n.º 2
0
        public void Scenario_GetMutant_0003_Wolverine(DbContextOptions <MutantsContext> options, WebApplicationFactory <TestStartup> currentfactory,
                                                      HttpResponseMessage response)
        {
            $"Given database".x(() =>
            {
                options = new DbContextOptionsBuilder <MutantsContext>()
                          .UseInMemoryDatabase(databaseName: nameof(Scenario_GetMutant_0003_Wolverine))
                          .Options;
            });

            var mutant = new Mutant
            {
                Name       = "Wolverine",
                RealName   = "Logan",
                Superpower = "Invulnerability, Claws"
            };

            $"And the mutant {mutant}".x(() =>
            {
                using (var context = new MutantsContext(options))
                {
                    context.Mutants.Add(mutant);
                    context.SaveChanges();
                }
                currentfactory = factory
                                 .WithWebHostBuilder(builder => builder.ConfigureTestServices(
                                                         services => services.AddSingleton(new MutantsContext(options))));
            });

            $"When get mutant by name Wolverine"
            .x(async() => response = await currentfactory.CreateClient().GetAsync("api/mutants/Wolverine"));

            "Then returns the mutant Wolverine (Logan): Invulnerability, Claws"
            .x(async() =>
            {
                response.EnsureSuccessStatusCode();
                var result = await response.Content.ReadAsAsync <Mutant>();
                Assert.Equal("Wolverine", result.Name);
                Assert.Equal("Logan", result.RealName);
                Assert.Equal("Invulnerability, Claws", result.Superpower);
            });
        }