在尝试模拟基类对象的受保护属性并在执行测试时遇到异常时,我遇到了问题。
这是我的基类,派生类和单元测试的代码片段
//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...
}
在运行测试时,受保护的成员“ Provider”由于以下错误而失败,并且没有获取fakeProvider对象以避免基类依赖。
我正在使用TypeMock框架,无法继续执行此步骤,我的想法是也伪造基类和受保护的属性。
在这种情况下,有人可以建议我采取正确的方法吗?