T4MVC 在单元测试期间抛出 TypeInitializationException - 我该如何解决这个问题?

发布于 2024-10-21 16:29:54 字数 1902 浏览 4 评论 0原文

我正在向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-10-28 16:29:54

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.

所谓喜欢 2024-10-28 16:29:54

对于那些可能关注我的人来说——使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文