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 8631709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:15:18+00:00 2026-06-12T09:15:18+00:00

I am using AutoFixture to try to test my controllers for a WebApi site.

  • 0

I am using AutoFixture to try to test my controllers for a WebApi site. I am using the AutoData feature with Moq as noted on Ploeh’s blog.

My controller takes an IDepartmentManager in the constructor. Here is my test:

[Theory, AutoMoqData]
public void GetCallsManagerCorrectly(
    [Frozen]Mock<IDepartmentManager> departmentManagerMock,
    DepartmentsController sut)
{
    // Fixture setup
    // Exercise system
    sut.Get();
    // Verify outcome
    departmentManagerMock.Verify(d => d.GetAllDepartments(), Times.Exactly(1));
    // Teardown
}

When I run this test it fails with the following:

GetCallsManagerCorrectly has failed:
System.InvalidOperationException : An exception was thrown while
getting data for theory
Provision.Tests.WebApiControllerTests.DepartmentControllerTests.GetCallsManagerCorrectly:
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. —> System.ArgumentException: Only
‘http’ and ‘https’ schemes are allowed. Parameter name: value at
System.Net.Http.HttpRequestMessage.set_RequestUri(Uri value)

First of all, is this still a valid and recommended way to write these tests? I love how small it makes everything.

Secondly, what should I do to fix this? If I change my test to this:

[Theory, AutoMoqData]
public void GetCallsManagerCorrectly(
    [Frozen]Mock<IDepartmentManager> departmentManagerMock)
{
    // Fixture setup
    DepartmentsController sut =
        new DepartmentsController(departmentManagerMock.Object);
    // Exercise system
    sut.Get();
    // Verify outcome
    departmentManagerMock.Verify(d => d.GetAllDepartments(), Times.Exactly(1));
    // Teardown
}

it passes, but then I lose the ability to have the controller built up automatically and still be ok if I add parameters to the constructor.

  • 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-12T09:15:19+00:00Added an answer on June 12, 2026 at 9:15 am

    This is definitely the recommended way to write tests with AutoFixture. The issue is pretty easy to fix.

    Instead of implementing the [AutoMoqData] attribute as described in the blog post, I’d recommend creating a slightly different attribute and Customization – a set that basically will act as a set of conventions for an entire unit test project. I always do this, and I always go to great lengths to have only a single set of conventions for a single unit test project. A single set of conventions help me keep my tests (and the SUTs) consistent.

    public class AutoMyWebApiDataAttribute : AutoDataAttribute
    {
        public AutoMyWebApiDataAttribute()
            : base(new Fixture().Customize(new MyWebApiCustomization()))
        {
        }
    }
    

    The MyWebApiCustomization could be defined like this:

    public class MyWebApiCustomization : CompositeCustomization
    {
        public MyWebApiCustomization()
            : base(
                new HttpSchemeCustomization(),
                new AutoMoqCustomization(),
            )
        {
        }
    
        private class HttpSchemeCustomization : ICustomization
        {
            public void Customize(IFixture fixture)
            {
                fixture.Inject(new UriScheme("http"));
            }
        }
    }
    

    Note the additional HttpSchemeCustomization class – that should do the trick.

    Please note that the order of Customizations matters.

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

Sidebar

Related Questions

I've just created my first test using AutoFixture. The SUT has the following constructor.
Using codeigniter 2.1.2, I have a form view loaded through a controller that takes
I recently started using AutoFixture library (http://autofixture.codeplex.com/) for Unit Testing and I quite like
I'm using Autofixture as a SUT factory and am having difficulty Freezing null instances.
I have a fair amount of experience using MOQ, while I've recently have stumbled
Using ASP.NET MVC 4 RTW, how do I log MVC controller actions and parameters
Using Spring MVC, do interceptors only intercept defined controller mappings or can they also
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using a populated Table Type as the source for a TSQL-Merge. I want to
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question

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.