autofixture

How do I assign a property on only a subrange of items in a list?

Deadly 提交于 2020-01-03 09:11:27
问题 I want to create a list of custom objects using AutoFixture . I want the first N objects to have a property set to one value, and the remainder to have it set to another value (or to simply be set by the Fixture 's default strategy). I am aware that I can use Fixture.CreateMany<T>.With , but this applies a function to all members of the list. In NBuilder there are methods named TheFirst and TheNext (among others) which provide this functionality. An example of their use: Given a class Foo :

Autofixture test for invalid constructor parameter

99封情书 提交于 2020-01-02 01:17:15
问题 I have the following class and test. I want to test passing a null value as a parameter to the constructor and are expecting an ArgumentNullException . But since I use the Autofixture's CreateAnonymous method I get a TargetInvocationException instead. What is the correct way to write those kinds of tests? public sealed class CreateObject : Command { // Properties public ObjectId[] Ids { get; private set; } public ObjectTypeId ObjectType { get; private set; } public UserId CreatedBy { get;

Autofixture customizations: provide constructor parameter

半腔热情 提交于 2020-01-01 07:32:44
问题 I have the following class: class Foo { public Foo(string str, int i, bool b, DateTime d, string str2) { ..... } } I'm creating a Foo with AutoFixture: var foo = fixture.Create<Foo>(); but I want AutoFixture to provide a known value for the str2 parameter and use the default behavior for every other parameter. I tried implementing a SpecimenBuilder but I can't find a way to get the metadata associated with the request to know that I'm being called from the Foo constructor. Is there any way to

How can I use custom ISpecimenBuilders with OmitOnRecursionBehavior?

折月煮酒 提交于 2020-01-01 05:24:11
问题 How can I use custom ISpecimenBuilder instances along with the OmitOnRecursionBehavior which I want applied globally to all fixture-created objects? I'm working with an EF Code First model with a foul-smelling circular reference that, for the purposes of this question, cannot be eliminated: public class Parent { public string Name { get; set; } public int Age { get; set; } public virtual Child Child { get; set; } } public class Child { public string Name { get; set; } public int Age { get;

Applying DRY to Autofixture “Build” statements

懵懂的女人 提交于 2019-12-30 02:01:14
问题 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>

Handling specimen creation inconsistencies between AutoFixture and Moq

♀尐吖头ヾ 提交于 2019-12-29 07:52:30
问题 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

CreateMany where only 1 element contains certain value

浪子不回头ぞ 提交于 2019-12-24 13:18:21
问题 This seems to be something lacking in every blog post of piece of documentation I read. How can I CreateMany<T> where only 1 of the elements in the collection contains an Id of something I specify? I want to pull this back into a customization. I tried many variations of something like this: public class OrdersCustomizations : ICustomization { public void Customize(IFixture fixture) { var single = fixture.Build<Order>().With(x => x.Id, 10).Create(); var many = fixture.Build<Order>()

Autofixture configure relationship between parent and child

坚强是说给别人听的谎言 提交于 2019-12-24 07:27:11
问题 It seems like I can't find an easy way to create relationship between parent and child using autofixture. Let is say I have a class Order and OrderLine and OrderLine is linked with Order by OrderId . Now I have a list of Order s and I want that each order inside the list have a few OrderLine and OrderLine should have the same OrderId as per the Order class. How to configure this relationship? fixture.AddManyTo(orderlist) adds many orders with many OrderLine s but they are all random Id s and

In code vs in script data generation for integration tests [closed]

五迷三道 提交于 2019-12-24 02:55:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . This question is very likely to be an opinion based. Yet I'm sure that opinion backed up with solid arguments paves the way to sound decisions. I do like to generate DB state using Autofixture. I sincerely hate writing SQL scripts and then coding expectations 'magically'

Autofixture generate collection without duplicates (by ID)

ぃ、小莉子 提交于 2019-12-23 22:35:45
问题 I have following classes: public class Foo { public List<DescriptionInfo> Descriptions { get; set; } } public class DescriptionInfo { public int LanguageId { get; set; } public string Value { get; set; } } I want to create Foo instance, using Autofixture. However, the LanguageId must come from a predefined list. Therefore I've created following customization: public class LanguageIdSpecimenBuilder : ISpecimenBuilder { private static readonly List<int> LanguageIds = new List<int> { 1, 2, 666,