简体   繁体   English

如何编写使用HttpContext的服务器端单元测试?

[英]How do I write a server side unit test that uses a HttpContext?

I am new to writing web applications. 我是写Web应用程序的新手。

I want to test code that creates a collection 我想测试创建集合的代码

Here is the unit test so far. 这是到目前为止的单元测试。

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var accessor = new HttpContextAccessor {HttpContext = new DefaultHttpContext()};
        var helper = new NodeHelper(accessor);
        var nodes = helper.GetNodes();
        Assert.IsTrue(nodes.Count > 0);
        // var nodes = NodeHelper
    }
}

It fails with the error 它因错误而失败

System.InvalidOperationException: Session has not been configured for this application or request. System.InvalidOperationException:尚未为此应用程序或请求配置会话。 at Microsoft.AspNetCore.Http.DefaultHttpContext.get_Session() 在Microsoft.AspNetCore.Http.DefaultHttpContext.get_Session()

Using examples from the DefaultHttpContextTests.cs on Github, it appears you would need to setup some helper classes so that the HttpContext has a usable Session for the test. 使用Github上DefaultHttpContextTests.cs中的示例,您似乎需要设置一些帮助程序类,以便HttpContext具有可用于测试的Session。

private class TestSession : ISession
{
    private Dictionary<string, byte[]> _store
        = new Dictionary<string, byte[]>(StringComparer.OrdinalIgnoreCase);

    public string Id { get; set; }

    public bool IsAvailable { get; } = true;

    public IEnumerable<string> Keys { get { return _store.Keys; } }

    public void Clear()
    {
        _store.Clear();
    }

    public Task CommitAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult(0);
    }

    public Task LoadAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult(0);
    }

    public void Remove(string key)
    {
        _store.Remove(key);
    }

    public void Set(string key, byte[] value)
    {
        _store[key] = value;
    }

    public bool TryGetValue(string key, out byte[] value)
    {
        return _store.TryGetValue(key, out value);
    }
}

private class BlahSessionFeature : ISessionFeature
{
    public ISession Session { get; set; }
}

You could have also mocked the context, session and other dependencies but this way required less setup than having to configure a lot of mocks. 您可能还模拟了上下文,会话和其他依赖关系,但是这种方式所需的设置要比必须配置大量的模拟要少。

With that the test can be arranged accordingly 这样可以相应地安排测试

[TestClass]
public class NodeHelperTests{
    [TestMethod]
    public void Should_GetNodes_With_Count_GreaterThanZero() {
        //Arrange
        var context = new DefaultHttpContext();
        var session = new TestSession();
        var feature = new BlahSessionFeature();
        feature.Session = session;
        context.Features.Set<ISessionFeature>(feature);
        var accessor = new HttpContextAccessor { HttpContext = context };
        var helper = new NodeHelper(accessor);

        //Act
        var nodes = helper.GetNodes();

        //Assert
        Assert.IsTrue(nodes.Count > 0);            
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对依赖于HttpContext.GetGlobalResourceObject的类进行单元测试? - How do I unit test a class that relies on HttpContext.GetGlobalResourceObject? 如何为我的单元测试创​​建 HttpContext? - How do I create an HttpContext for my unit test? 如何对使用 DbContext 和 NSubstitute 的存储库进行单元测试? - How do I unit test a repository that uses DbContext with NSubstitute? 如何对使用Fluent界面的代码进行单元测试? - How do I unit test code that uses a Fluent interface? 如何对“HttpContext.Current.Request.LogonUserIdentity.Name”进行单元测试? - How do I unit test “HttpContext.Current.Request.LogonUserIdentity.Name”? 如何对依赖于 HttpContext.Current 和 Sitecore.Context 的类进行单元测试? - How do I unit test a class that depends on HttpContext.Current and Sitecore.Context? 单元测试HttpContext.Current.Cache或C#中的其他服务器端方法? - Unit test HttpContext.Current.Cache or other server-side methods in C#? 如何在ASP.NET Core 1.1中对使用HttpContext的MVC控制器进行单元测试 - How to unit test MVC controller that uses HttpContext in ASP.NET Core 1.1 如何为C#Azure功能编写单元测试? - How do I write Unit Test for C# Azure Function? 如何编写依赖于文件系统事件的单元测试? - How do I write a unit test that relies on file system events?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM