使用asp.net身份RTW version。
我需要在事务中执行多个操作,包括UserMananger
函数调用和DbContext
上的其他操作(例如:创建新用户,将其添加到组并执行一些业务逻辑操作)。
我该怎么做?
我的想法如下。
的TransactionScope
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
// Do what I need
if (everythingIsOk) scope.Complete();
}
问题是:UserManager
函数都是异步的,而TransactionScope
并非设计用于async / await。 It seems to be solved in .Net Framework 4.5.1。但是我使用Azure网站来托管我的项目构建,所以我还不能以4.5.1为目标。
数据库交易
public class SomeController : Controller
{
private MyDbContext DbContext { get; set; }
private UserManager<User> UserManager { get; set; }
public AccountController()
{
DbContext = new MyDbContext()
var userStore = new UserStore<IdentityUser>(DbContext);
UserManager = new UserManager<IdentityUser>(userStore);
}
public async ActionResult SomeAction()
{
// UserManager uses the same db context, so they can share db transaction
using (var tran = DbContext.Database.BeginTransaction())
{
try
{
// Do what I need
if (everythingIsOk)
tran.Commit();
else
{
tran.Rollback();
}
}
catch (Exception)
{
tran.Rollback();
}
}
}
}
这似乎有效,但我怎样才能对它进行单元测试?
UserManager<>
构造函数接受IUserStore<>
,因此我可以轻松将其存根。
UserStore<>
构造函数接受DbContext
,不知道我怎么能存根。
答案 0 :(得分:0)
您可以实现自己的测试用户存储,可以为您的单元测试存根。
如果要在测试中使用实际的EF UserStore,那也可以使用,但默认情况下它将使用DefaultConnection字符串创建数据库。如果要确保每个测试都有一个干净的数据库,可以指定一个DatabaseInitializer来始终在测试中删除/重新创建表。