[TestMethod]
public void Uri_ValidFormatTest()
{
var requestMock = new Mock<IRequestClient>();
var client = new RestClient(requestMock.Object);
string address = "https://www.google.co.nz";
string postData = "method=payment¶ms[Key1]=Value1¶ms[Key2]=Value2¶ms[Key3]=Value3";
requestMock.Setup(x => x.UploadString(new Uri(address), postData)).Returns("Success");
Assert.AreEqual("Success", client.MakeRequest(address, postData));
}
[Test]
public void GetAllProductsTest()
{
var productController = new ProductController(_productService)
{
Request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(ServiceBaseURL + "v1/Products/Product/all")
}
};
productController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
_response = productController.Get();
var responseResult = JsonConvert.DeserializeObject<List<Product>>(_response.Content.ReadAsStringAsync().Result);
Assert.AreEqual(_response.StatusCode, HttpStatusCode.OK);
Assert.AreEqual(responseResult.Any(), true);
var comparer = new ProductComparer();
CollectionAssert.AreEqual(
responseResult.OrderBy(product => product, comparer),
_products.OrderBy(product => product, comparer), comparer);
}
et me explain the code step by step. We start by creating an instance of ProductController and initialize the Request property of the controller with new request message stating calling http method as GET and initialize theRequestUri with base hosted service URL and appended with actual end point of the method. InitializingRequestUri is not necessary in this case but will help you if you test actual service end point. In this case, we are not testing actual endpoint but the direct controller method.
HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration() line adds defaulthttpconfiguration to HttpConfigurationKey necessary for controller instance instantiation.
_response = productController.Get(); line calls the Get() method of controller that fetches all the products from dummy _products list. Since the return type of the method was an http response message, we need to parse it to get the JSON result sent from the method. All the transactions from API’s should ideally happen in the form of JSON or XML only. This helps the client to understand the response and its result set. We de-serialize the object we got from _response using NewtonSoft library into a list of products. That means the JSON response is converted to List<Product> for better accessibility and comparison. Once done with JSON to List object conversion, I have put three asserts to test the result.
Assert.AreEqual(_response.StatusCode, HttpStatusCode.OK); line checks the http status code of the response, the expected is HttpStatusCode.OK.
Second assert i.e. Assert.AreEqual(responseResult.Any(), true); checks that we have got the items in the list or not. Third assert is the actual confirmation assert for our test that compares each product from the actual product list to the returned product list.

No comments:
Post a Comment