.net - Autofixture 奇怪的错误

标签 .net autofixture

我遇到了这个错误。

Ploeh.AutoFixture.Kernel.IllegalRequestException : A request for an IntPtr was detected. This is an unsafe resource that will crash the process if used, so the request is denied. A common source of IntPtr requests are requests for delegates such as Func or Action. If this is the case, the expected workaround is to Customize (Register or Inject) the offending type by specifying a proper creational strategy.

这是我的测试代码。我正在使用 autofac,其中一个依赖项是聚合服务。

var fixture = new Fixture().Customize(new AutoMoqCustomization());

var moq = new Mock<ITaskReadService>();
moq.Setup(x => x.GetFormItems(1)).Returns(GetDataTable());

IIcpServiceAggregate aggregate = _container.Resolve<IIcpServiceAggregate>();

fixture.Freeze(aggregate);
fixture.Freeze(moq.Object);

var c = fixture.CreateAnonymous<TaskController>();

最佳答案

假设 TaskController 是一个 ASP.NET MVC 3 Controller ,您应该能够通过执行以下操作来解决这个特定问题:

fixture.Customize<ViewDataDictionary>(c =>
    c.Without(vdd => vdd.ModelMetadata));

因为 ViewDataDictionary.ModelMetadata 是一种 Func(记不清是哪一种)。

OOB support for delegates is coming in a future version of AutoFixture .

关于.net - Autofixture 奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6582711/

相关文章:

c# - CryptoStream 没有像预期的那样刷新

c# - Autofixture - 创建带余数的 float 、 double 或小数

c# - AutoFixture 无法创建声明(使用 .NET Framework 4.6)

c# - 异常调用堆栈被截断而没有任何重新抛出

c# - Always Freeze 使用 AutoFixture、XUnit 和 Moq 进行模拟

autofixture - 为什么 Autofixture Likeness 的行为不像我对这两个测试之一的预期那样?

c# - 有没有办法告诉 Autofixture 只设置具有特定属性的属性?

.net - log4net - FileAppender 在文件开头写入新条目

c# - 在 OpenFileDialog 中使用 DialogResult.OK 时出错

c# - 自更新 WPF 应用程序的库