如何使用 rhino 模拟存根 MVC RC1 中的 HttpSessionState?

发布于 2024-07-14 16:29:53 字数 506 浏览 11 评论 0原文

我正在尝试利用 最近在 asp.net mvc rc1 中进行 ControllerContext 重构。 我应该能够相当简单地对会话进行存根,但在运行以下代码时,我在第 2 行不断收到 System.NullReferenceException:

var mockContext = MockRepository.GenerateStub<ControllerContext>();
mockContext.Stub(x => x.HttpContext.Session["MyKey"]).Return("MyValue");

我做错了什么?

编辑:我刚刚验证了截至本文为止我拥有最新版本的犀牛。

I'm trying to take advantage of the recent ControllerContext refactoring in asp.net mvc rc1. I should be able to stub the session rather simply but I keep getting a System.NullReferenceException on line 2 when running the following code:

var mockContext = MockRepository.GenerateStub<ControllerContext>();
mockContext.Stub(x => x.HttpContext.Session["MyKey"]).Return("MyValue");

What am I doing wrong?

Edit: I just verified I have the latest version of rhino as of this post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深者入戏 2024-07-21 16:29:53

您还需要模拟 HttpContext 才能使其正常工作。
我为此使用了 HttpContext 的模拟:

public class HttpContextMock
{
    private readonly HttpContextBase _contextBase;
    private readonly HttpRequestBase _requestBase;
    private readonly HttpResponseBase _responseBase;
    private readonly HttpSessionStateBase _sessionStateBase;
    private readonly HttpServerUtilityBase _serverUtilityBase;

    public HttpContextBase Context { get { return _contextBase; } }
    public HttpRequestBase Request { get { return _requestBase; } }
    public HttpResponseBase Response { get { return _responseBase; } }
    public HttpSessionStateBase Session { get { return _sessionStateBase; } }
    public HttpServerUtilityBase Server { get { return _serverUtilityBase; } }


    public HttpContextMock()
    {
        _contextBase = MockRepository.GenerateStub<HttpContextBase>();
        _requestBase = MockRepository.GenerateStub<HttpRequestBase>();
        _responseBase = MockRepository.GenerateStub<HttpResponseBase>();
        _sessionStateBase = MockRepository.GenerateStub<HttpSessionStateBase>();
        _serverUtilityBase = MockRepository.GenerateStub<HttpServerUtilityBase>();

        _contextBase.Stub(x => x.Request).Return(_requestBase);
        _contextBase.Stub(x => x.Response).Return(_responseBase);
        _contextBase.Stub(x => x.Session).Return(_sessionStateBase);
        _contextBase.Stub(x => x.Server).Return(_serverUtilityBase);

        _requestBase.Stub(x => x.IsAuthenticated).Return(true);

        _contextBase.User = new GenericPrincipal(new GenericIdentity("[email protected]"),
                                                 new string[] {"Admin"});

    }
}

在测试的设置中,我创建了一个控制器的实例:

    [SetUp]
    public override void TestSetUp()
    {
        base.TestSetUp();

        repository = MockRepository.GenerateMock<IFooRepository>();
        controller = new FooController()
                         {
                             FooRepository = repository,
                             UserRepository = userMockRepository
                         };

        controller.ControllerContext = new ControllerContext(context.Context, new RouteData(), controller);
    }

一切工作正常,我可以向会话和许多其他内容添加参数。
希望这可以帮助。

You need to mock HttpContext too to make this working.
I'm using a mock of HttpContext for this:

public class HttpContextMock
{
    private readonly HttpContextBase _contextBase;
    private readonly HttpRequestBase _requestBase;
    private readonly HttpResponseBase _responseBase;
    private readonly HttpSessionStateBase _sessionStateBase;
    private readonly HttpServerUtilityBase _serverUtilityBase;

    public HttpContextBase Context { get { return _contextBase; } }
    public HttpRequestBase Request { get { return _requestBase; } }
    public HttpResponseBase Response { get { return _responseBase; } }
    public HttpSessionStateBase Session { get { return _sessionStateBase; } }
    public HttpServerUtilityBase Server { get { return _serverUtilityBase; } }


    public HttpContextMock()
    {
        _contextBase = MockRepository.GenerateStub<HttpContextBase>();
        _requestBase = MockRepository.GenerateStub<HttpRequestBase>();
        _responseBase = MockRepository.GenerateStub<HttpResponseBase>();
        _sessionStateBase = MockRepository.GenerateStub<HttpSessionStateBase>();
        _serverUtilityBase = MockRepository.GenerateStub<HttpServerUtilityBase>();

        _contextBase.Stub(x => x.Request).Return(_requestBase);
        _contextBase.Stub(x => x.Response).Return(_responseBase);
        _contextBase.Stub(x => x.Session).Return(_sessionStateBase);
        _contextBase.Stub(x => x.Server).Return(_serverUtilityBase);

        _requestBase.Stub(x => x.IsAuthenticated).Return(true);

        _contextBase.User = new GenericPrincipal(new GenericIdentity("[email protected]"),
                                                 new string[] {"Admin"});

    }
}

And in SetUp of test I create an instance of a controller:

    [SetUp]
    public override void TestSetUp()
    {
        base.TestSetUp();

        repository = MockRepository.GenerateMock<IFooRepository>();
        controller = new FooController()
                         {
                             FooRepository = repository,
                             UserRepository = userMockRepository
                         };

        controller.ControllerContext = new ControllerContext(context.Context, new RouteData(), controller);
    }

And all is working fine, I can add parameters to session and many other things.
Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文