我构建返回 ProfileBase.Create 的方法:
public class ProfilesProvider : ProfileBase, IProfilesProvider
{
public ProfilesProvider GetUserProfile()
{
return (HttpContext.Current.Profile) as ProfilesProvider;
}
public ProfilesProvider GetUserProfile(string userName)
{
if (String.IsNullOrEmpty(userName))
return (HttpContext.Current.Profile) as ProfilesProvider;
return Create(userName) as ProfilesProvider;
}
}
此代码运行良好,但是当我尝试为此方法创建单元测试时出现错误:
System.InvalidOperationException:在应用程序的预启动初始化阶段无法调用此方法。
PS这里是测试的代码:
_mockProfiles.Setup(m => m.GetUserProfile()).Returns(new ProfilesProvider());
var result = _controller.Settings(model);
_mockProfiles.Verify(m => m.GetUserProfile(), Times.Once());
Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));