惩戒新的微软实体框架的UserManager身份和RoleManager [英] Mocking new Microsoft Entity Framework Identity UserManager and RoleManager

查看:245
本文介绍了惩戒新的微软实体框架的UserManager身份和RoleManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人拿出的UserManager 一个成功的嘲讽解决方案, RoleManager ?我一直在打我的头撞墙一整天。所有我想要做的是模拟的对象在内存中收集到使用,而不是击中实体框架数据存储。我已经冲刷互联网和尝试使用最小起订量几种不同的方法。

Has anyone come up with a successful mocking solution for UserManager and RoleManager? I have been beating my head against a wall all day. All I want to do is mock the objects to use an in memory collection rather than hitting the Entity Framework data store. I've scoured the internet and tried several different approaches using MOQ.

我的印象是新的东西是更容易测试下。 ?我失去了一些东西。

I was under the impression that the new stuff was much easier to test. Am I missing something?

推荐答案

另外,你可以嘲笑 IUserStore< TUSER> 接口的UserManager 接受作为参数。<​​/ p>

Alternatively, you can mock the IUserStore<TUser> interface that UserManager accepts as an argument.

var userStore = new Mock<IUserStore<ApplicationUser>>();
var userManager = new UserManager(userStore.Object);



由于@Joe Brunscheon在下面的评论指出,检测到的UserManager像IUserPasswordStore等其他接口的支持你也可以起订量的:

As @Joe Brunscheon notes in the comment below, UserManager detects support for other interfaces like IUserPasswordStore, etc. You can also moq those:

var passwordManager = userStore.As<IUserPasswordStore<ApplicationUser>>()
    .Setup(...).Returns(...);

您不必MOQ所有这些一次,你可以起订量起来的通过您的代码下的测试需要。在现实中,EF用来实现IUserStore实现其他接口的UserStore,和的UserManager会做内部检测,看是否这些接口中实现的,因此,所支持的附加功能。幸运的是,起订量让你小样,可以实现多个接口,使用。至于℃的替代品; T>()

You don't have to moq out all of these at once, you can just moq them up as needed by your code-under-test. In reality, the UserStore that EF uses to implement IUserStore implements other interfaces, and UserManager will do internal detection to see if these interfaces are implemented, and therefore, additional features supported. Fortunately, moq lets you mock up a surrogate that can implement many interfaces, using .As<T>().

总之,Microsoft.AspNet.Identity确实给你你需要使用它光秃秃的,没有一个wapper,在你的代码的一切。 T> IUserStore<的传递它们起订量它的单元测试>被增强,以支持通过的UserManager内部检测,其他接口的方法最小起订量。

In short, Microsoft.AspNet.Identity does give you everything you need to use it bare, without a wapper, in your code. As long as you use dependency injection to instantiate your UserManager, you can safely moq it in unit tests by mocking up the interfaces it consumes and passing them via some kind of IUserStore<T> moq that is augmented to support methods on other interfaces internally detected by UserManager.

这篇关于惩戒新的微软实体框架的UserManager身份和RoleManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆