-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved type initialization error handling by calling cctor on expre…
…ssion tree build and moving logic to Registration. Also moved tests to new class. #812
- Loading branch information
1 parent
3e752b2
commit b9eb362
Showing
4 changed files
with
226 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
src/SimpleInjector.Tests.Unit/TypeInitializationErrorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
namespace SimpleInjector.Tests.Unit | ||
{ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
public class TopLevelClassWithFailingTypeInitializer | ||
{ | ||
static TopLevelClassWithFailingTypeInitializer() | ||
{ | ||
throw new Exception("<Inner exception>."); | ||
} | ||
} | ||
|
||
// #812 | ||
[TestClass] | ||
public class TypeInitializationErrorTests | ||
{ | ||
[TestMethod] | ||
public void GetInstance_ResolvingATypeWithTypeInitializationException_ThrowsExpressiveException() | ||
{ | ||
// Act | ||
var container = new Container(); | ||
|
||
container.Register<TopLevelClassWithFailingTypeInitializer>(); | ||
|
||
// Act | ||
Action action = () => container.GetInstance<TopLevelClassWithFailingTypeInitializer>(); | ||
|
||
// Assert | ||
AssertThat.ThrowsWithExceptionMessageContains<ActivationException>( | ||
"The type initializer for TopLevelClassWithFailingTypeInitializer threw an " + | ||
"exception. <Inner exception>.", | ||
action); | ||
} | ||
|
||
[TestMethod] | ||
public void GetInstance_ResolvingANestedTypeWithTypeInitializationException_ThrowsExpressiveException() | ||
{ | ||
// Act | ||
var container = new Container(); | ||
|
||
container.Register<NestedClassWithFailingTypeInitializer>(); | ||
|
||
// Act | ||
Action action = () => container.GetInstance<NestedClassWithFailingTypeInitializer>(); | ||
|
||
// Assert | ||
AssertThat.ThrowsWithExceptionMessageContains<ActivationException>( | ||
"The type initializer for " + | ||
$"{nameof(TypeInitializationErrorTests)}.NestedClassWithFailingTypeInitializer " + | ||
"threw an exception. <Inner exception>.", | ||
action); | ||
} | ||
|
||
[TestMethod] | ||
public void GetInstance_ResolvingAGenericNestedTypeWithTypeInitializationException_ThrowsExpressiveException() | ||
{ | ||
// Act | ||
var container = new Container(); | ||
|
||
container.Register(typeof(NestedClassWithFailingTypeInitializer<>)); | ||
|
||
// Act | ||
Action action = () => container.GetInstance<NestedClassWithFailingTypeInitializer<object>>(); | ||
|
||
// Assert | ||
AssertThat.ThrowsWithExceptionMessageContains<ActivationException>( | ||
"The type initializer for " + | ||
$"{nameof(TypeInitializationErrorTests)}.NestedClassWithFailingTypeInitializer<object> " + | ||
"threw an exception. <Inner exception>.", | ||
action); | ||
} | ||
|
||
[TestMethod] | ||
public void GetInstance_ResolvingASingletonWithTypeInitializationException_ThrowsExpressiveException() | ||
{ | ||
// Act | ||
var container = new Container(); | ||
|
||
container.RegisterSingleton<NestedClassWithFailingTypeInitializer>(); | ||
|
||
// Act | ||
Action action = () => container.GetInstance<NestedClassWithFailingTypeInitializer>(); | ||
|
||
// Assert | ||
AssertThat.ThrowsWithExceptionMessageContains<ActivationException>( | ||
"The type initializer for " + | ||
$"{nameof(TypeInitializationErrorTests)}.NestedClassWithFailingTypeInitializer " + | ||
"threw an exception. <Inner exception>.", | ||
action); | ||
} | ||
|
||
[TestMethod] | ||
public void GetInstance_ResolvingADecoratedInstanceWithInitializationException_ThrowsExpressiveException() | ||
{ | ||
// Act | ||
var container = new Container(); | ||
|
||
container.Register<INonGenericService, NestedClassWithFailingTypeInitializer>(); | ||
container.RegisterDecorator<INonGenericService, NonGenericServiceDecorator>(); | ||
|
||
// Act | ||
Action action = () => container.GetInstance<INonGenericService>(); | ||
|
||
// Assert | ||
AssertThat.ThrowsWithExceptionMessageContains<ActivationException>( | ||
"The type initializer for " + | ||
$"{nameof(TypeInitializationErrorTests)}.NestedClassWithFailingTypeInitializer " + | ||
"threw an exception. <Inner exception>.", | ||
action); | ||
} | ||
|
||
[TestMethod] | ||
public void GetInstance_ResolvingADecoratorWithTypeInitializationException_ThrowsExpressiveException() | ||
{ | ||
// Act | ||
var container = new Container(); | ||
|
||
container.Register<ICommandHandler<RealCommand>, RealCommandHandler>(); | ||
container.RegisterDecorator(typeof(ICommandHandler<>), typeof(DecoratorWithFailingTypeInitializer<>)); | ||
|
||
// Act | ||
Action action = () => container.GetInstance<ICommandHandler<RealCommand>>(); | ||
|
||
// Assert | ||
AssertThat.ThrowsWithExceptionMessageContains<ActivationException>( | ||
"The type initializer for " + | ||
$"{nameof(TypeInitializationErrorTests)}.DecoratorWithFailingTypeInitializer<RealCommand> " + | ||
"threw an exception. <Inner exception>.", | ||
action); | ||
} | ||
|
||
[TestMethod] | ||
public void GetInstance_ResolvingANestedDecoratorWithTypeInitializationException_ThrowsExpressiveException() | ||
{ | ||
// Act | ||
var container = new Container(); | ||
|
||
container.Register<ICommandHandler<RealCommand>, RealCommandHandler>(); | ||
container.RegisterDecorator(typeof(ICommandHandler<>), typeof(CommandHandlerDecorator<>)); | ||
container.RegisterDecorator(typeof(ICommandHandler<>), typeof(DecoratorWithFailingTypeInitializer<>)); | ||
container.RegisterDecorator(typeof(ICommandHandler<>), typeof(CommandHandlerDecorator<>)); | ||
|
||
// Act | ||
Action action = () => container.GetInstance<ICommandHandler<RealCommand>>(); | ||
|
||
// Assert | ||
AssertThat.ThrowsWithExceptionMessageContains<ActivationException>( | ||
"The type initializer for " + | ||
$"{nameof(TypeInitializationErrorTests)}.DecoratorWithFailingTypeInitializer<RealCommand> " + | ||
"threw an exception. <Inner exception>.", | ||
action); | ||
} | ||
|
||
public class NestedClassWithFailingTypeInitializer : INonGenericService | ||
{ | ||
static NestedClassWithFailingTypeInitializer() | ||
{ | ||
throw new Exception("<Inner exception>."); | ||
} | ||
|
||
public void DoSomething() | ||
{ | ||
} | ||
} | ||
|
||
public class NestedClassWithFailingTypeInitializer<T> | ||
{ | ||
static NestedClassWithFailingTypeInitializer() | ||
{ | ||
throw new Exception("<Inner exception>."); | ||
} | ||
} | ||
|
||
public class DecoratorWithFailingTypeInitializer<T> : ICommandHandler<T> | ||
{ | ||
static DecoratorWithFailingTypeInitializer() | ||
{ | ||
throw new Exception("<Inner exception>."); | ||
} | ||
|
||
public DecoratorWithFailingTypeInitializer(ICommandHandler<T> decoratee) | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.