我有一个名为“UserController”的控制器,其方法名为“Invite”。我的控制器具有以下覆盖方法:
DBRepository _repository;
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
_repository = new DBRepository();
}
因此,每次创建 UserController 类时都会调用此方法。
我的方法“邀请”有以下几行:
var startTime = _repository.Get<AllowedTime>(p => p.TimeID == selectTimeStart.Value);
但是当我尝试通过 Unit 方法调用此方法时:
[TestMethod()]
[UrlToTest("http://localhost:6001/")]
public void InviteTest()
{
UserController target = new UserController(); // TODO: Initialize to an appropriate value
int? selectTimeStart = 57;
int? selectTimeEnd = 61;
Nullable<int> selectAttachToMeeting = new Nullable<int>(); // TODO: Initialize to an appropriate value
int InvitedUserID = 9; // TODO: Initialize to an appropriate value
UserInviteModel model = new UserInviteModel();
model.Comment = "BLA_BLA_BLA";
ActionResult expected = null; // TODO: Initialize to an appropriate value
ActionResult actual;
actual = target.Invite(selectTimeStart, selectTimeEnd, selectAttachToMeeting, InvitedUserID, model);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
我收到错误“未设置参考...”。我理解它为什么会发生(_repository 为空,因为在我的情况下未调用 Initialize 方法,但如何正确执行?