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

  • SEARCH
  • Home
  • 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 7909371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:23:08+00:00 2026-06-03T12:23:08+00:00

We find ourselves coding repetitive fixture/mock setups in many test-cases – like this case:

  • 0

We find ourselves coding repetitive fixture/mock setups in many test-cases – like this case:

var fixture = new Fixture().Customize(new AutoMoqCustomization());
var encodingMock = fixture.Freeze<Mock<IEncodingWrapper>>();
var httpClientMock = fixture.Freeze<Mock<IHttpWebClientWrapper>>();
var httpResponseMock = fixture.Freeze<Mock<IHttpWebResponseWrapper>>();
var httpHeaderMock = fixture.Freeze<Mock<IHttpHeaderCollectionWrapper>>();
var etag = fixture.CreateAnonymous<string>();
byte[] data = fixture.CreateAnonymous<byte[]>();
Stream stream =  new MemoryStream(data);

encodingMock.Setup(m => m.GetBytes(It.IsAny<string>())).Returns(data);
httpHeaderMock.SetupGet(m => m[It.IsAny<string>()]).Returns(etag).Verifiable();
httpClientMock.Setup(m => m.GetResponse()).Returns(httpResponseMock.Object);
httpResponseMock.Setup(m => m.StatusCode).Returns(HttpStatusCode.OK);
httpResponseMock.SetupGet(m => m.Headers).Returns(httpHeaderMock.Object);
httpResponseMock.Setup(m => m.GetResponseStream()).Returns(stream);

As per the idea that the tests should be self-contained and readable from start to end we dont use magical Setup/Teardown methods.

Can we in any way (AutoFixture customizations, helper methods) reduce the “grunt work” of these tests?

  • 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-03T12:23:09+00:00Added an answer on June 3, 2026 at 12:23 pm

    You can create a composite Customization that will customize the fixture by using all contained customizations.

    public class HttpMocksCustomization : CompositeCustomization
    {
        public HttpMocksCustomization()
            : base(
                new AutoMoqCustomization(),
                new HttpWebClientWrapperMockCustomization(),
                new HttpWebResponseWrapperMockCustomization()
                // ...
                )
        {
        }
    }
    

    Each customization can be defined as follow:

    public class HttpWebClientWrapperMockCustomization : ICustomization
    {
        public void Customize(IFixture fixture)
        {
            var mock = new Mock<IHttpWebClientWrapper>();
            mock.Setup(m => m.GetResponse()).Returns(httpResponseMock.Object);
    
            fixture.Inject(mock);
        }
    }
    
    public class HttpWebResponseWrapperMockCustomization : ICustomization
    {
        public void Customize(IFixture fixture)
        {
            var mock = new Mock<IHttpWebResponseWrapper>();
            mock.Setup(m => m.StatusCode).Returns(HttpStatusCode.OK);
    
            fixture.Inject(mock);
        }
    }
    
    // The rest of the Customizations.
    

    Then inside the test method you can do this:

    var fixture = new Fixture().Customize(new HttpMocksCustomization());
    

    That way, when you request a Mock instance you don’t have to repeat the setup steps. The one we customized earlier will be returned:

    var httpClientMock = fixture.Freeze<Mock<IHttpWebClientWrapper>>();
    

    However, if you use xUnit.net, things can be simplified even further.

    You can create an AutoDataAttribute-derived type to provide auto-generated data specimens generated by AutoFixture as an extention to xUnit.net’s Theory attribute:

    public class AutoHttpMocksDataAttribute : AutoDataAttribute
    {
        public AutoHttpMocksDataAttribute()
            : base(new Fixture().Customize(new HttpMocksCustomization()))
        {
        }
    }
    

    Then, in your test method you can pass the Mocks as arguments:

    [Theory, AutoHttpMocksData]
    public void MyTestMethod([Freeze]Mock<IHttpWebClientWrapper> httpClientMock, [Freeze]Mock<IHttpWebResponseWrapper> httpResponseMock)
    {
        // ...
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many times we find ourselves working on a problem, only to figure out the
Find and replace scope can be limited like this: :16,256s/search_term/replacement/gc I don't want to
We unfortunately find ourselves having to support our product in IE 6 because some
I find this pretty confusing. When you want to rotate a view, it's going
I find myself creating Converters often and would like to be able to: right-click
I have a table storing millions of rows. It looks something like this: Table_Docs
I find myself writing this class often in my python code when I need
I find it hard to believe this hasn't been asked before, but it doesn't
I find @RequestMapping is very usable in the controller class. This annotation based controller
Cannot find any menu item to do this. Is it doable?

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.