使用模拟框架进行 MVC 测试 (Moq)
我正在使用 Moq 来帮助测试我的 ASP.NET MVC2 应用程序。
问题:ArgumentException 未被用户代码处理。无法获取 StrongNameKeyPair 的公钥
此代码改编自 Scott Hanselman 的 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");
}
使用Moq参考...WinXP下的VS2010。
I'm using Moq to help in testing my ASP.NET MVC2 application.
Problem: ArgumentException was unhandled by user code. Unable to obtain public key for StrongNameKeyPair
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");
}
Using Moq referenced... VS2010 under WinXP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码没有任何问题。我刚刚测试过,效果很好。问题出在
Moq
组件上。您需要向C:\Documents and Settings\AllUsers\ApplicationData\Microsoft\Crypto\RSA\MachineKeys
文件夹授予特定权限。查看此讨论。另外,在 Windows 资源管理器中右键单击
Moq.dll
,并在属性中确保它未锁定。当您从 Internet 下载某些 DLL 时,Windows 会自动对其应用受限权限。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 theC:\Documents and Settings\AllUsers\ApplicationData\Microsoft\Crypto\RSA\MachineKeys
folder. Checkout this discussion.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.