简体   繁体   English

使用MicrosoftFakes为UserManager设置假的扩展方法

[英]Setting a fake extension method for UserManager using MicrosoftFakes

I'm trying to write some unit tests for my ASP.NET MVC project to test my controller which works with UserManager and uses its extension method FindById(string). 我正在尝试为ASP.NET MVC项目编写一些单元测试,以测试与UserManager一起使用并使用其扩展方法FindById(string)的控制器。 I generated fakes for the Microsoft.AspNet.Identity where the method is stored so I got ShimUserManagerExtensions but I was not able to actually assign a new behaviour because I have zero idea how to do it and I wasn't able to find it anywhere. 我为存储该方法的Microsoft.AspNet.Identity生成了伪造品,因此我得到了ShimUserManagerExtensions,但是由于我对如何执行此操作的想法不为零,而且无法在任何地方找到它,因此我无法实际分配新行为。

Here's what I get when I'm trying to access the method. 这是我尝试访问该方法时得到的信息。

图片

I've tried to assign something to it as follows 我试图给它分配一些东西,如下

ShimUserManagerExtensions.FindByIdOf2UserManagerOfM0M1M1 = () => new ApplicationUser();

But I was told that FindByIdOf2UserManagerOfM0M1M1 is a method group and I can't assign anything to it. 但是我被告知FindByIdOf2UserManagerOfM0M1M1是一个方法组,我无法为其分配任何内容。 As I understand, that means there are some overloaded methods and I have to assign my behaviour to the exact one. 据我了解,这意味着有些重载方法,我必须将自己的行为分配给确切的行为。 However, I don't know how to do it. 但是,我不知道该怎么做。

Could someone please give me a hint or perhaps just tell how to formulate the query for google so I'll find any information? 有人可以给我一个提示,或者只是告诉我如何制定Google查询,以便我找到任何信息?

I had the same problem and solved it like this: 我有同样的问题,并像这样解决了它:

ShimUserManagerExtensions
            .FindByIdOf2UserManagerOfM0M1M1<ApplicationUser, string>(
                (m, u) => new ApplicationUser());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM