Orchard Core 中运行带程序上下文的单元测试

Orchard Core 中运行带程序上下文的单元测试

 

 Orchard Core 带有很多单元测试,使用 Xunit 单元测试框架,除了简单的直接调用待测试的方法,有一些复杂的测试是需要上下文的,甚至需要 Application 程序启动起来,Orchard Core 的例子中有一个基于 HTTP 的 Application 测试,但是其测试都是通过调用 HTTP API 执行的,测试 Controller 挺方便,但是测试 Service 等就麻烦了,而且测试往往是需要调用内部的一些方法的,所以 HTTP API 测试适用范围有限。

 

`WebApplicationFactory` can only test by http method, could not direct call Application’s component, like do some Session.Query() then direct call some method to do a complex test.

 

所以自己做了个能够启动 Application 且在 Application 上下文内执行测试的单元测试基类和辅助方法。使用方便,继承即可使用,然后你就可以像在 Orchard Core 内部写代码一样,去调用各种 Service、Query 进行测试啦。

 

由于是从我给 Orchard Core 团队提的 issue 里面整理拷贝而来,中英文混合,将就着看,主要把我的实现代码分享,方便有需要的人。

 

 public class AppTestBase<TFixture> : IClassFixture<TFixture> where TFixture : class, IApplicationStartupFixture 
{
public readonly TFixture Application; public AppTestBase(TFixture application) { this.Application = application; } protected T GetService<T>() where T : class { return this.Application.ServiceProvider.GetService<T>(); } protected T GetRequiredService<T>() where T : class { return this.Application.ServiceProvider.GetRequiredService<T>(); } protected async Task RunInShellScopeAsync(Func<ShellScope, Task> execute) { var shellContextFactory = GetRequiredService<IShellContextFactory>(); IShellHost shellHost = GetRequiredService<IShellHost>(); using (var shell = await shellContextFactory.CreateDefaultShellContext()) using (var shellScope = shell.CreateScope()) { var httpContextAccessor = shellScope.ServiceProvider.GetService<IHttpContextAccessor>(); httpContextAccessor.HttpContext = shellScope.ShellContext.CreateHttpContext(); await shellScope.UsingAsync(execute); } } protected T CreateController<T>(ShellScope shellScope, HttpContext httpContext) { var controllerActivatorProvider = shellScope.ServiceProvider.GetService<IControllerActivatorProvider>(); var controllerContext = new ControllerContext() { HttpContext = httpContext, }; var controllerObj = (T) controllerActivatorProvider.CreateActivator( new ControllerActionDescriptor() {ControllerTypeInfo = (TypeInfo) typeof(T),}) .Invoke(controllerContext); return controllerObj; } }

 

add database setting in ShellSettings (required):

    settings["DatabaseProvider"] ="SqlConnection";
    settings["ConnectionString"] = "Server=localhost;Database=xxx;User Id=sa;Password=xxxxxxx";
    settings["TablePrefix"] = "xxx";

 

    
public static class TestShellContextFactoryExtensions
{
    internal static Task<ShellContext> CreateDefaultShellContext(this IShellContextFactory shellContextFactory)
    {
            var settings = new ShellSettings()
            {
                Name = ShellHelper.DefaultShellName,
                State = TenantState.Running
            };
            
        settings["DatabaseProvider"] ="SqlConnection";
        settings["ConnectionString"] = "Server=localhost;Database=xxx;User Id=sa;Password=xxxxxxx";
        settings["TablePrefix"] = "xxx";
    
            return shellContextFactory.CreateDescribedContextAsync(settings, new ShellDescriptor()
            {
                Features = new List<ShellFeature>()
                {
                    new ShellFeature("Application.Default"),
                    new ShellFeature("OrchardCore.Setup"),
                    new ShellFeature("Operational"),
                },
                Parameters = new List<ShellParameter>(),
            });
            // return shellContextFactory.CreateShellContextAsync(settings);
    }

}

 

 

 

An helper to create `HttpContext`, and set set `shell.RequestServices` to `HttpContext`.

 

    public static HttpContext CreateHttpContext(this ShellContext shell)
    {
        var settings = shell.Settings;

        var context = new DefaultHttpContext();
         
                // set shell.RequestServices to context
        context.RequestServices = shell.ServiceProvider;
        OrchardCore.Modules.HttpContextExtensions.UseShellScopeServices(context);

        var urlHost = settings.RequestUrlHost?.Split('/',
            StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();

        context.Request.Host = new HostString(urlHost ?? "localhost");

        if (!String.IsNullOrWhiteSpace(settings.RequestUrlPrefix))
        {
            context.Request.PathBase = "/" + settings.RequestUrlPrefix;
        }

        context.Request.Path = "/";
        context.Items["IsBackground"] = true;

        context.Features.Set(new ShellContextFeature
        {
            ShellContext = shell,
            OriginalPathBase = String.Empty,
            OriginalPath = "/"
        });

        return context;
    }


 

使用的例子(先继承基类):

    [Fact]
    public async Task InAppRuntimeTest() {
        await RunInShellScopeAsync(async shellScope =>
        {
            var session = shellScope.ServiceProvider.GetService<ISession>();
            Assert.NotNull(session);

            var controllerObj =
                    CreateController<XxxxxController>(shellScope, shellScope.ShellContext.CreateHttpContext());

            var result = await controllerObj.Index(new XxxxModel(){});

            Assert.NotNull(result);
        });
    }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119608.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 深入浅出ES6(四):模板字符串

    在上一篇文章中,我说过要写一篇风格迥异的新文章,在了解了迭代器和生成器后,是时候来品味一些不烧脑的简单知识,如果你们觉得太难了,还不快去啃犀牛书!现在,就让我们从最简单的知识学起吧!反撇号(`)基础知识ES6引入了一种新型的字符串字面量语法,我们称之为模板字符串(templatestrings)。除了使用反撇号字符`代替普通字符串的引号’或”外,它们看起来与普通

    2022年4月5日
    125
  • win10键盘锁住了怎么解决

    win10键盘锁住了怎么解决有win10系统用户在使用的时候,发现键盘被锁住了,导致无法使用,经过分析可能是不小心按到了键盘上的锁住键锁定键盘的快捷键笔记本电脑:Fn+Numlock键第一种方法:1、外接键盘,是否按过“Numlock”键,如果按过“Numlock”键,再次使用外接键盘按一次“Numlock”键,就会恢复正常了。(仅仅锁定或者解锁数字键盘)2、可以在电脑键盘中找到“Fn”键,这个键又被称为“第二功能键”,因为当你按下这个键,再去按另一个带有两种功能的键时,那个键就会出现第二种功能。3、再找到“F.

    2022年5月29日
    61
  • 职称计算机一个模块1500,职称计算机考试模块如何选择

    职称计算机一个模块1500,职称计算机考试模块如何选择职称计算机考试模块如何选择选择模块的原则:1、熟悉度:选择自己最常用切最熟悉的模块进行学习考试,像WindowsXP、Word2003、Excel2003、PPT2003、Internet应用、网页制作等都是大家较为熟悉的模块。2、相关性:有些模块之间的相关性很近,比如:学了Word之后再去学习PPT及Frontpage就显得非常容易。3、常用性:像Windows98、Word98、E…

    2022年6月2日
    37
  • Qt小软件:LanFileSender(局域网文件传输小工具)

    Qt小软件:LanFileSender(局域网文件传输小工具)最近写了一个局域网文件传输小工具,自己用了一段时间了,觉得也稳定了,发出来分享一下。开启后会自动搜索局域网内的主机,然后直接发送文件就可以了。可以设置昵称和存储目录(默认在下载文件夹下)截图:功能详解:File:添加文件Director:添加文件夹Clear:清空文件列表Send:发送文件Ping:发送ping(不是CMD的那个p

    2022年6月3日
    29
  • 计算机高配表要表格,为何高配电脑还会卡? 因为你没选择FreeSync套装

    计算机高配表要表格,为何高配电脑还会卡? 因为你没选择FreeSync套装可能有很多玩家在网络对战游戏中都遇到如此状况:电脑配置并不低,但游戏画面依然不够顺滑,不但经常卡顿,而且明明先瞄准敌人开枪,敌人没死而自己被秒掉。其实,这并不是因为玩家枪法太菜,问题在很大程度上出在玩家选择的显卡与显示器上。那到底玩家的显卡和显示器上到底有什么问题?让我们为大家分析一下吧。高配电脑可以提供高帧速,但并不一定无卡顿高配置的电脑当然能提供强劲的性能,在游戏中自然可以提供很高的帧速。但为…

    2022年6月1日
    36
  • expandablelistview详解[通俗易懂]

    expandablelistview详解[通俗易懂]我在项目中使用到expandablelistview,然后我就在网上找了很多关于expandablelistview的文章,那么这里,将一些对去进行总结一些,并将自己踩过的坑填上。expandablelistview就是类似QQ分组,点击分类,显示其各个详细的分类信息。下面是一些效果图这样是完成了有父标题,和子标题,实现了分组,接下来看看如何布局的。

    2022年6月18日
    29

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号