Exemple #1
0
        public async Task GetContentById_ReturnsOk()
        {
            // Arrange
            var expectedContentId  = 0xdead;
            var expectedTitle      = "EXPECTED TITLE";
            var expectedContentDto = new Models.Dto.Content {
                Title = expectedTitle
            };
            var cacheKey1     = $"{nameof(ContentController)}_{expectedContentId}";
            var expectedModel = new Shared.Content(expectedContentId, string.Empty, string.Empty, string.Empty, string.Empty, new string[0]);

            object junk;

            The <IMemoryCache>().Setup(m => m.TryGetValue(cacheKey1, out junk)).Returns(false);
            The <IMemoryCache>().Setup(m => m.CreateEntry(It.IsAny <string>())).Returns(The <ICacheEntry>().Object);

            The <IContentRepository>()
            .Setup(m => m.GetAsync(expectedContentId, It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedContentDto);

            The <IMapper>()
            .Setup(m => m.Map <Shared.Content>(expectedContentDto))
            .Returns(expectedModel);

            // Act
            var response = await Target.GetContentById(expectedContentId);

            // Assert
            Assert.Same(expectedModel, response.Value);
            VerifyAll();
        }
        public async Task GetContentTextById_ReturnsOK()
        {
            // Arrange
            var expectedContentId  = 0xdead;
            var expectedUrl        = $"/api/content/text/{expectedContentId}";
            var expectedPath       = "fake/path/document.txt";
            var expectedText       = "EXPECTED TEXT";
            var expectedContentDto = new Models.Dto.Content {
                Path = expectedPath
            };

            The <IContentRepository>()
            .Setup(m => m.GetAsync(expectedContentId, It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedContentDto);

            The <ITextRepository>()
            .Setup(m => m.ReadAsync(Path.Combine(BaseDir, expectedPath), It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedText);

            // Act
            var actualText = await _client.GetStringAsync(expectedUrl);

            // Assert
            Assert.Equal(expectedText, actualText);
            VerifyAll();
        }
        public async Task GetContentById_ReturnsOK()
        {
            // Arrange
            var expectedContentId  = 0xdead;
            var expectedUrl        = $"/api/content/{expectedContentId}";
            var expectedTitle      = "EXPECTED TITLE";
            var expectedContentDto = new Models.Dto.Content {
                Title = expectedTitle
            };

            The <IContentRepository>()
            .Setup(m => m.GetAsync(expectedContentId, It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedContentDto);

            // Act
            var actualResponse = await _client.GetFromJsonAsync <Shared.Content>(expectedUrl);

            // Assert
            Assert.Equal(expectedTitle, actualResponse?.Title);
            VerifyAll();
        }
Exemple #4
0
        public async Task GetContentTextById_ReturnsOk()
        {
            // Arrange
            var expectedContentId = 0xdead;
            var expectedPath      = "fake/path/document.txt";
            var expectedBaseDir   = "c:/testing/";
            var expectedOptions   = new Models.ContentOptions {
                BaseDir = expectedBaseDir
            };
            var expectedContentDto = new Models.Dto.Content {
                Path = expectedPath
            };
            var expectedFullPath = Path.Combine(expectedBaseDir, expectedPath);
            var expectedText     = "EXPECTED TEXT";
            var cacheKey1        = $"{nameof(ContentController)}_{expectedContentId}";
            var cacheKey2        = $"{nameof(ContentController)}_{expectedFullPath}";

            object junk;

            The <IMemoryCache>().Setup(m => m.TryGetValue(cacheKey1, out junk)).Returns(false);
            The <IMemoryCache>().Setup(m => m.TryGetValue(cacheKey2, out junk)).Returns(false);
            The <IMemoryCache>().Setup(m => m.CreateEntry(It.IsAny <string>())).Returns(The <ICacheEntry>().Object);

            The <IOptions <Models.ContentOptions> >().SetupGet(mbox => mbox.Value).Returns(expectedOptions);

            The <IContentRepository>()
            .Setup(m => m.GetAsync(expectedContentId, It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedContentDto);

            The <ITextRepository>()
            .Setup(m => m.ReadAsync(expectedFullPath, It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedText);

            // Act
            var response = await Target.GetContentTextById(expectedContentId);

            // Assert
            Assert.Equal(expectedText, response.Value);
            VerifyAll();
        }