I'm trying to test the GetProblemsRepositoryAsync method in my repository and I need to pass in two anonymous types 'testListOpen' and 'testListClosed' as my test lists to the repository layer. What i'm wondering is, what is the best way to pass those two to the repository layer and populate the lists there?
Repository unit test
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TeamStats.API.Repositories;
using TeamStats.API.ServiceModel.Messages;
using TeamStats.API.ServiceModel.Types;
using Newtonsoft.Json;
using Moq;
using TeamStats.API.Interfaces;
using TeamStats.API.Interfaces.Repositories;
using System.Threading;
namespace TeamStats.API.Tests.UnitTests.Repositories
{
[TestClass]
public class ProblemsRepositoryTest
{
ProblemsRequest problemsRequest = new ProblemsRequest()
{
StartDate = new DateTime(2017, 03, 18).ToString(),
EndDate = new DateTime(2018, 03, 18).ToString(),
};
[TestMethod]
public void GetProblemsEndPoint()
{
var testListOpen = new
{
result = new
{
response = new
{
problem = new
{
openedrecords = new[]
{
new { Number = "RPRB222222", ShortDescription = "Automated Deployment: Navisphere"},
},
}
}
},
};
var testListClosed = new
{
result = new
{
response = new
{
problem = new
{
openedrecords = new[]
{
new { Number = "RPRB1111111", ShortDescription = "Automated Deployment: Navisphere"},
},
}
}
},
};
HttpResponseMessage message = new HttpResponseMessage();
message.StatusCode = System.Net.HttpStatusCode.OK;
message.Content = new StringContent(JsonConvert.SerializeObject(testListOpen), Encoding.UTF8, "application/json");
//arrange
Mock<IHttpClient> mc = new Mock<IHttpClient>();
mc.Setup(x => x.GetAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>())).Returns(Task.FromResult(message));
ProblemsRepository pr = new ProblemsRepository(mc.Object);
ProblemsRequest req = new ProblemsRequest();
//act
var actual = pr.GetProblemsRepositoryAsync(req).Result;
//assert
}
}
}
Repository
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using TeamStats.API.Interfaces;
using TeamStats.API.Interfaces.Repositories;
using TeamStats.API.ServiceModel.Messages;
using TeamStats.API.ServiceModel.Types;
namespace TeamStats.API.Repositories
{
public class ProblemsRepository : IProblemsRepository
{
private readonly string baseUrl = ConfigurationManager.AppSettings["myQBaseURL"];
private readonly string token = ConfigurationManager.AppSettings["myQToken"];
private readonly IHttpClient client;
public ProblemsRepository(IHttpClient http)
{
client = http;
}
public async Task<ProblemsLists> GetProblemsRepositoryAsync(ProblemsRequest request)
{
List<Problems> openProblems = new List<Problems>();
List<Problems> closedProblems = new List<Problems>();
string myQUrl = baseUrl + "/api/chrow/groupmetrics/getMetrics?";
myQUrl += "EndDate=" + request.EndDate + "%2000:00:00&";
myQUrl += "Responsible=Yes&";
myQUrl += "StartDate=" + request.StartDate + "%2000:00:00&";
myQUrl += "Group=" + request.TeamName;
var headers = new Dictionary<string, string>() { ["token"] = token };
var res = await client.GetAsync(myQUrl, headers);
res.EnsureSuccessStatusCode();
var responseMessage = res.Content.ReadAsByteArrayAsync();
var message = res.Content.ReadAsStringAsync();
JObject apiResponse = JObject.Parse(message.Result);
IList<JToken> problemOpenedJson = apiResponse["result"]["response"]["problem"]["openedrecords"].Children().ToList();
IList<JToken> problemClosedJson = apiResponse["result"]["response"]["problem"]["closedrecords"].Children().ToList();
foreach (JToken p in problemOpenedJson)
{
Problems problems = p.ToObject<Problems>();
openProblems.Add(problems);
}
foreach (JToken p in problemClosedJson)
{
Problems problems = p.ToObject<Problems>();
closedProblems.Add(problems);
}
return new ProblemsLists {problemsClosed = closedProblems, problemsOpen = openProblems};
}
}
}