我是 AutoFixture 的新手,我正在尝试在我的测试上下文中为团队中不太倾向于 TDD 的开发人员创建一个友好的扩展。这是代码:
public class HomeController : Controller
{
private readonly ISomeService _someService;
public HomeController(ISomeService someService)
{
_someService = someService;
}
public ActionResult Index()
{
_someService.SomeMethod();
return View("Index");
}
}
public class ControllerContext<T> where T : Controller
{
protected static T ControllerUnderTest;
private static IFixture _fixture;
public ControllerContext()
{
_fixture = new Fixture().Customize(new AutoMoqCustomization());
_fixture.Customize<ControllerContext>(c => c.Without(x => x.DisplayMode));
ControllerUnderTest = _fixture.Create<T>();
}
protected static Mock<TDouble> For<TDouble>() where TDouble : class
{
//var mock = _fixture.Create<TDouble>();
var mock = _fixture.Create<Mock<TDouble>>();
return mock;
}
}
所以扩展就是For
方法——当我检查ControllerUnderTest
哪个注入了“ISomeService”时,它有一个注入的实例很好,它肯定会调用我断言的方法。当我检查在“For”方法中创建的模拟时,它似乎与注入控制器的模拟版本相同,但它不会Verif
!
public class EXAMPLE_When_going_to_home_page : ControllerContext<HomeController>
{
Because of = () =>
{
ControllerUnderTest.Index();
};
It should_do_something = () =>
{
//This throws a 'Invocation was not performed'
For<ISomeService>().Verify(x => x.SomeMethod());
};
Establish context = () =>
{
};
}
我正在努力寻找任何人做类似事情的例子,我知道我在这里肯定做了一些愚蠢的事情,但在我看来,这个测试应该通过吗?