コード例 #1
0
        public async void CreateDefaultElectionUnitTest()
        {
            // Arrange
            string templatePath           = $".{Path.DirectorySeparatorChar}ElectionTemplate.json";
            var    mockElectionRepository = GetElectionRepositoryMock();

            mockElectionRepository.Setup(repo => repo.AddAsync(It.IsAny <Election>()))
            .ReturnsAsync((Election election) => election);

            var electionService = new ElectionService(mockElectionRepository.Object);

            var mockElectionTemplateLoader = new Mock <IElectionJsonTemplateLoader>();

            mockElectionTemplateLoader.Setup(el => el.Load(It.IsAny <string>()))
            .Returns((string str) =>
                     new Election
            {
                ElectionQr = Guid.NewGuid()
            });


            // Act
            var result = await electionService.CreateElectionUsingTemplateAsync(templatePath, mockElectionTemplateLoader.Object);

            // Assert
            mockElectionTemplateLoader.Verify(loader => loader.Load(It.IsAny <string>()), Times.AtMostOnce);

            Assert.NotNull(result);
            await Assert.ThrowsAsync <TemplateNotFoundException>(async() => await electionService.CreateElectionUsingTemplateAsync("test", mockElectionTemplateLoader.Object));
        }