コード例 #1
0
        public async Task AddStageAsync_AddsStage_WhenSuccessful()
        {
            // Arrange
            var connectionStringBuild =
                new SqliteConnectionStringBuilder {
                DataSource = ":memory:"
            };
            var connection = new SqliteConnection(connectionStringBuild.ToString());

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

            using (var context = new StageBuilderDbContext(options))
            {
                await context.Database.OpenConnectionAsync();

                await context.Database.EnsureCreatedAsync();

                var stage = new StageEntity()
                {
                    UserId          = 1,
                    GameId          = 1,
                    CreatedDate     = DateTime.Now,
                    LastUpdatedDate = DateTime.Now
                };
                var service = new StageService(context);

                // Act 1
                var entity = await service.AddStageAsync(stage);

                // Assertion 1
                var expected = "Test stage 1";
                var msg      = $"Stage {entity.Name} was returned from add method";
                entity.Name.Should().Be(expected, because: msg);
            }

            using (var context = new StageBuilderDbContext(options))
            {
                var stages = context.Stages;

                // Act 2 - Check if item is is DB
                var id     = 1;
                var entity = stages.First(p => p.StageId == id);

                // Assertion 2
                var expected = "Test stage 1";
                var msg      = $"Stage {entity.Name} exists in database";
                entity.Name.Should().Be(expected, because: msg);
            }
        }