Autofixture and WebApi Controller

I am using AutoFixture to try and test my controllers for the WebApi site. I am using the AutoData function with Moq, as stated on the Ploeh blog .

My controller accepts the 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 failed:
Bug Fix System.InvalidOperationException: An exception was thrown while retrieving data for the Provision.Tests.WebApiControllerTests.DepartmentControllerTests.GetCallsManagerCorrectly: System.Reflection.TargetInvocationException theory: The exception was thrown on the target of the call. ---> System.ArgumentException: Only "http" and "https" schemes are allowed. Parameter name: value in System.Net.Http.HttpRequestMessage.set_RequestUri (Uri value)

First of all, is this really the right and recommended way to write these tests? I like how little he does 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 automatically create the controller and still be fine if I add parameters to the constructor.

+4
source share
1 answer

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

Instead of embedding the [AutoMoqData] attribute, as described in a blog post, I would recommend creating a slightly different attribute and Customization - a set that will basically act as a set of conventions for the entire unit test project. I always do this, and I always try to have only one set of conventions for one unit test project. One set of conventions helps me maintain my tests (and SUT s).

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

MyWebApiCustomization can be defined as follows:

 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 optional HttpSchemeCustomization class - this should do the trick.

Please note that the order of settings matters .

+4
source

Source: https://habr.com/ru/post/1436415/


All Articles