示例#1
0
        public void GetRandomJokes_BadRequest_ReturnsEmptyList()
        {
            //Arrange
            var    name     = new List <Tuple <string, string> >();
            string category = null;
            int    numJokes = 1;
            int    expected = 0;

            var handlerMock = new Mock <HttpMessageHandler>();

            handlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage {
                StatusCode = HttpStatusCode.BadRequest,
            });

            var client = new HttpClient(handlerMock.Object);

            _sut = new JokeGenerator.JokeGenerator(client);


            //Act
            var actual = _sut.GetRandomJokes(name, category, numJokes);

            //Assert
            Assert.AreEqual(expected, actual.Count);
        }
示例#2
0
        public void GetRandomJokes_CallMethod_ReturnsJoke()
        {
            //Arrange
            var    name     = new List <Tuple <string, string> >();
            string category = null;
            int    numJokes = 1;

            var handlerMock = new Mock <HttpMessageHandler>();

            handlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("{\"categories\":[],\"created_at\":\"2020-01-05 13:42:26.447675\"," +
                                               "\"icon_url\":\"https://assets.chucknorris.host/img/avatar/chuck-norris.png\",\"id\":\"fivi0z5lt8gp_6vt3cc8pw\"," +
                                               "\"updated_at\":\"2020-01-05 13:42:26.447675\",\"url\":\"https://api.chucknorris.io/jokes/fivi0z5lt8gp_6vt3cc8pw\"," +
                                               "\"value\":\"If Chuck Norris writes code with bugs, the bugs fix themselves.\"}")
            });

            var client = new HttpClient(handlerMock.Object);

            _sut = new JokeGenerator.JokeGenerator(client);


            //Act
            var actual = _sut.GetRandomJokes(name, category, numJokes);

            //Assert
            Assert.AreEqual(numJokes, actual.Count);
        }
示例#3
0
        public void GetCategories_CallMethod_ReturnsAtLeastOneCategory()
        {
            //Arrange
            var handlerMock = new Mock <HttpMessageHandler>();

            handlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("[\"animal\",\"career\",\"celebrity\",\"dev\"," +
                                               "\"explicit\",\"fashion\",\"food\",\"history\",\"money\",\"movie\",\"music\"," +
                                               "\"political\",\"religion\",\"science\",\"sport\",\"travel\"]")
            });

            var client = new HttpClient(handlerMock.Object);

            _sut = new JokeGenerator.JokeGenerator(client);


            //Act
            var actual = _sut.GetCategories();

            //Assert
            Assert.Greater(actual.Count, 0);
        }
示例#4
0
        /// <summary>
        /// Calls API for the list of categories, and sets the information into the results variable.
        /// </summary>
        private static List <string> GetCategories()
        {
            var jokeGen = new JokeGenerator(new HttpClient());

            catList = jokeGen.GetCategories();
            return(catList);
        }
示例#5
0
        /// <summary>
        /// Main function of the program
        /// </summary>
        static void Main()
        {
            try
            {
                JokeGenerator generator = new JokeGenerator();

                while (generator.state != JokeGenerator.State.Cancel)
                {
                    switch (generator.state)
                    {
                    case JokeGenerator.State.Main:
                        generator.DisplayMainInformation();
                        generator.GetNextState();
                        break;

                    case JokeGenerator.State.Name:
                        generator.GetName();
                        generator.ResetState();
                        break;

                    case JokeGenerator.State.RandomName:
                        generator.GetRandomName();
                        generator.ResetState();
                        break;

                    case JokeGenerator.State.Category:
                        generator.GetCategory();
                        generator.ResetState();
                        break;

                    case JokeGenerator.State.Number:
                        generator.GetNumber();
                        generator.ResetState();
                        break;

                    case JokeGenerator.State.Joke:
                        generator.DisplayJokes();
                        generator.ResetState();
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("There was an unexpected error:");
                Console.WriteLine(e.GetType());
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
示例#6
0
        public void GetCategories_BadRequest_ReturnsEmptyList()
        {
            //Arrange
            var handlerMock = new Mock <HttpMessageHandler>();

            handlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage {
                StatusCode = HttpStatusCode.NotFound,
            });

            var client = new HttpClient(handlerMock.Object);

            _sut = new JokeGenerator.JokeGenerator(client);


            //Act
            var actual = _sut.GetCategories();

            //Assert
            Assert.AreEqual(0, actual.Count);
        }
示例#7
0
        /// <summary>
        /// Calls API for a list of Random jokes
        /// </summary>
        /// <param name="category">An optional category of joke.</param>
        /// <param name="nameList">An optional list of names to be used in the jokes.</param>
        /// <param name="number">The number of jokes to get.</param>
        private static List <string> GetRandomJokes(string category, List <Tuple <string, string> > nameList, int number)
        {
            var jokeGen = new JokeGenerator(new HttpClient());

            return(jokeGen.GetRandomJokes(nameList, category, number));
        }