autofixture

AutoFixture 2 With() isn't working as it did in AutoFixture 1?

怎甘沉沦 提交于 2019-12-10 16:47:11
问题 I'm porting my tests to AutoFixture 2.0, and I've run in to some strange behavior that I can neither explain nor fix. This simple test is failing for me: var autoFixtures = new Fixture(); var file = autoFixtures.Build<File>() .With(f => f.Name, "bleh.txt") .CreateAnonymous(); Assert.That(file.Name, Is.EqualTo("bleh.txt")); // Fail?! The test succeeds if I change Name to another property of File , which leads me to think that I have some sort of customization present for Name that wasn't

IList<something> constructor parameter and AutoFixture

自古美人都是妖i 提交于 2019-12-10 15:53:55
问题 Using autofixture, I'm trying to construct anonymous instance of Project : _f=new Fixture().Customize(new AutoMoqCustomization()); _p=_f.CreateAnonymous<Project>(); This fails, cause Project public constructor demands IList<Partner> public Project(/*.....*/,IList<Partner> partners){ Guard.AgainstEmpty(partners); } Stack trace isn't meaningful (at least - for me). Just some reflection yada-yada: failed: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an

How do I get AutoFixture AutoMoq to return results from injected services in an instantiated object?

痞子三分冷 提交于 2019-12-10 15:49:38
问题 I am trying to test a service class that consumers a repository service. I have customizations set up that I believe should work with my repository service, but instead return default Anonymous results. If you look at the code sample below, I'm trying to get the "Foo" objects that I registered in the Customization Class back when I call the svc.GetFoos method, instead I get nothing: void Main() { var fixture = new Fixture().Customize( new CompositeCustomization( new Customization(), new

Class with a nested collection - how do I populate the nested class?

梦想与她 提交于 2019-12-10 15:37:14
问题 I'm a bit confused as to hydrate a class with a nested collection of another class. I get the error: AutoFixture was unable to create an instance from System.Collections.Generic.IList`1[typename...] I've tried to use Fixture.Register() to register the type for populating the inner class. I could just to do with a sample which shows The main class The nested collection (IList/IEnumerable) of another class. I've also noticed that Register() is marked as obsolete, yet there is no documentation

AutoFixture with “weak” types

南楼画角 提交于 2019-12-10 15:11:32
问题 I love AutoFixture, but have run into a bit of very repetitive "arrange" code that I feel like it should be able to handle - somehow. Here is my scenario, illustrated using implementations of IInterceptor from Castle Dynamic Proxy. First the systems under test: public class InterceptorA : IInterceptor { public void Intercept(IInvocation context) { object proxy = context.Proxy; object returnValue = context.ReturnValue; // Do something with proxy and returnValue } } public class InterceptorB :

Customization for creating implementations of a base type

百般思念 提交于 2019-12-10 14:48:00
问题 So I have the following types: public abstract class Base { public string Text { get; set; } public abstract int Value { get; set; } } public class BaseImplA : Base { public override int Value { get; set; } } public class BaseImplB : Base { public override int Value { get { return 1; } set { throw new NotImplementedException(); } } } I want AutoFixture to alternate creating BaseImplA and BaseImplB when Base is requested. var fixture = new Fixture().Customize(new TestCustomization()); var b1 =

Create a fixture for recursive data structure with AutoFixture

不问归期 提交于 2019-12-10 13:19:37
问题 I'm working on a project where I have some recursive data structure and I want to create a fixture for it. The data structure is XmlCommandElement , it has a single method ToCommand that converts XmlCommandElement to Command . Each node on the tree can be a XmlCommandElement and/or XmlCommandPropertyElement . Now, in order to test the behaviour of the method ToCommand I want to fetch XmlCommandElement with some arbitrary data. I want to control the depth of the tree and the amount of

How to mockup Entity Framework 6 With Moq & Autofixture

蹲街弑〆低调 提交于 2019-12-10 13:14:48
问题 I am using AutoMoq but I am kinda confused how to write my first unit test because of Entity Framework's (using EF6 and code first) dbContext // in service class(constructor) private readonly MyContext context; public PriceService(MyContext context) { this.context = context; } // following would be in nunit test method. var fixture = new Fixture().Customize(new AutoMoqCustomization()); var priceService = fixture.Create<PriceService>(); When I run the unit test it crashes at Ploeh.AutoFixture

How can I Freeze a null instance in AutoFixture

ⅰ亾dé卋堺 提交于 2019-12-10 12:53:52
问题 I'm using Autofixture as a SUT factory and am having difficulty Freezing null instances. I'd like to do something like: _fixture.Freeze<IPayPalConfiguration>(c => null); but quickly realized that was wrong. I've worked around the problem using this: _fixture.Inject((IMyInterface)null); but it doesn't seem right. Hopefully someone will contribute the correct solution to the HiveMind. 回答1: Internally, Freeze creates an instance of the requested type (e.g. IPayPalConfiguration ) and then injects

Creating a struct with autofixture throws no public constructor error

核能气质少年 提交于 2019-12-10 12:53:47
问题 I have a struct e.g.: public struct Foo { public int Bar; public string Baz; } and would like to create it in my unit tests using autofixture. I tried using the following: IFixture fixture = new Fixture(); var f = fixture.CreateAnonymous<Foo>(); But this throws an AutoFixture was unable to create an instance error. Is it possible to auto create a struct with autofixture? How could this be done? Please note, that I am working on legacy code and thus need to deal with structs. :) EDIT : Changed