Search code examples
c#asp.net-mvc-4monomoqtypeinitializationexception

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


Using Moq the following test on a MVC 4 action method that returns a viewmodel with a Stack was created:

// 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);
}

Where MonthViewModel is like this (it has some more properties for display which are removed for brevity, SowingAndHarvestingViewModel is a wrapper around this):

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

    public string MonthForDataAttribute { get; set; }
}

The test looks as follows:

[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.");
        }
    }

Now, when I run this test in isolation it passes. But when I run it together with other tests it fails with the message:

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

Does anyone know why this is and how it can be solved?


Solution

  • I was running Mono 3.12 as shown by mono -V. Updating to Mono 4 with sudo apt-get install mono-complete fixed the issue!