我是新来AutoFixture,我试图创建在球队少TDD倾斜的开发者我的测试情况下友好的扩展送入MVC控制器通过AutoFixture。下面是代码:管理服务的通用测试帮手
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”它注入了就好了一个实例,它肯定叫我主张对方法。当我检查的“对”的方法似乎是同一版本作为一个注入控制器创建的模拟,但它不会Verif
Y!
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 =() =>
{
};
}
我努力寻找做类似的东西别人的任何例子,我知道我肯定做一些愚蠢的事在这里,但在我的脑海这个测试应该通过?
这完全不是那么回事。应在“静态ISomeService SomeService”是“静态模拟 SomeService”,还是应该the'For”方法的变化是一个真正的实例 –
Grace
@Grace:它应该是'模拟'固定 –
如果什么。测试客户端调用'For'方法之前读取'ControllerUnderTest'财产 –