Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So i'm using Moq and starting to write unit tests for my controller actions.

All of my controllers inherit from a base CustomController class that provides a userId property set in the constructor to be the value of Membership.GetUser().ProviderUserKey. This way I can pass the userId to my service layer simply by passing contactId.

This does not seem to work well with this unit test:

  var serviceMock = new Mock<IFormService>();
        serviceMock.Setup(g => g.GetForm(2)).Returns(new FormViewModel() {ID = 2, Name = "Mock Form"});
        var controller = new FormController(serviceMock.Object);
        var result = controller.Index(2);
        Assert.IsNotNull(result, "View Result is null");

It throws an exception on the base CustomController where it tries to set the userId from Membership.GetUser().ProviderUserKey.

"UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\App_Data' is denied."

Has anyone else come across this issue or have any thoughts on a way around it?

share|improve this question
    
It may help to see your constructor, but my guess is you will need to inject a mock membership provider, and configure your DI container to use a lifecyle based on the request. Basically if it is a dependency it needs to be in the contructor so you can control its behaviors in a test. see stackoverflow.com/questions/4193484/… –  stuisme Jul 20 '13 at 4:43

1 Answer 1

You maybe forget to set the membership provider in your webconfig:

<system.web>
    <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20">
</system.web>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.