本文整理汇总了C#中Container.Verify方法的典型用法代码示例。如果您正苦于以下问题:C# Container.Verify方法的具体用法?C# Container.Verify怎么用?C# Container.Verify使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container.Verify方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Analyze_TwoSingletonRegistrationsForTheSameImplementation_ReturnsExpectedWarning
public void Analyze_TwoSingletonRegistrationsForTheSameImplementation_ReturnsExpectedWarning()
{
// Arrange
string expectedMessage1 =
"The registration for IFoo maps to the same implementation and lifestyle as the " +
"registration for IBar does. They both map to FooBar (Singleton).";
string expectedMessage2 =
"The registration for IBar maps to the same implementation and lifestyle as the " +
"registration for IFoo does. They both map to FooBar (Singleton).";
var container = new Container();
container.Register<IFoo, FooBar>(Lifestyle.Singleton);
container.Register<IBar, FooBar>(Lifestyle.Singleton);
container.Verify(VerificationOption.VerifyOnly);
// Act
var results = Analyzer.Analyze(container).OfType<TornLifestyleDiagnosticResult>().ToArray();
// Assert
Assert.AreEqual(2, results.Length, Actual(results));
Assert_ContainsDescription(results, expectedMessage1);
Assert_ContainsDescription(results, expectedMessage2);
}
示例2: Verify_TValueTypeParameter_ThrowsExpectedException
public void Verify_TValueTypeParameter_ThrowsExpectedException()
{
// Arrange
string expectedString = string.Format(@"
The constructor of type {0}.{1} contains parameter 'intArgument' of type Int32 which can not
be used for constructor injection because it is a value type.",
this.GetType().Name,
typeof(TypeWithSinglePublicConstructorWithValueTypeParameter).Name)
.TrimInside();
var behavior = new Container().Options.DependencyInjectionBehavior;
var constructor =
typeof(TypeWithSinglePublicConstructorWithValueTypeParameter).GetConstructors().Single();
var consumer = new InjectionConsumerInfo(
constructor.DeclaringType,
constructor.DeclaringType,
constructor.GetParameters().Single());
try
{
// Act
behavior.Verify(consumer);
// Assert
Assert.Fail("Exception expected.");
}
catch (ActivationException ex)
{
AssertThat.StringContains(expectedString, ex.Message);
}
}
示例3: Analyze_ComponentDependingOnUnregisteredEnumerable_ReturnsWarning
public void Analyze_ComponentDependingOnUnregisteredEnumerable_ReturnsWarning()
{
// Arrange
var container = new Container();
container.Options.ResolveUnregisteredCollections = true;
container.Register<Consumer<IEnumerable<ILogger>>>();
container.Verify();
// Act
var results =
Analyzer.Analyze(container).OfType<ContainerRegisteredServiceDiagnosticResult>().ToArray();
// Assert
Assert.AreEqual(1, results.Length);
Assert.AreEqual(
"Consumer<IEnumerable<ILogger>> depends on container-registered type IEnumerable<ILogger>.",
results.Single().Description,
"It's important for this warning to be signaled, because developers sometimes misconfigure " +
"the container in a way that their registered collection doesn't get injected, because " +
"they accidentally depend on an other type. Simple Injector will in that case silently " +
"inject an empty collection.");
}
示例4: Scenario1
public void Scenario1()
{
// Arrange
var container = new Container();
var plugins = new KeyedRegistrations<string, IPlugin>(container);
container.Options.DependencyInjectionBehavior = new NamedDependencyInjectionBehavior(
container.Options.DependencyInjectionBehavior,
(serviceType, name) => plugins.GetRegistration(name));
plugins.Register(typeof(Plugin1), "1");
plugins.Register<Plugin2>("2");
plugins.Register(typeof(Plugin3), "3", Lifestyle.Singleton);
plugins.Register(() => new Plugin("4"), "4");
plugins.Register(() => new Plugin("5"), "5", Lifestyle.Singleton);
container.RegisterCollection<IPlugin>(plugins);
container.RegisterDecorator(typeof(IPlugin), typeof(PluginDecorator), Lifestyle.Singleton,
context => context.ImplementationType == typeof(Plugin3));
container.RegisterSingleton<Func<string, IPlugin>>(key => plugins.GetInstance(key));
container.Verify();
// Act
var actualPlugins1 = container.GetAllInstances<IPlugin>().ToArray();
var actualPlugins2 = container.GetAllInstances<IPlugin>().ToArray();
var factory = container.GetInstance<Func<string, IPlugin>>();
var consumer = container.GetInstance<NamedPluginConsumer>();
// Assert
AssertThat.IsInstanceOfType(typeof(Plugin1), actualPlugins1[0]);
AssertThat.IsInstanceOfType(typeof(Plugin2), actualPlugins1[1]);
AssertThat.IsInstanceOfType(typeof(PluginDecorator), actualPlugins1[2]);
AssertThat.IsInstanceOfType(typeof(Plugin), actualPlugins1[3]);
AssertThat.IsInstanceOfType(typeof(Plugin), actualPlugins1[4]);
AssertThat.IsInstanceOfType(typeof(Plugin1), factory("1"));
AssertThat.IsInstanceOfType(typeof(Plugin2), factory("2"));
AssertThat.IsInstanceOfType(typeof(PluginDecorator), factory("3"));
AssertThat.IsInstanceOfType(typeof(Plugin), factory("4"));
AssertThat.IsInstanceOfType(typeof(Plugin), factory("5"));
Assert.AreNotSame(actualPlugins1[0], actualPlugins2[0]);
Assert.AreNotSame(actualPlugins1[1], actualPlugins2[1]);
Assert.AreSame(actualPlugins1[2], actualPlugins2[2]);
Assert.AreNotSame(actualPlugins1[3], actualPlugins2[3]);
Assert.AreSame(actualPlugins1[4], actualPlugins2[4]);
AssertThat.IsInstanceOfType(typeof(Plugin1), consumer.Plugin1);
AssertThat.IsInstanceOfType(typeof(Plugin2), consumer.Plugin2);
AssertThat.IsInstanceOfType(typeof(PluginDecorator), consumer.Plugin3);
AssertThat.IsInstanceOfType(typeof(Plugin), consumer.Plugin4);
}
示例5: Analyze_WithVerfiedContainer_Succeeds
public void Analyze_WithVerfiedContainer_Succeeds()
{
// Arrange
var verfiedContainer = new Container();
verfiedContainer.Verify();
// Act
Analyzer.Analyze(verfiedContainer);
}
示例6: Verify_WithNoHttpContext_Succeeds
public void Verify_WithNoHttpContext_Succeeds()
{
// Arrange
Assert.IsNull(HttpContext.Current, "Test setup failed.");
var container = new Container();
container.RegisterPerWebRequest<ConcreteCommand>();
// Act
container.Verify();
}
示例7: Verify_RegistrationWithSingletonDependingOnInstancePerDependencyInstance_Succeeds
public void Verify_RegistrationWithSingletonDependingOnInstancePerDependencyInstance_Succeeds()
{
// Arrange
var container = new Container();
var lifestyle = new InstancePerDependencyLifestyle();
container.RegisterSingleton<CommandWithILoggerDependency>();
container.Register<ILogger, NullLogger>(new InstancePerDependencyLifestyle());
// Act
container.Verify();
}
示例8: Ctor_WithLockedContainer_LeavesContainerLocked
public void Ctor_WithLockedContainer_LeavesContainerLocked()
{
var container = new Container();
// This locks the container
container.Verify();
Assert.IsTrue(container.IsLocked, "Test setup failed.");
// Act
new ContainerDebugView(container);
// Assert
Assert.IsTrue(container.IsLocked);
}
示例9: Ctor_WithLockedContainer_ReturnsAnItemWithTheRegistrations
public void Ctor_WithLockedContainer_ReturnsAnItemWithTheRegistrations()
{
var container = new Container();
// This locks the container
container.Verify();
var debugView = new ContainerDebugView(container);
// Act
var registrationsItem = debugView.Items.Single(item => item.Name == "Registrations");
// Assert
AssertThat.IsInstanceOfType(typeof(InstanceProducer[]), registrationsItem.Value);
}
示例10: Analyze_ComponentDependingOnUnregisteredConcreteType_ReturnsSeverityInformation
public void Analyze_ComponentDependingOnUnregisteredConcreteType_ReturnsSeverityInformation()
{
// Arrange
var container = new Container();
container.Register<Consumer<ConcreteShizzle>>();
container.Verify();
// Act
var result = Analyzer.Analyze(container).OfType<ContainerRegisteredServiceDiagnosticResult>().First();
// Assert
Assert.AreEqual(DiagnosticSeverity.Information, result.Severity);
}
示例11: Analyze_TransientRegistrationForDisposableComponent_ReturnsSeverityWarning
public void Analyze_TransientRegistrationForDisposableComponent_ReturnsSeverityWarning()
{
// Arrange
var container = new Container();
container.Register<IPlugin, DisposablePlugin>();
container.Verify(VerificationOption.VerifyOnly);
// Act
var result = Analyzer.Analyze(container).OfType<DisposableTransientComponentDiagnosticResult>().First();
// Assert
Assert.AreEqual(DiagnosticSeverity.Warning, result.Severity);
}
示例12: Analyze_SameInstanceRegisteredForTwoInterfacesUsingRegisterSingleton_DoesNotResultInAWarning
public void Analyze_SameInstanceRegisteredForTwoInterfacesUsingRegisterSingleton_DoesNotResultInAWarning()
{
var container = new Container();
var fooBar = new FooBar();
container.RegisterSingleton<IFoo>(fooBar);
container.RegisterSingleton<IBar>(fooBar);
container.Verify(VerificationOption.VerifyOnly);
// Act
var results = Analyzer.Analyze(container).OfType<TornLifestyleDiagnosticResult>();
// Assert
Assert.IsFalse(results.Any(), Actual(results));
}
示例13: Analyze_TwoSingletonRegistrationsForTheSameImplementation_ReturnsSeverityWarning
public void Analyze_TwoSingletonRegistrationsForTheSameImplementation_ReturnsSeverityWarning()
{
// Arrange
var container = new Container();
container.Register<IFoo, FooBar>(Lifestyle.Singleton);
container.Register<IBar, FooBar>(Lifestyle.Singleton);
container.Verify(VerificationOption.VerifyOnly);
// Act
var result = Analyzer.Analyze(container).OfType<TornLifestyleDiagnosticResult>().First();
// Assert
Assert.AreEqual(DiagnosticSeverity.Warning, result.Severity);
}
示例14: Analyze_ConfigurationWithCollectionRegistration_DoesNotProduceAnyWarnings
public void Analyze_ConfigurationWithCollectionRegistration_DoesNotProduceAnyWarnings()
{
// Arrange
var container = new Container();
container.RegisterCollection<IPlugin>(new[] { typeof(SomePluginImpl) });
container.Verify();
// Act
var results = Analyzer.Analyze(container);
// Assert
Assert.AreEqual(0, results.Length,
"Since the SomePluginImpl is registered explicitly using the RegisterAll method, this " +
"registration should contain no warnings, but the following warnings are present: \"" +
string.Join(" ", results.Select(result => result.Description)) + "\"");
}
示例15: BuildMediator
private static IMediator BuildMediator()
{
var container = new Container();
var assemblies = GetAssemblies().ToArray();
container.RegisterSingleton<IMediator, Mediator>();
container.Register(typeof(IRequestHandler<,>), assemblies);
container.Register(typeof(IAsyncRequestHandler<,>), assemblies);
container.RegisterCollection(typeof(INotificationHandler<>), assemblies);
container.RegisterCollection(typeof(IAsyncNotificationHandler<>), assemblies);
container.RegisterSingleton(Console.Out);
container.RegisterSingleton(new SingleInstanceFactory(container.GetInstance));
container.RegisterSingleton(new MultiInstanceFactory(container.GetAllInstances));
container.Verify();
var mediator = container.GetInstance<IMediator>();
return mediator;
}