0

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.

1 Answer 1

1

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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome.. I wrote a blog post on this too programgood.net/2010/07/05/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.