简体   繁体   中英

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). 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.

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. 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?

I had the same problem and solved it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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