gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:22:02 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com