问题描述
我让 xUnit/Moq/AutoFixture 成功地协同工作,以便我通过测试方法输入参数自动模拟对象.我创建了一个自定义的 [AutoMoqData]
属性,我在每次测试中都会使用它.这是属性的代码:
I have xUnit/Moq/AutoFixture successfully working together so that I am auto mocking objects via test method input parameters. I created a custom [AutoMoqData]
attribute which I use on every test. Here's the code for the attribute:
using System.Linq;
using AutoFixture;
using AutoFixture.AutoMoq;
using AutoFixture.Xunit2;
namespace Shared.TestResources.AutoFixture
{
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute() : base(() => new Fixture().Customize(new CompositeCustomization(new AutoMoqCustomization(), new SupportMutableValueTypesCustomization())))
{
this.Fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => Fixture.Behaviors.Remove(b));
this.Fixture.Behaviors.Add(new OmitOnRecursionBehavior());
}
}
}
这有效,但我收到以下编译警告:warning CS0618: 'AutoDataAttribute.Fixture' is obsolete: 'Fixture is lazily created for the performance efficiency,因此不推荐使用此属性,因为它会立即激活fixture.如果您需要自定义夹具,请在传递给构造函数的工厂方法中进行.'
This works, but I get the following compile warning: warning CS0618: 'AutoDataAttribute.Fixture' is obsolete: 'Fixture is created lazily for the performance efficiency, so this property is deprecated as it activates the fixture immediately. If you need to customize the fixture, do that in the factory method passed to the constructor.'
我通过用 #pragma
包围警告来消除警告:
I've muted the warning by surrounding it with a #pragma
:
using System.Linq;
using AutoFixture;
using AutoFixture.AutoMoq;
using AutoFixture.Xunit2;
namespace Shared.TestResources.AutoFixture
{
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute() : base(() => new Fixture().Customize(new CompositeCustomization(new AutoMoqCustomization(), new SupportMutableValueTypesCustomization())))
{
#pragma warning disable 0618
this.Fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => Fixture.Behaviors.Remove(b));
this.Fixture.Behaviors.Add(new OmitOnRecursionBehavior());
#pragma warning restore 0618
}
}
}
但是,我想正确设置它,而不必假装警告不存在.问题是,我无法找出 #pragma
中两行的正确语法.
However, I want to set this up correctly and not have to pretend like the warning doesn't exist. The problem is, I'm having trouble figuring out the correct syntax for the two lines inside of the #pragma
.
有什么想法吗?
推荐答案
在返回之前,只需使用带有 body 的 lambda 来额外配置 fixture
实例:
Just use a lambda with body to additionally configure the fixture
instance before returning it:
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute() : base(() =>
{
var fixture = new Fixture().Customize(new CompositeCustomization(
new AutoMoqCustomization(),
new SupportMutableValueTypesCustomization()));
fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => Fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new OmitOnRecursionBehavior());
return fixture;
})
{
}
}
这样 Fixture
激活将真正懒惰,您将获得预期的性能优化;-)
This way the Fixture
activation will be truly lazy and you will get the expected performance optimization ;-)
这篇关于为自动模拟设置自定义 AutoDataAttribute 时,告诉 AutoFixture 忽略所有递归结构的正确语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!