public void ShouldReturnApiResponseModelForRandomNumberMathFact()
        {
            // Arrange
            APIResponseModel apiResponseModel = new APIResponseModel();

            apiResponseModel.Number = 2030;
            apiResponseModel.Type   = "math";
            apiResponseModel.Text   = "some text";
            apiResponseModel.Found  = true;

            Mock <IRestClient> mockIRestClient = new Mock <IRestClient>();

            mockIRestClient.Setup(x => x.Execute <APIResponseModel>(It.IsAny <IRestRequest>()))
            .Returns(new RestResponse <APIResponseModel>
            {
                StatusCode = HttpStatusCode.OK,
                Data       = apiResponseModel,
            });

            NumfactsAPIClient numfactsAPIClient = new NumfactsAPIClient(mockIRestClient.Object);

            // Act
            APIResponseModel response = numfactsAPIClient.GetNumFact();

            // Assert
            Assert.AreEqual(2030, response.Number);
            Assert.AreEqual("math", response.Type);
            Assert.AreEqual("some text", response.Text);
            Assert.IsTrue(response.Found);
        }
Пример #2
0
        public static void Main(string[] args)
        {
            try
            {
                // Make sure the arguments are properly split apart before creating the argument model.
                string[] splitArgs = ArgumentsHandler.BreakApartArguments(args);

                // Create arguments model from passed in arguments.
                ArgumentsModel argumentsModel = ArgumentsHandler.CreateArgumentsModelFromUserInput(splitArgs);

                // Validate the arguments model before making the API request.
                ArgumentsHandler.ValidateArgumentsModel(argumentsModel);

                // Notify the user that we're about to make an API request.
                Console.WriteLine(OutputConstants.API_LOADING);

                // Make the API request.
                NumfactsAPIClient numfactsAPIClient = new NumfactsAPIClient(
                    new RestClient(
                        NumfactsAPIClient.BuildAPIEndpointUrl(argumentsModel)
                        )
                    );

                APIResponseModel apiResponseModel = numfactsAPIClient.GetNumFact();

                // Display the results to the user.
                if (apiResponseModel.Found)
                {
                    Console.WriteLine(OutputConstants.FACT_DISPLAY_TEMPLATE, MathOrTriviaFactPrefix(apiResponseModel), apiResponseModel.Number, apiResponseModel.Text);
                }
                else
                {
                    throw new Exception(ErrorConstants.NO_FACT_FOUND + apiResponseModel.Number);
                }
            } catch (Exception ex)
            {
                // Display the error to the user.
                Console.WriteLine(ex.Message);
            }
        }
        public void ShouldThrowExceptionOnAPIError()
        {
            // Arrange
            APIResponseModel apiResponseModel = new APIResponseModel();

            apiResponseModel.Number = 2030;
            apiResponseModel.Type   = "math";
            apiResponseModel.Text   = "some text";
            apiResponseModel.Found  = true;

            Mock <IRestClient> mockIRestClient = new Mock <IRestClient>();

            mockIRestClient.Setup(x => x.Execute <APIResponseModel>(It.IsAny <IRestRequest>()))
            .Returns(new RestResponse <APIResponseModel>
            {
                StatusCode   = HttpStatusCode.InternalServerError,
                ErrorMessage = "Internal Server Error",
            });

            NumfactsAPIClient numfactsAPIClient = new NumfactsAPIClient(mockIRestClient.Object);

            // Act
            APIResponseModel response = numfactsAPIClient.GetNumFact();
        }