我想单元测试的方法在某个时候会转到此代码。
var resourceOwnerToken =
new TokenClient(_tokenClientEndpoint, MobileClient.ClientId, MobileClient.ClientSecret);
IdentityModel.Client.TokenResponse tokenResponse =
await resourceOwnerToken.RequestResourceOwnerPasswordAsync(model.Phone, model.Password);
RequestResourceOwnerPasswordAsync是库中的静态扩展方法。
public static Task<TokenResponse> RequestResourceOwnerPasswordAsync
(this TokenClient client, string userName, string password, string scope = null,
object extra = null, CancellationToken cancellationToken = default(CancellationToken));
当我开始测试时,令牌响应值为空,所以我收到错误。我试过模拟。设置,但我在扩展方法错误时设置无效。 我应该怎么做才能使令牌响应具有值?
你有两个选择
1( 使用垫片。https://learn.microsoft.com/en-us/visualstudio/test/isolating-code-under-test-with-microsoft-fakes?view=vs-2017
2( 将静态类包装在接口中。
我通常做第二个,因为我经常发现垫片的维护比包装类的开销更烦人。