1
我建立方法,返回ProfileBase.Create:.NET自定義配置文件提供方法
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: This method cannot be called during the application's pre-start initialization phase.
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));