在我的代码中,我正在解决以下autofac依赖关系。
public class OAuthTokenProvider: OAuthAuthorizationServerProvider
{
private readonly IContainer _container;
public OAuthTokenProvider()
{
_container = BuildContainer();
}
public static IContainer BuildContainer()
{
var builder = IocConfig.RegisterTypes(new ContainerBuilder());
return builder.Build();
}
public async Task<AuthenticationTicket> CreateAuthenticationTicket(string authenticationType, string username, string clientId)
{
using (var scope = _container.BeginLifetimeScope())
{
var userService = scope.Resolve<IUserService>();
var user = await userService.FindByNameAsync(username);
// code
}
}
}
我正在尝试使用NUnit / MOQ框架对单元CreateCreateTicket方法进行如下测试
[TestMethod]
public async Task Test_Validating_A_Valid_Authentication_Request()
{
var prov = new OAuthTokenProvider();
var cb = new ContainerBuilder();
var userServiceMock = new Mock<IUserService>().Object;
cb.RegisterInstance(userServiceMock).As<IUserService>();
cb.Build();
}
我不确定如何像用户服务和逻辑单元测试这样的依赖关系。有什么想法吗?