我正在尝试为此设置单元测试。但我见过的每一个实际有效的例子都是这种设置。它将以这种方式工作。
//Setup DBContext
public MyContext(DbConnection connection) : base(connection, true){}
//Have some service that gets passed the context etc..
public class SomeService()
{
public SomeService(MyContext passedContext){
Context = passedContext;
}
public MyContext Context {get;set;}
public Book GetBook(int id){
return Context.Books.Find(id);
}
}但是我的设置方式是这样的,我不知道如何在不破坏所有东西的情况下正确地完成它
public class SomeService()
{
public async Task<Book> GetBook(int id){
using(var context = new MyContext()
{
return await context.FindAsync(id);
}
}
}那么,我如何才能在没有上下文属性和传递上下文的情况下对此进行测试。因为根据我所读到的,我不能异步操作,因为DBContext不是线程安全的。但我也不能费力地测试它,除非我通过了所有来自努力的正确上下文。
不会起作用,因为我在每个服务方法上都使用了using。
发布于 2017-02-24 00:14:40
Asyns在这里不是问题。请参考这个问题来了解如何实现工厂方法:What happens to using statement when I move to dependency injection
https://stackoverflow.com/questions/42419899
复制相似问题