-
Notifications
You must be signed in to change notification settings - Fork 31
/
DependencyInjectionVerificationTests.cs
115 lines (99 loc) · 3.96 KB
/
DependencyInjectionVerificationTests.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using Autofac;
using Autofac.Core;
using AzureFunctions.Autofac.Configuration;
using AzureFunctions.Autofac.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AzureFunctions.Autofac.NetStandard.Tests
{
[TestClass]
public class DependencyInjectionVerificationTests
{
[TestMethod]
public void VerifyConfiguration_Should_PassSuccessfully_When_CorrectConfiguration()
{
DependencyInjection.VerifyConfiguration(typeof(FunctionWithCorrectConfiguration));
}
[TestMethod]
public void VerifyConfiguration_Should_ThrowException_When_IncorrectConfiguration()
{
Assert.ThrowsException<DependencyResolutionException>(() =>
DependencyInjection.VerifyConfiguration(typeof(FunctionWithIncorrectConfiguration)));
}
[TestMethod]
public void VerifyConfiguration_Should_ThrowException_When_MisingConfiguration()
{
Assert.ThrowsException<MissingAttributeException>(() =>
DependencyInjection.VerifyConfiguration(typeof(FunctionWithInjectionButNoConfiguration)));
}
[TestMethod]
public void VerifyConfiguration_Should_ThrowException_When_MisingInjectAttribute()
{
Assert.ThrowsException<MissingAttributeException>(() =>
DependencyInjection.VerifyConfiguration(typeof(FunctionWithConfigurationButNoInjection)));
}
[TestMethod]
public void VerifyConfiguration_Should_Pass_When_NotRequiringAMatchingAttribute()
{
DependencyInjection.VerifyConfiguration(typeof(FunctionWithConfigurationButNoInjection), false);
}
[TestMethod]
public void VerifyConfiguration_Should_Pass_When_NoConfigurationOrAttributes()
{
DependencyInjection.VerifyConfiguration(typeof(FunctionWithNeitherConfigurationNorInjection));
}
// Interface/Implementation
public interface IInterface1 { }
public interface IInterface2 { }
public class DependentImplementation : IInterface1
{
public DependentImplementation(IInterface2 _) { }
}
public class DependeeImplementation : IInterface2 { }
// Configurations
public class CorrectConfigurationExample
{
public CorrectConfigurationExample(string functionName)
{
DependencyInjection.Initialize(builder =>
{
builder.RegisterType<DependentImplementation>().As<IInterface1>();
builder.RegisterType<DependeeImplementation>().As<IInterface2>();
}, functionName);
}
}
public class IncorrectConfigurationExample
{
public IncorrectConfigurationExample(string functionName)
{
DependencyInjection.Initialize(builder =>
{
builder.RegisterType<DependentImplementation>().As<IInterface1>();
}, functionName);
}
}
// Example classes using dependency injection
[DependencyInjectionConfig(typeof(CorrectConfigurationExample))]
public class FunctionWithCorrectConfiguration
{
public static void Run([Inject] IInterface1 d) { }
}
[DependencyInjectionConfig(typeof(IncorrectConfigurationExample))]
public class FunctionWithIncorrectConfiguration
{
public static void Run([Inject] IInterface1 d) { }
}
public class FunctionWithInjectionButNoConfiguration
{
public static void Run([Inject] IInterface1 d) { }
}
[DependencyInjectionConfig(typeof(CorrectConfigurationExample))]
public class FunctionWithConfigurationButNoInjection
{
public static void Run() { }
}
public class FunctionWithNeitherConfigurationNorInjection
{
public static void Run() { }
}
}
}