Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Exception handling during test class construction no longer relies on…

… deprecated uses of ExceptionList objects. Factory delegate type simplifies to Func<Type, object>. When factories throw, PreservedExceptions are properly unpacked so that the original exception is presented to the user.
  • Loading branch information...
commit f0bc304b1a77725be08a0edf889806979dc89ae3 1 parent ed71a69
@plioi plioi authored
View
25 src/Fixie/Behaviors/CreateInstancePerCase.cs
@@ -5,9 +5,9 @@ namespace Fixie.Behaviors
{
public class CreateInstancePerCase : TypeBehavior
{
- readonly Factory construct;
+ readonly Func<Type, object> construct;
- public CreateInstancePerCase(Factory construct)
+ public CreateInstancePerCase(Func<Type, object> construct)
{
this.construct = construct;
}
@@ -18,18 +18,14 @@ public void Execute(Type testClass, Convention convention, Case[] cases)
{
var exceptions = @case.Exceptions;
- object instance;
-
- var constructionExceptions = construct(testClass, out instance);
- if (constructionExceptions.Any())
- {
- exceptions.Add(constructionExceptions);
- }
- else
+ try
{
+ var instance = construct(testClass);
+
var fixture = new Fixture(testClass, instance, convention.CaseExecution.Behavior, new[] { @case });
convention.InstanceExecution.Behavior.Execute(fixture);
+ //TODO: Further opportunity for simplification: consider not bothering with this try/catch.
try
{
Lifecycle.Dispose(instance);
@@ -39,6 +35,15 @@ public void Execute(Type testClass, Convention convention, Case[] cases)
exceptions.Add(disposalException);
}
}
+ catch (PreservedException preservedException)
+ {
+ var constructionException = preservedException.OriginalException;
+ exceptions.Add(constructionException);
+ }
+ catch (Exception constructionException)
+ {
+ exceptions.Add(constructionException);
+ }
}
}
}
View
29 src/Fixie/Behaviors/CreateInstancePerTestClass.cs
@@ -5,28 +5,23 @@ namespace Fixie.Behaviors
{
public class CreateInstancePerTestClass : TypeBehavior
{
- readonly Factory construct;
+ readonly Func<Type, object> construct;
- public CreateInstancePerTestClass(Factory construct)
+ public CreateInstancePerTestClass(Func<Type, object> construct)
{
this.construct = construct;
}
public void Execute(Type testClass, Convention convention, Case[] cases)
{
- object instance;
-
- var constructionExceptions = construct(testClass, out instance);
- if (constructionExceptions.Any())
- {
- foreach (var @case in cases)
- @case.Exceptions.Add(constructionExceptions);
- }
- else
+ try
{
+ var instance = construct(testClass);
+
var fixture = new Fixture(testClass, instance, convention.CaseExecution.Behavior, cases);
convention.InstanceExecution.Behavior.Execute(fixture);
+ //TODO: Further opportunity for simplification: consider not bothering with this try/catch.
try
{
Lifecycle.Dispose(instance);
@@ -37,6 +32,18 @@ public void Execute(Type testClass, Convention convention, Case[] cases)
@case.Exceptions.Add(disposalException);
}
}
+ catch (PreservedException preservedException)
+ {
+ var constructionException = preservedException.OriginalException;
+
+ foreach (var @case in cases)
+ @case.Exceptions.Add(constructionException);
+ }
+ catch (Exception constructionException)
+ {
+ foreach (var @case in cases)
+ @case.Exceptions.Add(constructionException);
+ }
}
}
}
View
16 src/Fixie/Behaviors/Lifecycle.cs
@@ -5,26 +5,16 @@ namespace Fixie.Behaviors
{
public class Lifecycle
{
- public static ExceptionList Construct(Type type, out object instance)
+ public static object Construct(Type type)
{
- var exceptions = new ExceptionList();
-
- instance = null;
-
try
{
- instance = Activator.CreateInstance(type);
+ return Activator.CreateInstance(type);
}
catch (TargetInvocationException ex)
{
- exceptions.Add(ex.InnerException);
- }
- catch (Exception ex)
- {
- exceptions.Add(ex);
+ throw new PreservedException(ex.InnerException);
}
-
- return exceptions;
}
public static void Dispose(object instance)
View
33 src/Fixie/Conventions/TypeBehaviorBuilder.cs
@@ -4,7 +4,6 @@
namespace Fixie.Conventions
{
public delegate void TypeBehaviorAction(Type testClass, Convention convention, Case[] cases, Action innerBehavior);
- public delegate ExceptionList Factory(Type testClass, out object instance);
public class TypeBehaviorBuilder
{
@@ -22,7 +21,7 @@ public TypeBehaviorBuilder CreateInstancePerCase()
}
public TypeBehaviorBuilder CreateInstancePerCase(Func<Type, object> construct)
{
- Behavior = new CreateInstancePerCase(new SafeFactory(construct).Construct);
+ Behavior = new CreateInstancePerCase(construct);
return this;
}
@@ -34,7 +33,7 @@ public TypeBehaviorBuilder CreateInstancePerTestClass()
public TypeBehaviorBuilder CreateInstancePerTestClass(Func<Type, object> construct)
{
- Behavior = new CreateInstancePerTestClass(new SafeFactory(construct).Construct);
+ Behavior = new CreateInstancePerTestClass(construct);
return this;
}
@@ -80,33 +79,5 @@ public void Execute(Type testClass, Convention convention, Case[] cases)
}
}
}
-
- class SafeFactory
- {
- readonly Func<Type, object> construct;
-
- public SafeFactory(Func<Type, object> construct)
- {
- this.construct = construct;
- }
-
- public ExceptionList Construct(Type type, out object instance)
- {
- var exceptions = new ExceptionList();
-
- instance = null;
-
- try
- {
- instance = construct(type);
- }
- catch (Exception ex)
- {
- exceptions.Add(ex);
- }
-
- return exceptions;
- }
- }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.