前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用TestServer测试ASP.NET Core API

使用TestServer测试ASP.NET Core API

作者头像
喝茶去
发布2019-04-16 16:17:04
9100
发布2019-04-16 16:17:04
举报
文章被收录于专栏:知识累积

今儿给大家分享下,在ASP.NET Core下使用TestServer进行集成测试,这意味着你可以在没有IIS服务器或任何外部事物的情况下测试完整的Web应用程序。下面给出示例: 

代码语言:javascript
复制
 public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            Configuration = configuration;
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            builder.AddEnvironmentVariables();
            Configuration = builder.Build();

            AutoMapperConfig.RegisterMappings();
        }
        [HttpGet]
        [Route("HomeVideo")]
        [ProducesResponseType(typeof(VideoProHomeDataModel), (int)HttpStatusCode.OK)]
        public HttpResponseMessage HomeVideo(int pd)
        {
            if (pd == 0)
                return Error("参数 pd 不能为 0");
            var result = _videoService.HomeVideoList();

            var identityList = new List<string>();
            identityList.AddRange(result.LookBack.Select(x => x.VideoIdentity));
            identityList.AddRange(result.SpeciaList.Select(x => x.VideoIdentity));

            var allVideoPageView = HttpLinkVideoPlay.GetVidepPageViewList(pd, ProjectName, HttpDefaultValue.VideoModuleName, identityList).KeyList.ToDictionary(x => x.CounterKey);

            result.TopAdvList.ForEach(x =>
            {
                x.VideoImg = ReplaceVideoImg(x.VideoImg, VideoMaxImg);
            });

            result.LookBack.ForEach(x =>
            {
                x.VideoImg = ReplaceVideoImg(x.VideoImg, VideoMinImg);
                if (allVideoPageView.ContainsKey(x.VideoIdentity))
                {
                    x.Pageview = allVideoPageView[x.VideoIdentity].KeyCount;
                }
            });
            result.SpeciaList.ForEach(x =>
            {
                x.VideoImg = ReplaceVideoImg(x.VideoImg, VideoMinImg);
                if (allVideoPageView.ContainsKey(x.VideoIdentity))
                {
                    x.Pageview = allVideoPageView[x.VideoIdentity].KeyCount;
                }
            });
            return Success(result);
        }
  1. 添加一个新的Test项目
  2. 添加对Web项目的引用
  3. 添加NuGet包: Microsoft.AspNetCore.TestHost
  4. 创建一个测试
代码语言:javascript
复制
public class TestStartup : Startup
    {
        public TestStartup(IConfiguration configuration, IHostingEnvironment env) : base(configuration, env)
        {
        }

        public void ConfigureTestServices(IServiceCollection services)
        {
             // todo:为测试环境配置服务
        }
    }
代码语言:javascript
复制
[TestClass]
public class Tests
{
    [TestMethod]
    public async Task TestMethod1()
    {
        var webHostBuilder =
              new WebHostBuilder()
                    .UseEnvironment("Development")
                    .UseStartup<TestStartup>(); 

        using (var server = new TestServer(webHostBuilder))
        using (var client = server.CreateClient())
        {
            string result = await client.GetStringAsync("/api/pro/HomeVideo?pd=2");
             Assert.AreEqual(result.TopAdvList.Count > 0, true);
        }
    }
}

 以上就是ASP.NET Core集成测试的简单介绍。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-11-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档