public async void ShouldReturnFactListWithMock()
        {
            var handlerMock = new Mock <HttpMessageHandler>();
            var httpClient  = this.GetMockHttpClient(handlerMock, this.factListText);

            using var animalFactsEndpoint = new AnimalFacts(httpClient);

            var facts = await animalFactsEndpoint.GetRandomFactsAsync(amount : 2);

            var factList = facts.ToList();

            Assert.Equal(2, factList.Count);

            for (int i = 0; i < 2; i++)
            {
                var fact = factList[i];
                Assert.NotNull(fact);
                Assert.Equal($"factId{i + 1}", fact.Id);
                Assert.Equal($"userId{i + 1}", fact.UserId);
                Assert.Equal($"Test Text: {i + 1}.", fact.Text);
                Assert.Equal(i + 1, fact.Version);
                Assert.False(fact.Used);
                Assert.False(fact.IsDeleted);
            }

            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get),
                ItExpr.IsAny <CancellationToken>());
        }
        public async void ShouldReturnSingleFactLive(string animal)
        {
            using var animalFactsEndpoint = new AnimalFacts();

            var fact = await animalFactsEndpoint.GetRandomFactAsync(animal : animal);

            Assert.NotNull(fact);
            Assert.NotNull(fact.Id);
            Assert.NotNull(fact.UserId);
            Assert.NotNull(fact.Text);
            Assert.Equal(animal, fact.Type);
        }
Beispiel #3
0
        public static async Task WriteFacts(FactSubject subject, int repeatSeconds, ConsoleColor consoleColor)
        {
            AnimalFacts facts = new AnimalFacts(HttpClient);

            while (true)
            {
                var fact = await facts.GetFact(new AnimalFactQuery(subject));

                Console.ForegroundColor = consoleColor;
                Console.WriteLine(fact.ToString(), consoleColor);
                await Task.Delay(TimeSpan.FromSeconds(repeatSeconds));
            }
        }
Beispiel #4
0
        public static async Task WriteFacts()
        {
            AnimalFacts    facts  = new AnimalFacts(HttpClient);
            TextFileWriter writer = new TextFileWriter(WRITEFILE);

            while (true)
            {
                var fact = await facts.GetFact(new AnimalFactQuery(FactSubject.Cat));

                bool successfullyWritten = await writer.CreateOrAppend(fact.ToString());

                Console.WriteLine($"Fact {fact.Id} written to file. Successful: {successfullyWritten}");
                await Task.Delay(TimeSpan.FromSeconds(10));
            }
        }
        public async void ShouldReturnFactListLive(string animal, int amount)
        {
            using var animalFactsEndpoint = new AnimalFacts();

            var facts = await animalFactsEndpoint.GetRandomFactsAsync(animal : animal, amount : amount);

            var factList = facts.ToList();

            Assert.Equal(amount, factList.Count);

            for (int i = 0; i < amount; i++)
            {
                var fact = factList[i];
                Assert.NotNull(fact);
                Assert.NotNull(fact.Id);
                Assert.NotNull(fact.UserId);
                Assert.NotNull(fact.Text);
                Assert.Equal(animal, fact.Type);
            }
        }
        public async void ShouldReturnSingleFactWithMock()
        {
            var handlerMock = new Mock <HttpMessageHandler>();
            var httpClient  = this.GetMockHttpClient(handlerMock, this.factText);

            using var animalFactsEndpoint = new AnimalFacts(httpClient);

            var fact = await animalFactsEndpoint.GetRandomFactAsync();

            Assert.NotNull(fact);
            Assert.Equal("factId", fact.Id);
            Assert.Equal("userId", fact.UserId);
            Assert.Equal("Test Text.", fact.Text);
            Assert.Equal(1, fact.Version);
            Assert.False(fact.Used);
            Assert.False(fact.IsDeleted);

            handlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get),
                ItExpr.IsAny <CancellationToken>());
        }