autofixture

Force AutoFixture to use the greediest constructor

╄→гoц情女王★ 提交于 2019-11-30 08:04:22
I have a data type with multiple constructors and I need AutoFixture to choose the greediest (one with the most parameters). The default behaviour is to choose the constructor with the smallest number. The author's blog post, http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx doesn't seem to imply there's a way of overriding this behaviour, so is it possible, and if so, how? This is certainly possible . To change the strategy for a single type ( MyClass ): fixture.Customize<MyClass>(c => c.FromFactory( new MethodInvoker( new GreedyConstructorQuery()))); To change the strategy

Applying DRY to Autofixture “Build” statements

丶灬走出姿态 提交于 2019-11-30 06:53:34
Assume I have this concrete class: public partial class User { public int ID { get; set; } public string Email { get; set; } public string FullName { get; set; } } And I want to create an anonymous instance that has a valid email address, and the fullname field is no more than 20 characters. I can do this: var fixture = new Fixture(); var anonUser = fixture.Build<User>() .With(x => x.Email, string.Format("{0}@fobar.com", fixture.Create<string>())) .With(x => x.FullName, fixture.Create<string>()Substring(0,20)) .Create(); Is there a way that I can define this in one place, so that AF knows that

How to combine PropertyData and AutoNSubstituteData attributes in xunit/autofixture?

淺唱寂寞╮ 提交于 2019-11-30 05:48:39
问题 I am using the [AutoNSubstituteData] attribute, which was posted here: AutoFixture, xUnit.net, and Auto Mocking I would like to combine this with the [PropertyData("")] attribute from xunit extensions. This is my test: public static IEnumerable<string[]> InvalidInvariant { get { yield return new string[] { null }; yield return new [] { string.Empty }; yield return new [] { " " }; } } [Theory, AutoNSubstituteData, PropertyData("InvalidInvariant")] public void

Select specific constructor with AutoFixture

本小妞迷上赌 提交于 2019-11-30 05:43:19
问题 I'm using AutoFixture and I'd like to use a specific constructor. I have the following code and I like to select the constructor with ITemplateParameterHandler. public sealed class TemplateSegmentHandler : ITemplateSegmentHandler { public TemplateSegmentHandler(ITemplateIterator iterator) : this(new TemplateParameterHandler(iterator)) { Contract.Requires(iterator != null); } public TemplateSegmentHandler(ITemplateParameterHandler parameterHandler) { Contract.Requires(parameterHandler != null)

AutoFixture: mock methods don't return a frozen instance

我们两清 提交于 2019-11-30 04:43:55
问题 I'm trying to write this simple test: var fixture = new Fixture().Customize(new AutoMoqCustomization()); var postProcessingAction = fixture.Freeze<Mock<IPostProcessingAction>>(); var postProcessor = fixture.Freeze<PostProcessor>(); postProcessor.Process("", ""); postProcessingAction.Verify(action => action.Do()); The Verify check fails. The code for postProcessor.Process is public void Process(string resultFilePath, string jobId) { IPostProcessingAction postProcessingAction =

Why does Autofixture w/ AutoMoqCustomization stop complaining about lack of parameterless constructor when class is sealed?

六月ゝ 毕业季﹏ 提交于 2019-11-30 04:20:51
问题 When I use Moq directly to mock IBuilderFactory and instantiate BuilderService myself in a unit test, I can get a passing test which verifies that the Create() method of IBuilderFactory is called exactly once. However, when I use Autofixture with AutoMoqCustomization , freezing a mock of IBuilderFactory and instantiating BuilderService with fixture.Create<BuilderService> , I get the following exception: System.ArgumentException: Can not instantiate proxy of class: OddBehaviorTests.CubeBuilder

AutoFixture: Configuring an Open Generics Specimen Builder

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 03:58:19
问题 I have an object model that uses Open Generics (Yes, yes, now I have two problems; that's why I'm here :) :- public interface IOGF<T> { } class C { } class D { readonly IOGF<C> _ogf; public D( IOGF<C> ogf ) { _ogf = ogf; } } I'm trying to get AutoFixture to generate Anonymous instances of D above. However, on its own, AutoFixture doesn't have a built in strategy for building an IOGF<> and hence we observe: public class OpenGenericsBinderDemo { [Fact] public void X() { var fixture = new

Create anonymous enum value from a subset of all values

╄→гoц情女王★ 提交于 2019-11-30 03:14:01
问题 Let's say we have an enum type defined as: enum Statuses { Completed, Pending, NotStarted, Started } I'd like to make Autofixture create a value for me other than e.g. Pending. So (assuming round-robin generation) I'd like to obtain: Completed, NotStarted, Started, Completed, NotStarted, ... 回答1: The easiest way to do that is with AutoFixture's Generator<T> : var statuses = fixture .Create<Generator<Statuses>>() .Where(s => Statuses.Pending != s) .Take(10); If you only need a single value,

How to implement date restrictions with AutoFixture?

假装没事ソ 提交于 2019-11-30 01:37:05
问题 I'm currently having a model class which contains several properties. A simplified model could look like this: public class SomeClass { public DateTime ValidFrom { get; set; } public DateTime ExpirationDate { get; set; } } Now I'm implementing some unit tests by using NUnit and use AutoFixture to create some random data: [Test] public void SomeTest() { var fixture = new Fixture(); var someRandom = fixture.Create<SomeClass>(); } This works perfect so far. But there is the requirement that the

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

时间秒杀一切 提交于 2019-11-29 22:08:47
Before starting, I'm a big fan of AutoFixture, I'm still in the curve of learning how to use the tool. So thanks for having developed Autofixture Mr Ploeh and all the contributors. So let's start with my question. According to AutoFixture/AutoMoq ignores injected instance/frozen mock The interesting part of the above link is given this code Mock<ISettings> settingsMock = new Mock<ISettings>(); settingsMock.Setup(s => s.Get(settingKey)).Returns(xmlString); ISettings settings = settingsMock.Object; fixture.Inject(settings); To which Mark answer it can be rewritten to fixture.Freeze<Mock