T4MVC 在单元测试期间抛出 TypeInitializationException - 我该如何解决这个问题?
我正在向 NerdDinner 解决方案添加单元测试,并遇到了这个问题。我正在测试 Edit POST 方法,如下所示:
[AcceptVerbs(HttpVerbs.Post), Authorize]
public virtual ActionResult Edit(int id, FormCollection formValues)
{
Dinner dinner = dinnerRepository.GetDinner( id );
if (!dinner.IsHostedBy(User.Identity.Name))
{
return View(Views.InvalidOwner);
}
try
{
UpdateModel(dinner);
dinnerRepository.Save();
//return RedirectToAction("Details", new { id = dinner.DinnerID });
return RedirectToAction(Actions.Details(dinner.DinnerID));
}
catch (Exception ex)
{
foreach (var issue in dinner.GetRuleViolations())
{
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(new DinnerFormViewModel(dinner));
}
}
System.TypeInitializationException 在“return RedirectToAction...”行上抛出,并显示“‘MVC’的类型初始值设定项引发了异常。”
当我用原始行(上面注释掉)替换 T4MVC 编码行时,我没有得到异常。
这是单元测试代码:
[TestMethod]
public void EditAction_Should_Redirect_When_Update_Successful()
{
// Arrange
var controller = CreateDinnersControllerAs("Some User");
var formValues = new FormCollection
{
{ "Title", "Another Value" },
{ "Description", "Another Description" }
};
controller.ValueProvider = formValues.ToValueProvider();
// Act
var result = controller.Edit(1, formValues) as RedirectToRouteResult;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual("Details", result.RouteValues["Action"]);
}
我有一些想法可能导致抛出此异常,但我不确定是否可以在这里提出。我完全不清楚如何解决它。
有想法吗?
戴夫
I am adding unit testing to the NerdDinner solution, and ran across this. I am testing the Edit POST method, given here:
[AcceptVerbs(HttpVerbs.Post), Authorize]
public virtual ActionResult Edit(int id, FormCollection formValues)
{
Dinner dinner = dinnerRepository.GetDinner( id );
if (!dinner.IsHostedBy(User.Identity.Name))
{
return View(Views.InvalidOwner);
}
try
{
UpdateModel(dinner);
dinnerRepository.Save();
//return RedirectToAction("Details", new { id = dinner.DinnerID });
return RedirectToAction(Actions.Details(dinner.DinnerID));
}
catch (Exception ex)
{
foreach (var issue in dinner.GetRuleViolations())
{
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(new DinnerFormViewModel(dinner));
}
}
The System.TypeInitializationException is thrown on the "return RedirectToAction..." line, and says "The type initializer for 'MVC' threw an exception."
When I replace the T4MVC-encoded line with the original line (commented out above), I do not get the exception.
Here is the unit test code:
[TestMethod]
public void EditAction_Should_Redirect_When_Update_Successful()
{
// Arrange
var controller = CreateDinnersControllerAs("Some User");
var formValues = new FormCollection
{
{ "Title", "Another Value" },
{ "Description", "Another Description" }
};
controller.ValueProvider = formValues.ToValueProvider();
// Act
var result = controller.Edit(1, formValues) as RedirectToRouteResult;
// Assert
Assert.IsNotNull(result);
Assert.AreEqual("Details", result.RouteValues["Action"]);
}
I have a couple of ideas what may be causing this exception to be thrown, but I'm not sure enough to posit here. I'm totally unclear as to how to fix it.
Ideas?
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MVC是T4MVC生成的类。您看到的错误只是意味着在生成的 MVC 类的构造函数内引发了异常(请注意,从这个意义上讲,“构造函数”也意味着在声明时分配了值的任何字段的初始化)。
打开并保存 T4MVC.tt 文件,以确保代码生成的文件是最新的。如果这没有帮助,并且您正在使用可用的最新版本,请在生成的 MVC 类的构造函数中设置一个断点,以找出破坏它的原因。
MVC is a class generated by T4MVC. The error you are seeing simply means that an exception was thrown inside the constructor of this generated MVC class (note that "constructor" in this sense also means initialization of any fields that are assigned a value where declared).
Open and save the T4MVC.tt file to ensure your code generated file is up to date. If that doesn't help and you're using the most recent version available, set a breakpoint in the constructor of the generated MVC class to find out what breaks it.
对于那些可能关注我的人来说——使用 T4MVC 在测试过程中引入了交叉依赖性。本质上,T4MVC 为所有控制器生成新的部分类,这可能会导致问题。就我而言,MVC 构造函数正在尝试创建 RSVPController,并且由于我还没有接触过它,因此它仍在尝试连接到数据库。
我不确定如何将单独的控制器与 T4MVC 混合在一起进行单元测试,因为它涉及到一切。如果您有想法,请告诉我...
戴夫
For those who may follow behind me -- using T4MVC introduces cross-dependencies in the testing process. Essentially, T4MVC generates new partial classes for all your controllers, and that can cause problems. In my case, the MVC constructor was trying to create the RSVPController, and since I hadn't touched that yet, it was still trying to connect to the database.
I'm not sure how to decouple the separate controllers for unit testing with T4MVC in the mix, as it touches everything. If you have ideas, please let me know...
Dave