Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:35:30+00:00 2026-06-01T17:35:30+00:00

I’m looking to customize the creation-time behavior of AutoFixture such that I can set

  • 0

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 test so it can further customize the fixture if necessary.)

What I’d like to do is automatically associate that user with an anonymous collection by its Id at creation time, but I can’t do this as-is because Id has not been generated by the time I hand the return value back to the unit test proper. Here’s the sort of thing I’m trying to do:

public static ObjectBuilder<User> BuildUserIn(this Fixture f, UserCollection uc)
{
   return f.Build<User>()
           .With(u => u.IsDeleted, false);
           .AfterCreation(u =>
            {
               var relation = f.Build<UserCollectionMembership>()
                               .With(ucm => ucm.UserCollectionId, uc.Id)
                               .With(ucm => ucm.UserId, u.Id)
                               .CreateAnonymous();
               Repository.Install(relation);
            }
}

Is something like this possible? Or perhaps there is a better way to accomplish my goal of creating an anonymous object graph?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:35:32+00:00Added an answer on June 1, 2026 at 5:35 pm

    For the Build method, this isn’t possible, and probably never will be, because there are much better options available.

    First of all, it should never be necessary to write static helper methods around the Build method. The Build method is for truly one-off initializations where one needs to define property or field values before the fact.

    I.e. imagine a class like this:

    public class MyClass
    {
        private string txt;
    
        public string SomeWeirdText
        {
            get { return this.txt; }
            set
            {
                if (value != "bar")
                    throw new ArgumentException();
                this.txt = value;
            }
        }
    }
    

    In this (contrived) example, a straight fixture.CreateAnonymous<MyClass> is going to throw because it’s going to attempt to assign something other than “bar” to the property.

    In a one-off scenario, one can use the Build method to escape this problem. One example is simply to set the value explicitly to “bar”:

    var mc =
        fixture.Build<MyClass>().With(x => x.SomeWeirdText, "bar").CreateAnonymous();
    

    However, even easier would be to just omit that property:

    var mc =
        fixture.Build<MyClass>().Without(x => x.SomeWeirdText).CreateAnonymous();
    

    However, once you start wanting to do this repeatedly, there are better options. AutoFixture has a very sophisticated and customizable engine for defining how things get created.

    As a start, one could start by moving the omission of the property into a customization, like this:

    fixture.Customize<MyClass>(c => c.Without(x => x.SomeWeirdText));
    

    Now, whenever the fixture creates an instance of MyClass, it’s just going to skip that property altogether. You can still assign a value afterwards:

    var mc = fixture.CreateAnonymous<MyClass>();
    my.SomeWeirdText = "bar";
    

    If you want something more sophisticated, you can implement a custom ISpecimenBuilder. If you want to run some custom code after the instance has been created, you can decorate your own ISpecimenBuilder with a Postprocessor and supply a delegate. That might look something like this:

    fixture.Customizations.Add(
        new Postprocessor(yourCustomSpecimenBuilder, obj =>
            { */ do something to obj here */ }));
    

    (BTW, are you still on AutoFixture 1.0? IIRC, there hasn’t been an ObjectBuilder<T> around since then…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.