UnitTest ASP.NET Application Requiring User Login

I have an ASP.NET application that allows users to log in. Another part of my application uses UserId (the user must be logged in to access the controller). How to fake logon for unit testing?

This is how I get UserId

Private _UserId As Guid
    Public ReadOnly Property UserId() As Guid
        Get
            _UserId = System.Web.Security.Membership.GetUser().ProviderUserKey
            Return _UserId
        End Get
    End Property

thank

EDIT

This is an MVC 3 project.

+5
source share
5 answers

You can write a wrapper class for your membership so you can create a layout for use in unit tests. The code is in C #, I'm sorry, but you will get my opinion.

    public interface IMyMemberShip
    {
        Guid GetUserId();
    }

    public class MyMemberShip : IMyMemberShip
    {
        public Guid GetUserId()
        {
            return (Guid)System.Web.Security.Membership.GetUser().ProviderUserKey;
        }
    }

    public class MockMyMembership : IMyMemberShip
    {
        public Guid GetUserId()
        {
            return Guid.NewGuid();
        }
    }

    public class AnotherPartOfYourApplication
    {
        IMyMemberShip _myMembership;

        public AnotherPartOfYourApplication(IMyMemberShip myMemberShip)
        {
            _myMembership = myMemberShip;
        }

        public void GetUserIdAndDoSomething()
        {
            var userId = _myMembership.GetUserId();
        }
    }

, , , moq, mock .

var mock = new Mock<IMyMemberShip>();
mock.Setup(m => m.GetUserId()).Returns(Guid.NewGuid());
+5

FormsAuthentication.SetAuthCookie() - , FormsAuthentication .

0

, Unit Test. UserId() As Guid Guid , , , "".

0

You can write your own Fake ASP.NET Memberhip Provider or try using TypeMock Isolator to emulate an ASP.NET environment.

0
source

All Articles