This project is read-only.

Freeze with OmitAutoProperties

Mar 18, 2010 at 9:44 PM

Hi again

I'm trying to do somthing like this:

User creator = Fixture.Build<User>().OmitAutoProperties().Freeze<User>();

But the method is not available on the ObjectBuilder.

Is it by design? 

And another small question... is there a way to set OmitAutoProperties as the default way of building?



Mar 18, 2010 at 9:58 PM

You can do this instead:

User creator = fixture.Freeze<User>(ob => ob.OmitAutoProperties());

For AutoFixture 1.0 there's no way to switch off AutoProperties for all types. If you really need this feature now, feel free to create an issue in the Issue Tracker, or better yet: send us a patch :)

In the bright and shiny new world of AutoFixture 2.0, you can compose the engine in more or less any way you would like, so it would be trivial to never turn on AutoProperties.

Mar 19, 2010 at 1:35 PM

Thank you again for your quick reply!

I will gladly try to implement this and send you a patch :)

I've been looking through the sources a little now. My first thought was to make a CustomizeAll method.

I guess, I'll have to distribute a kind of global BuilderTransform around to everywhere the ObjectBuilders are created. Which are a lot of places it seems :)

Does that sound right to you?

Mar 19, 2010 at 3:47 PM

Hmm... no that will not do. I'll take another approach :)

Mar 19, 2010 at 8:37 PM

If you choose to take on writing a patch for this, be adviced that it only makes sense to do so against the Release 1.1 branch, and that your work may not be carried forward, because AutoFixture 2.0, due to the design of its kernel, will have this feature included by default. However, if done well I can see much value in this feature, so we might consider carrying the unit tests and the API forward and then re-implement the feature with the new kernel.

That said, this feature would not be particularly difficult to implement in Release 1.1. If you examine ObjectBuilder<T>, you will find a single boolean flag that controls AutoProperties. All you need to do is to consistently set the value of this flag to true. One key to that goes through the internal class CustomizedObjectFactory. Since it's internal, you can more or less do with it what you want, as long as you don't break any tests :)

I'd suggest adding a boolean property (OmitAutoProperties) on Fixture with a default value of false. Setting this to true will disable AutoProperties for all types. However, if you do that, I think that an explicit opt-in mechanism would be in order. Right now ObjectionBuilder<T> has the OmitAutoProperties method, and I think that it should have a similar WithAutoProperties method which can be used to opt-in if AutoProperties are switched off by default.

Please let me know if you have any questions.

Mar 20, 2010 at 11:02 AM

A little delayed, but I have now created an issue and uploaded a patch. 

I had implemented it against trunk initially, but it is now reimplemented in 1.1. 

I hope it's usable... :)

I know it will not be in there for long, and I really look forward to the shininess of 2.0! 

Mar 20, 2010 at 11:02 AM

And the issue: