c# - 不单独运行测试时,Moq.Proxy.CaSTLeProxyFactory 的类型初始值设定项抛出的异常

标签 c# asp.net-mvc-4 mono moq typeinitializeexception

使用 Moq 对返回带有 Stack 的 View 模型的 MVC 4 操作方法进行了以下测试:

// GET: /Home/SowingAndHarvesting
public ActionResult SowingAndHarvesting()
{
    // Months are used for the CSS classes 
    // to add to the squares and for displayal within the square.
    var months = MonthHelper.GetAllMonths().ToList();

    // Ordering for the squared boxes view (4 columns for the seasons)
    var monthIndexOrdering = new[] { 7, 4, 1, 10, 
                                     6, 3, 0,  9, 
                                     5, 2, 11, 8 };
    var displayMonthsOrdered = new Stack<MonthViewModel>();
    foreach (var monthIndex in monthIndexOrdering)
    {
        var month = months[monthIndex];
        var name = month.ToString();
        var monthViewModel = new MonthViewModel(name);

        displayMonthsOrdered.Push(monthViewModel);
    }

    var viewModel = new SowingAndHarvestingViewModel 
    {
        // Months in the squared and information belonging to the month
        OrderedMonthViewModels = displayMonthsOrdered
    };

    return View(viewModel);
}

MonthViewModel 是这样的(它有一些更多的显示属性,为简洁起见,SowingAndHarvestingViewModel 是一个包装器):

public class MonthViewModel
{
    public MonthViewModel(string monthName)
    {
        MonthForDataAttribute = monthName.ToLower();
    }

    public string MonthForDataAttribute { get; set; }
}

测试看起来如下:

[TestFixture]
public class HomeControllerTest
{

    [Test]
    public void Controllers_SowingAndHarvesting_DataMonthOrdering()
    {
        // Arrange
        var expectedMonthOrdering =  return new Stack<MonthViewModel>(new[] 
            {   
                new MonthViewModel("august"), 
                new MonthViewModel("may"),
                new MonthViewModel("february"),
                new MonthViewModel("november"),
                new MonthViewModel("july"),
                new MonthViewModel("april"),
                new MonthViewModel("january"),
                new MonthViewModel("october"),     
                new MonthViewModel("june"),
                new MonthViewModel("march"),
                new MonthViewModel("december"),
                new MonthViewModel("september")       
            });  ;

        var mock = new Mock<ICalendarService>();
        mock.Setup(c => c.GetMonthsWithAction())
            .Returns(It.IsAny<Month>);
        var controller = new HomeController(mock.Object);

        // Act
        var result = (SowingAndHarvestingViewModel)((ViewResult)controller.SowingAndHarvesting()).Model;

        // Assert
        while (expectedMonthOrdering.Count != 0)
        {
            var expected = expectedMonthOrdering.Pop().MonthForDataAttribute;
            var actual = result.OrderedMonthViewModels.Pop().MonthForDataAttribute;

            Assert.AreEqual(expected, actual,
                "The months in the data attributes should be in the correct order and format.");
        }
    }

现在,当我单独运行此测试时,它通过了。但是当我将它与其他测试一起运行时,它会失败并显示消息:

System.TypeInitializationException : An exception was thrown by the type initializer for Moq.Mock`1 ----> System.TypeInitializationException : An exception was thrown by the type initializer for Moq.Proxy.CastleProxyFactory ----> System.NullReferenceException : Object reference not set to an instance of an object

有谁知道这是为什么以及如何解决?

最佳答案

如果您使用的是 .Net Framework 4.6 和 Web API,您可能会遇到此错误,因为最近对 CaSTLe.Core 进行了更新。将 CaSTLe.Core 更新到 v4.2.0 会导致此错误,因此请尝试将 CaSTLe.Core NuGet 包回滚到版本 4.1.1

关于c# - 不单独运行测试时,Moq.Proxy.CaSTLeProxyFactory 的类型初始值设定项抛出的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30055158/

相关文章:

c# - 当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'ClientDetails' 中为标识列插入显式值

c# - 通过 ADO.NET 调用存储过程来填充 List<int> 的最有效方法

c# - 使用 PostAsync、HttpClient 和 Json 从 C# Metro UI 客户端调用 MVC4 WebAPI 方法

macos - F# 无法在 MonoDevelop 2.8 for Mac 上安装

c# - 使用未赋值的局部变量 "delivered"

asp.net-mvc - 保留 ASP.NET MVC Controller 名称?

c# - MVC 4 如何正确地将数据从 Controller 传递到 View

c# - 为 .NET 和单声道编写 C# 应用程序的指南

c++ - Linux 中伪终端的读写问题

c# - GZipStream 和 DeflateStream 产生更大的文件