コード例 #1
0
        public async void GetExampleById_Calls_Correct_Url()
        {
            // ARRANGE
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(new Example())),
            })
            .Verifiable();

            var loggerMock = new Mock <ILogger>();

            var httpClient = new HttpClient(handlerMock.Object);

            IOptions <EnvironmentConfiguration> environmentConfiguration = Options.Create <EnvironmentConfiguration>(new EnvironmentConfiguration());
            var configuration = new MapperConfiguration(cfg => { cfg.AddProfile <ExampleProfile>(); });
            var mapper        = new Mapper(configuration);


            var tenant    = "tenant";
            var contentId = "1";

            var exampleServiceClient = new ExampleServiceClient(
                loggerMock.Object,
                mapper,
                environmentConfiguration,
                httpClient);
            var exampleId = 1;

            // ACT
            await exampleServiceClient.GetExampleById(exampleId);

            // ASSERT

            var expectedUri = new Uri($"http://localhost/api/v1/examples/{exampleId}");

            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Get &&
                                               req.RequestUri == expectedUri // to this uri
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }
        public async void GetExampleById_CallsCorrectUrl()
        {
            // ARRANGE
            _handlerMock
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(new Example())),
            })
            .Verifiable();

            _httpClientFactoryMock.Setup(x => x.CreateClient(string.Empty)).Returns(new HttpClient(_handlerMock.Object));

            var environmentConfiguration = Options.Create(new EnvironmentConfiguration()
            {
                SERVICE_URL = "https://localhost"
            });
            var configuration = new MapperConfiguration(cfg => { cfg.AddProfile <ExampleProfile>(); });
            var mapper        = new Mapper(configuration);

            var exampleServiceClient = new ExampleServiceClient(
                _loggerMock.Object,
                mapper,
                environmentConfiguration,
                _httpClientFactoryMock.Object);
            var exampleId = 1;

            // ACT
            await exampleServiceClient.GetExampleById(exampleId);

            // ASSERT
            var expectedUri = new Uri($"https://localhost/api/v1/examples/{exampleId}");

            _handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Get &&
                                               req.RequestUri == expectedUri
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }
        public async void GetExampleById_ThrowsException_WhenRequestIsNotSuccessful()
        {
            // ARRANGE
            _handlerMock
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode     = HttpStatusCode.InternalServerError,
                Content        = new StringContent("Big bad thing happened."),
                RequestMessage = new HttpRequestMessage()
            })
            .Verifiable();

            _httpClientFactoryMock.Setup(x => x.CreateClient(string.Empty)).Returns(new HttpClient(_handlerMock.Object));

            var environmentConfiguration = Options.Create(new EnvironmentConfiguration()
            {
                SERVICE_URL = "https://localhost"
            });
            var configuration = new MapperConfiguration(cfg => { cfg.AddProfile <ExampleProfile>(); });
            var mapper        = new Mapper(configuration);

            var exampleServiceClient = new ExampleServiceClient(
                _loggerMock.Object,
                mapper,
                environmentConfiguration,
                _httpClientFactoryMock.Object);
            var exampleId = 1;

            // ACT /  ASSERT
            await Assert.ThrowsAsync <Exception>(async() => await exampleServiceClient.GetExampleById(exampleId));
        }