Mocking Protected Property in TypeMock

143 views Asked by At

I am facing issue while trying to mock protected property of my base class object and getting exception while executing the test.

Here is the code snippet of my base class, Derived class and Unit Test

//A abstract class that provides external user authentication methods and helpers.
    public abstract class ExternalAuthenticationProvider
    {
        public abstract ExternalLoginResult SessionLogin(string username, string password);
    }

    //BaseAccountController has protected property of type 'ExternalAuthenticationProvider'
    public class BaseAccountController : BaseController
    {
        public BaseAccountController();
        protected ExternalAuthenticationProvider Provider { get; }
    }

    //Deriver class and AccountController inherits BaseAccountController
    public class AccountController : BaseAccountController
    {
        public ActionResult RecruitLogin(MvcLoginModel model)
        {
            var result = Provider.SessionLogin(model.Username, model.Password);
        }
    }

    //Here is my Unit Test method
    [TestMethod, Isolated]
    public void LoginTest_Post()
    {
        //Arrange
        var fakeModel = Isolate.Fake.Instance<MvcLoginModel>();
        var fakeResult = Isolate.Fake.Instance<ExternalLoginResult>();

        var fakePovider = Isolate.NonPublic.Fake.Instance(typeof(ExternalAuthenticationProvider));
        var fakeController = Isolate.Fake.Instance<BaseAccountController>(Members.ReturnRecursiveFakes);

        Isolate.NonPublic.WhenCalled(fakePovider, "SessionLogin").WillReturn(fakeResult);
        Isolate.NonPublic.Property.WhenGetCalled(fakeController, "Provider").ReturnRecursiveFake(typeof(ExternalAuthenticationProvider));

        //Act
        var _controller = new AccountController();
        var result = _controller.RecruitLogin(fakeModel, fakeUrl) as ViewResult;

        //Asert todo...
    }

While running the test, protected member "Provider" is failing with below error and not getting fakeProvider object to avoid base class dependencies.

I am using TypeMock framework and unable to proceed on this step and my idea is to fake the base class and protected property as well.

Can anyone suggest me with right approach in this scenario?

Error screen shot

0

There are 0 answers