AutoFixture.AutoMoq supply a known value for one constructor parameter

So I’m sure people could work out the generalized implementation of Mark’s suggestion but I thought I’d post it for comments. I’ve created a generic ParameterNameSpecimenBuilder based on Mark’s LifeSpanArg: public class ParameterNameSpecimenBuilder<T> : ISpecimenBuilder { private readonly string name; private readonly T value; public ParameterNameSpecimenBuilder(string name, T value) { // we don’t want a … Read more

How can I instruct AutoFixture to not bother filling out some properties?

The answer provided by Nikos Baxevanis provides various convention-based ways to answer the question. For completeness sake, you can also do a more ad-hoc build: var phoneBook = fixture.Build<PhoneBook>().Without(p => p.AllContacts).Create(); If you want your Fixture instance to always do this, you can Customize it: fixture.Customize<PhoneBook>(c => c.Without(p => p.AllContacts)); Every time that Fixture instance … Read more

Autofixture customizations: provide constructor parameter

As answered here you can have something like public class FooArg : ISpecimenBuilder { private readonly string value; public FooArg(string value) { this.value = value; } public object Create(object request, ISpecimenContext context) { var pi = request as ParameterInfo; if (pi == null) return new NoSpecimen(request); if (pi.Member.DeclaringType != typeof(Foo) || pi.ParameterType != typeof(string) || … Read more

How to combine AutoDataAttribute with InlineData

You’ll have to create your own InlineAutoMoqDataAttribute, similar to this: public class InlineAutoMoqDataAttribute : InlineAutoDataAttribute { public InlineAutoMoqDataAttribute(params object[] objects) : base(new AutoMoqDataAttribute(), objects) { } } and you’d use it like this: [Theory] [InlineAutoMoqData(3,4)] [InlineAutoMoqData(33,44)] [InlineAutoMoqData(13,14)] public void SomeUnitTest(int DataFrom, int OtherData, [Frozen]Mock<ISomeInterface> theInterface, MySut sut) { // actual test omitted } Note that … Read more

What is AutoFixture AutoMoq?

In short, AutoFixture.AutoMoq is an extension that turns AutoFixture into an Auto-Mocking Container using the Moq dynamic mock library. There’s also a similar extension for AutoFixture that enables auto-mocking with Rhino Mocks. This article introduces auto-mocking for AutoFixture: http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx. Here’s a couple of follow-ups: http://blog.ploeh.dk/2010/08/25/ChangingTheBehaviorOfAutoFixtureAutomockingWithMoq.aspx http://blog.ploeh.dk/2010/11/13/RhinoMocksbasedAutomockingWithAutoFixture.aspx

AutoFixture – configure fixture to limit string generation length

With the Build method itself, there aren’t that many options, but you can do something like this: var constrainedText = fixture.Create<string>().Substring(0, 10); var mc = fixture .Build<MyClass>() .With(x => x.SomeText, constrainedText) .Create(); However, personally, I don’t see how this is any better or easier to understand that this: var mc = fixture .Build<MyClass>() .Without(x => … Read more

Can’t grasp the difference between Freeze/Inject/Register

Register and Inject Once upon a time, there was no Inject and no Freeze; Register ruled the code. Back then, there was a Register overload defined thusly: public static void Register<T>(this IFixture fixture, T item) However, it had to share the API with this close relative: public static void Register<T>(this IFixture fixture, Func<T> creator) The … Read more

Difference between Fact and Theory? – xUnit.net

Assuming that your [AutoMoqData] attribute looks something like this: public class AutoMoqDataAttribute : AutoDataAttribute { internal AutoMoqDataAttribute() : base(new Fixture().Customize(new AutoMoqCustomization())) { } } Then, yes, those two tests are equivalent. Both [Fact] and [Theory] attributes are defined by xUnit.net. The [Fact] attribute is used by the xUnit.net test runner to identify a ‘normal’ unit … Read more