1
1
using System . Collections . Generic ;
2
- using System . Linq ;
3
2
using System . Web ;
4
3
using System . Web . Routing ;
5
4
6
- using BetterModules . Core . DataAccess . DataContext . Migrations ;
7
- using BetterModules . Core . Modules ;
8
- using BetterModules . Core . Modules . Registration ;
5
+ using BetterModules . Core . Tests . TestHelpers ;
9
6
using BetterModules . Core . Web . Environment . Host ;
10
7
using BetterModules . Core . Web . Modules . Registration ;
11
8
@@ -24,58 +21,40 @@ public class DefaultWebApplicationHostTests : TestBase
24
21
private bool eventFired ;
25
22
26
23
[ Test ]
27
- public void ShouldExecute_OnApplicationStart_Correctly ( )
24
+ public void ShouldExecute_Init_Correctly ( )
28
25
{
29
26
eventFired = false ;
30
27
var routesRegistered = false ;
31
- var databaseMigrated = false ;
32
-
33
- var registration = new Mock < IWebModulesRegistration > ( ) ;
34
- registration
35
- . Setup ( r => r . RegisterKnownModuleRoutes ( It . IsAny < RouteCollection > ( ) ) )
36
- . Callback < RouteCollection > ( rc => routesRegistered = true ) ;
37
-
38
- var moduleDescriptor1 = new ModuleRegistrationContext ( new Mock < ModuleDescriptor > ( ) . Object ) ;
39
- var moduleDescriptor2 = new ModuleRegistrationContext ( new Mock < ModuleDescriptor > ( ) . Object ) ;
40
-
41
- registration
42
- . Setup ( r => r . GetModules ( ) )
43
- . Returns ( ( ) => new [ ] { moduleDescriptor1 , moduleDescriptor2 } ) ;
44
-
45
- var migrationRunner = new Mock < IMigrationRunner > ( ) ;
46
- migrationRunner
47
- . Setup ( r => r . MigrateStructure ( It . IsAny < IList < ModuleDescriptor > > ( ) ) )
48
- . Callback < IList < ModuleDescriptor > > (
49
- descriptors =>
50
- {
51
- Assert . AreEqual ( descriptors . Count , 2 ) ;
52
- Assert . IsTrue ( descriptors . Any ( d => d == moduleDescriptor1 . ModuleDescriptor ) ) ;
53
- Assert . IsTrue ( descriptors . Any ( d => d == moduleDescriptor2 . ModuleDescriptor ) ) ;
54
-
55
- databaseMigrated = true ;
56
- } ) ;
57
-
58
- var host = new DefaultWebApplicationHost ( registration . Object , migrationRunner . Object ) ;
59
- CreateApplication ( ) ;
60
28
61
- WebCoreEvents . Instance . HostStart += Instance_Fired ;
62
- host . OnApplicationStart ( application ) ;
63
- WebCoreEvents . Instance . HostStart -= Instance_Fired ;
29
+ using ( var fakeProvider = new ContextScopeProviderHelper ( ) )
30
+ {
31
+
32
+ var registration = new Mock < IWebModulesRegistration > ( ) ;
33
+ registration
34
+ . Setup ( r => r . RegisterKnownModuleRoutes ( It . IsAny < RouteCollection > ( ) ) )
35
+ . Callback < RouteCollection > ( rc => routesRegistered = true ) ;
36
+ fakeProvider . RegisterFakeServiceInstance ( registration . Object , typeof ( IWebModulesRegistration ) ) ;
37
+
38
+ var host = new UtilityHost ( ) ;
39
+ CreateApplication ( ) ;
64
40
41
+ WebCoreEvents . Instance . HostStart += Instance_Fired ;
42
+ host . Init ( application ) ;
43
+ WebCoreEvents . Instance . HostStart -= Instance_Fired ;
44
+ }
65
45
Assert . IsTrue ( eventFired ) ;
66
46
Assert . IsTrue ( routesRegistered ) ;
67
- Assert . IsTrue ( databaseMigrated ) ;
68
47
}
69
48
70
49
[ Test ]
71
- public void ShouldExecute_OnApplicationEnd_Correctly ( )
50
+ public void ShouldExecute_Dispose_Correctly ( )
72
51
{
73
- var host = CreateHost ( ) ;
52
+ var host = new UtilityHost ( ) ;
74
53
CreateApplication ( ) ;
75
54
eventFired = false ;
76
-
55
+ host . Init ( application ) ;
77
56
WebCoreEvents . Instance . HostStop += Instance_Fired ;
78
- host . OnApplicationEnd ( application ) ;
57
+ host . Dispose ( ) ;
79
58
WebCoreEvents . Instance . HostStop -= Instance_Fired ;
80
59
81
60
Assert . IsTrue ( eventFired ) ;
@@ -119,17 +98,38 @@ void Instance_Fired(SingleItemEventArgs<HttpApplication> args)
119
98
120
99
private IWebApplicationHost CreateHost ( )
121
100
{
122
- var registration = new Mock < IWebModulesRegistration > ( ) ;
123
- var migrationRunner = new Mock < IMigrationRunner > ( ) ;
124
-
125
- var host = new DefaultWebApplicationHost ( registration . Object , migrationRunner . Object ) ;
126
-
101
+ var host = new UtilityHost ( ) ;
127
102
return host ;
128
103
}
129
104
130
105
private void CreateApplication ( )
131
106
{
132
107
application = new Mock < HttpApplication > ( ) . Object ;
133
108
}
109
+
110
+ private class EventTestApplicationAutoHost : DefaultWebApplicationAutoHost
111
+ {
112
+ public List < string > Results = new List < string > ( ) ;
113
+
114
+ public override void OnAuthenticateRequest ( HttpApplication application )
115
+ {
116
+ Results . Add ( "OnAuthenticateRequest" ) ;
117
+ }
118
+
119
+ public override void OnBeginRequest ( HttpApplication application )
120
+ {
121
+ Results . Add ( "OnBeginRequest" ) ;
122
+ }
123
+
124
+ public override void OnEndRequest ( HttpApplication application )
125
+ {
126
+ Results . Add ( "OnEndRequest" ) ;
127
+ }
128
+
129
+ public override void OnApplicationError ( HttpApplication application )
130
+ {
131
+ Results . Add ( "OnApplicationError" ) ;
132
+ }
133
+ }
134
134
}
135
135
}
0 commit comments