autofixture

AutoFixture: PropertyData and heterogeneous parameters

社会主义新天地 提交于 2019-11-29 14:22:30
Given the following test: [Theory] [PropertyData("GetValidInputForDb")] public void GivenValidInputShouldOutputCorrectResult( string patientId , string patientFirstName ) { var fixture = new Fixture(); var sut = fixture.Create<HtmlOutputBuilder>(); sut.DoSomething(); // More code } I want to encapsulate fixture creation in its own class, something akin to: [Theory] [CustomPropertyData("GetValidInputForDb")] public void GivenValidInputShouldOutputCorrectResult( string patientId , string patientFirstName , HtmlOutputBuilder sut ) { sut.DoSomething(); // More code } The problem is that I'm using

What does CreateMany with seed do?

∥☆過路亽.° 提交于 2019-11-29 13:32:21
What does the CreateMany overload with the T seed parameter actually do? I've tried to seed, but the seed seems to have no effect on the created objects. For example, I was expecting that if my seed had a property of type string , that either: the string value would be used to populate that property in all newly created objects or the string value would be used as a prefix when setting that property in all newly created objects tl;dr As a general rule, AutoFixture does not guarantee how seed values are going to be used during the creation process, if at all. This characteristic stems from the

Handling specimen creation inconsistencies between AutoFixture and Moq

此生再无相见时 提交于 2019-11-29 11:03:44
I am using AutoMoqCustomization in my test conventions. Consider the code below. Everything works great until I add a constructor to one of the concrete classes. When I do, I get "could not find a parameterless constructor". We know AutoFixture doesn't have an issue with the constructor because it delivered me the test object one which proved to be assignable from IThings... no failure there. So it must be moq. This makes some sense because I assume builder was generated by moq and passed into the GetCommands method. So I think I can see that control has been passed from AutoFixture to moq at

Specifying [readonly] property values [via ctor args] when instantiating [immutable] objects with AutoFixture

风格不统一 提交于 2019-11-29 10:19:30
My test requires that I set the Response property on an immutable Rsvp object (see below) to a specific value. public class Rsvp { public string Response { get; private set; } public Rsvp(string response) { Response = response; } } I initially tried to do this using Build<Rsvp>().With(x => x.Rsvp, "Attending") , but realized this only supports writable properties. I replaced that with Build<Rsvp>().FromFactory(new Rsvp("Attending")) . This works, but is cumbersome for more complex objects where it doesn't matter what some of the properties are. For instance, if the Rsvp object had a

How to let Autofixture create an instance of a type that contains properties with an interface type?

a 夏天 提交于 2019-11-29 06:44:01
I have such a class: public class ViewModel { public IPagination<Data> List { get; set; } // interface! public SearchFilter SearchFilter { get; set; } public string Test { get; set; } } public class SearchFilter { public string Name { get; set; } } A dynamic proxy shall be created around the IPagination interface and the proxy shall be filled with test data. Now is it possible to let AutoFixture create an instance of the ViewModel type? Be aware that I only know the type at runtime ( typeof(ViewModel) ). By now I know I can do this: var context = new SpecimenContext(fixture.Compose()); var

need to create convention for ApiControllers

馋奶兔 提交于 2019-11-29 04:11:47
I have a set of working imperative code in test and I'm trying to boil it down to an essential test convention. My test looks like the following: [Theory, BasicConventions] public void GetVersionOnSiteVersionControllerReturnsASiteVersion(IFixture fixture) { fixture.OmitAutoProperties = true; SiteVersion expected = fixture.Create<SiteVersion>(); SiteVersion actual = null; var sut = fixture.Create<SiteVersionController>(); var response = sut .GetSiteVersion() .ExecuteAsync(new CancellationToken()) .Result .TryGetContentValue<SiteVersion>(out actual); actual.AsSource().OfLikeness<SiteVersion>()

AutoFixture - configure fixture to limit string generation length

僤鯓⒐⒋嵵緔 提交于 2019-11-28 18:12:28
When using autofixture Build method for some type, how can I limit the length of the strings generated to fill that objects string properties/fields? Mark Seemann 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 => x.SomeText) .Create(); mc.SomeText = fixture

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

喜你入骨 提交于 2019-11-28 17:22:15
问题 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

Automocking Web Api 2 controller

纵饮孤独 提交于 2019-11-28 17:14:41
I am trying to auto mock ApiController class in my test cases. It worked perfectly when I was using WebApi1. I started to use WebApi2 on the new project and I am getting this exception thrown after I try to run my new tests: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Security.Cryptography.CryptographicException: pCertContext is an invalid handle. at System.Security.Cryptography.CAPI.CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, UInt32 dwPropId, UInt32 dwFlags, SafeLocalAllocHandle

Can AutoFixture execute a delegate at object creation time?

喜欢而已 提交于 2019-11-28 12:20:45
I'm looking to customize the creation-time behavior of AutoFixture such that I can set up some dependent objects after the properties of the fixture have been generated and assigned. For example, suppose I have a method that customizes a User because its IsDeleted property always has to be false for a certain set of tests: public class User { public int Id { get; set; } public string Name { get; set; } public bool IsDeleted { get; set; } } public static ObjectBuilder<User> BuildUser(this Fixture f) { return f.Build<User>().With(u => u.IsDeleted, false); } (I hand an ObjectBuilder back to the