示例#1
0
        public async Task Test1()
        {
            Mock <ISleeperApiClient> mockClient = new Mock <ISleeperApiClient>();

            var factory = new TestWebApplicationFactory <Startup>(services =>
            {
                // setup the swaps
                services.SwapTransient <ISleeperApiClient>(provider => mockClient.Object);
            });

            mockClient.Setup(x => x.GetUserAsync(It.IsAny <string>())).ReturnsAsync(new SleeperUser {
                Name = "My Moq User"
            });

            // Create an HttpClient which is setup for the test host
            var client = factory.CreateClient();

            // Make a call to the base route
            HttpResponseMessage response = await client.GetAsync($"/api/sleeper/anyvalue");

            // Assert we got the successful response
            Assert.That(response.IsSuccessStatusCode, Is.True);

            // Assert the content is as expected
            string responseContent = await response.Content.ReadAsStringAsync();

            SleeperUser sleeperUser = JsonSerializer.Deserialize <SleeperUser>(responseContent);

            Assert.That(sleeperUser.Name, Is.EqualTo("My Moq User"));

            mockClient.Verify(x => x.GetUserAsync(It.IsAny <string>()), Times.Once);
        }
        public async Task Test2()
        {
            var factory = new TestWebApplicationFactory <Startup>(services =>
            {
                // setup the swaps
                services.SwapTransient <ISleeperApiClient, MockSleeperApiClient>();
            });

            // Create an HttpClient which is setup for the test host
            var client = factory.CreateClient();

            // Make a call to the base route
            HttpResponseMessage response = await client.GetAsync("/api/sleeper/anyvalue");

            // Assert we got the successful response
            Assert.That(response.IsSuccessStatusCode, Is.True);

            // Assert the content is as expected
            string responseContent = await response.Content.ReadAsStringAsync();

            SleeperUser sleeperUser = JsonSerializer.Deserialize <SleeperUser>(responseContent);

            Assert.That(sleeperUser.Name, Is.EqualTo("Test User"));
        }