Automocking Web Api 2 controller

前端 未结 3 2070
梦谈多话
梦谈多话 2020-12-13 04:19

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 thi

3条回答
  •  悲&欢浪女
    2020-12-13 05:03

    Based on Nikos' answer:

    This is a more generic way of using this customization where the controller type can be supplied and the Customization can be used for any controller

    internal class WebApiCustomization : ICustomization
        where TControllerType : ApiController
        {
            public void Customize(IFixture fixture)
            {
                fixture.Customize(c => c
                    .Without(x => x.Content)
                    .Do(x => x.Properties[HttpPropertyKeys.HttpConfigurationKey] =
                        new HttpConfiguration()));
    
                fixture.Customize(c => c
                    .OmitAutoProperties()
                    .With(x => x.Request, fixture.Create()));
    }
    }
    

    Then use as follows:

    var fixture = new Fixture().Customize(
        new WebApiCustomization());
    var sut = fixture.Create();
    

提交回复
热议问题