/
CreateInstancePerCase.cs
50 lines (44 loc) · 1.59 KB
/
CreateInstancePerCase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using Fixie.Conventions;
namespace Fixie.Behaviors
{
public class CreateInstancePerCase : TypeBehavior
{
readonly Func<Type, object> construct;
public CreateInstancePerCase(Func<Type, object> construct)
{
this.construct = construct;
}
public void Execute(Type testClass, Convention convention, Case[] cases)
{
foreach (var @case in cases)
{
var exceptions = @case.Exceptions;
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);
}
catch (Exception disposalException)
{
exceptions.Add(disposalException);
}
}
catch (PreservedException preservedException)
{
var constructionException = preservedException.OriginalException;
exceptions.Add(constructionException);
}
catch (Exception constructionException)
{
exceptions.Add(constructionException);
}
}
}
}
}