Exemplo n.º 1
0
        public async Task DeleteAsyncShouldWorkCorrectly()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            using var db = new ApplicationDbContext(options);

            var trophyRepository = new EfDeletableEntityRepository <Trophy>(db);

            var service = new TrophiesService(null, trophyRepository);

            await trophyRepository.AddAsync(new Trophy { Id = 1 });

            await trophyRepository.AddAsync(new Trophy { Id = 2 });

            await trophyRepository.AddAsync(new Trophy { Id = 3 });

            await trophyRepository.SaveChangesAsync();

            Assert.Equal(3, service.GetCount());

            await service.DeleteAsync(1);

            await trophyRepository.SaveChangesAsync();

            Assert.Equal(2, service.GetCount());

            await service.DeleteAsync(2);

            await trophyRepository.SaveChangesAsync();

            Assert.Equal(1, service.GetCount());
        }
Exemplo n.º 2
0
        public async Task AddAsyncShouldWorkCorrect()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            using var db = new ApplicationDbContext(options);
            var trophyRepository = new EfDeletableEntityRepository <Trophy>(db);

            var clodinaryMock = new Mock <ICloudinaryService>();

            var service = new TrophiesService(clodinaryMock.Object, trophyRepository);

            // Arrange
            var fileMock = new Mock <IFormFile>();

            // Setup mock file using a memory stream
            var content  = "Hello World from a Fake File";
            var fileName = "test.jpg";
            var ms       = new MemoryStream();

            using var writer = new StreamWriter(ms);
            writer.Write(content);
            writer.Flush();
            ms.Position = 0;
            fileMock.Setup(_ => _.OpenReadStream()).Returns(ms);
            fileMock.Setup(_ => _.FileName).Returns(fileName);
            fileMock.Setup(_ => _.Length).Returns(ms.Length);

            var file = fileMock.Object;

            var input = new AddTrophyInputModel
            {
                LakeId          = 1,
                BaitDescription = "corn",
                RigId           = null,
                Weight          = 12.500,
                MainImage       = file,
                OtherImages     = new List <IFormFile> {
                    file
                },
            };

            Assert.Equal(0, service.GetCount());

            await service.AddAsync(input, Guid.NewGuid().ToString());

            await trophyRepository.SaveChangesAsync();

            Assert.Equal(1, service.GetCount());
        }
Exemplo n.º 3
0
        public void GetCountShouldWorkCorrect()
        {
            var trophiesList = new List <Trophy>
            {
                new Trophy(),
                new Trophy(),
                new Trophy(),
            };

            var repository = new Mock <IDeletableEntityRepository <Trophy> >();

            repository.Setup(r => r.All()).Returns(trophiesList.AsQueryable());

            var service = new TrophiesService(null, repository.Object);

            Assert.Equal(3, service.GetCount());
        }