I have to write automated test scripts for an API by sending a request and validating the response and I don't know where to begin. The API Client is pretty simple and apparently from the developers all I need from the code is this which is the APIClient:
using System.Collections.Generic; using System.Threading.Tasks; using System; using API.Models; namespace AutomatedAPI { public interface ApiClient { Task AuthenticateAsync(string clientId, string clientSecret); void SetBearerToken(string token); Task<ApiResponse<PagedItemResult>> SearchIemsAsync(int? page = 1, Guid? shopId = null); } } This is all housed in VS, so I have created my own API test class and tried the following:
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using API; namespace API.Automation { [TestClass] public class SearchItems { private Uri apiBaseUri => new Uri("https://qa-shop-items.azurewebsites.net"); private Uri identityBaseUri => new Uri("https://qa-shop-store"); [TestMethod] public async Task TestMethod() { var itemId = new Guid("fsdf78dsff-fsdgfg89g-fsgvssfdg89"); var client = new ThirdPartyApiClient(apiBaseUri); client.SetBearerToken("Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkI4QzE2QUNEOEYxODR"); ApiResponse<Item> result = await client.GetItemAsync(itemId); } } } Now no errors in the syntax but when I run my script I get the following message:
Message: Test method API.Automation.SearchItemss.TestMethod threw exception: System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. Stack Trace: ApiClient.deserializeTokenPart[T](String tokenPart) ApiClient.SetBearerToken(String token) line 98 <TestMethod>d__4.MoveNext() line 20 --- End of stack trace from previous location where exception was thrown --- TaskAwaiter.ThrowForNonSuccess(Task task) TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) ThreadOperations.ExecuteWithAbortSafety(Action action) As I have not used Task before can someone tell me how to implement this SearchItems method? Or at least give me a template of how this is done?
asyncan useawaitbeforeclient.GetFleetAsync(itemId);, like in this thread How does one test async code using MSTest