【发布时间】:2018-10-13 12:44:19
【问题描述】:
我要在单元测试中实例化这个类:
public class Customer
{
internal Customer(Guid id) {
// initialize property
}
}
如果我使用 new Customer() 从另一个 (unittests) 程序集实例化测试类,因为我添加了 [assembly: InternalsVisibleTo("MyProject.Tests")]
var sut = new Customer(Guid.NewGuid()); // works
但是当我在另一个(unittest)程序集中设置一个 autofac 容器时
var builder = new ContainerBuilder();
builder.RegisterType<Customer>().AsSelf();
var container = builder.Build();
我无法用 autofac 解决。
var theParam = new NamedParameter("id", Guid.NewGuid());
_sut = container.Resolve<Customer>(theParam); // throws exception
我最好的猜测是内部构造函数不可用。但是在另一个旁边添加[assembly: InternalsVisibleTo("Autofac")] 并没有帮助。
Autofac 抛出的异常是
Autofac.Core.DependencyResolutionException:
An error occurred during the activation of a particular registration. See the inner exception for details.
Registration: Activator = Customer (ReflectionActivator),
Services = [MyProject.Customer],
Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,
Sharing = None,
Ownership = OwnedByLifetimeScope
---> No accessible constructors were found for the type 'MyProject.Customer'.
Autofac 不能处理 internal 构造函数吗?
【问题讨论】:
标签: autofac