我正在使用AutoFixture尝试测试我的控制器的WebApi网站。我正在使用Moq的AutoData功能,如Ploeh's blog所述。Autofixture和WebApi控制器
我的控制器在构造函数中需要一个IDepartmentManager。下面是我的测试:
[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
}
当我运行这个测试失败,出现以下:
GetCallsManagerCorrectly失败:
System.InvalidOperationException:已引发异常,同时 获取数据的理论 Provision.Tests.WebApiControllerTests.DepartmentControllerTests.GetCallsManagerCorrectly: System.Reflection.TargetInvocationException:异常已被调用的目标引发 。 ---> System.ArgumentException:只允许使用 'http'和'https'方案。参数名:在 System.Net.Http.HttpRequestMessage.set_RequestUri(URI值)
首先价值,这是仍然是有效的,建议的方式来写这些测试?我喜欢它让所有事物变得多小。
其次,我应该怎么做才能解决这个问题?如果我改变我的测试,以这样的:
[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
}
它通过,但后来我失去了让控制器自动建立起来,仍然是确定的,如果我添加参数构造函数的能力。
感谢您的快速和正确的回应。很棒。 –
@BrianMcCord和其他人很确定上面的'HttpSchemeCustomization'现在是AF V3 +中的固有功能。 –