Moq.Proxy.CastleProxyFactory 的类型初始值设定项在未单独测试 运行 时抛出的异常

Exception thrown by the type initializer for Moq.Proxy.CastleProxyFactory when not running test in isolation

使用 Moq 对 MVC 4 操作方法进行以下测试,returns 创建了具有 Stack 的视图模型:

// 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

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

我是 运行 Mono 3.12,如 mono -V 所示。使用 sudo apt-get install mono-complete 更新到 Mono 4 解决了这个问题!

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