MVC测试用讽刺框架(MOQ) [英] MVC Testing using a mocking framework (Moq)

查看:111
本文介绍了MVC测试用讽刺框架(MOQ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用起订量在测试我的ASP.NET MVC2应用程序来帮助。

I'm using Moq to help in testing my ASP.NET MVC2 application.

问题:ArgumentException的是由用户code未处理。无法获取StrongNameKeyPair公共密钥

Problem: ArgumentException was unhandled by user code. Unable to obtain public key for StrongNameKeyPair

这code已被改编自斯科特Hanselman的NerdDinner1。

This code has been adapted from Scott Hanselman's NerdDinner1.

HomeController CreateHomeControllerAs(string userName)
    {
        var mock = new Mock<ControllerContext>();
        mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(userName); // fails here
        mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

        var controller = new HomeController();
        controller.ControllerContext = mock.Object;

        return controller;
    }

    [TestMethod]
    public void should_be_able_to_get_to_index_page_logged_in()
    {
        HomeController controller = CreateHomeControllerAs("dave");
    }

使用起订量引用... VS2010在WinXP下。

Using Moq referenced... VS2010 under WinXP.

推荐答案

有什么不对您的code。我只是测试它和它的工作的罚款。问题是与起订量组装。您需要授予特定的权限 C:\\ Documents和Settings \\ AllUsers的\\的ApplicationData \\微软\\加密\\ RSA \\ MachineKeys的文件夹中。结帐<一个href=\"http://groups.google.co.uk/group/RhinoMocks/browse_thread/thread/26df68ff01567509/5ddebf407228edc4\"相对=nofollow>这个讨论。

There's nothing wrong with your code. I've just tested it and it worked fine. The problem is with the Moq assembly. You need to grant specific permissions to the C:\Documents and Settings\AllUsers\ApplicationData\Microsoft\Crypto\RSA\MachineKeys folder. Checkout this discussion.

此外右键点击 Moq.dll 在Windows资源管理器,然后在属性确保它没有被锁定。当你从网上下载一些DLL Windows将自动适用于受限制的权限了。

Also right click on the Moq.dll in Windows Explorer and in the properties make sure that it is not locked. When you download some DLL from the internet Windows automatically applies restricted permissions to it.

这篇关于MVC测试用讽刺框架(MOQ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆