组合匿名类型

时间:2018-09-07 21:33:31

标签: c#

我正在尝试在存储库中测试GetProblemsRepositoryAsync方法,我需要将两个匿名类型“ testListOpen”和“ testListClosed”作为我的测试列表传递到存储库层。 我想知道的是,将这两者传递到存储库层并在其中填充列表的最佳方法是什么?

存储库单元测试

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
        }
    }
}

存储库

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};

        }
    }
}

0 个答案:

没有答案