Can AutoFixture execute a delegate at object creation time?

前端 未结 2 1198
清酒与你
清酒与你 2020-12-11 05:34

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

2条回答
  •  执念已碎
    2020-12-11 05:54

    There's a useful discussion on this topic on the AutoFixture CodePlex site.

    I believe my postprocessor Customization linked over there should help you. Example usage:

    class AutoControllerDataAttribute : AutoDataAttribute
    {  
        public AutoControllerDataAttribute()
            : this( new Fixture() )
        {
        }
    
        public AutoControllerDataAttribute( IFixture fixture )
            : base( fixture )
        {
            fixture.Customize( new AutoMoqCustomization() );
            fixture.Customize( new ApplyControllerContextCustomization() );
        }
    
        class ApplyControllerContextCustomization : PostProcessWhereIsACustomization
        {
            public ApplyControllerContextCustomization()
                : base( PostProcess )
            {
            }
    
            static void PostProcess( Controller controller )
            {
                controller.FakeControllerContext();
                // etc. - add stuff you want to happen after the instance has been created
    

提交回复
热议问题