2020#endregion
2121
2222using System ;
23-
23+ using System . Collections . Generic ;
2424using Autofac ;
25+ using Autofac . Core ;
2526using Autofac . Extras . CommonServiceLocator ;
26-
2727using Microsoft . Practices . ServiceLocation ;
28-
2928using Moq ;
30-
3129using Patterns . Autofac ;
3230using Patterns . ExceptionHandling ;
3331using Patterns . Testing . Moq ;
3432
3533namespace Patterns . Testing . Autofac . Moq
3634{
3735 /// <summary>
38- /// Provides a default implementation of the <see cref="IAutofacMoqContainer" /> interface.
36+ /// Provides a default implementation of the <see cref="IAutofacMoqContainer" /> interface.
3937 /// </summary>
4038 public sealed class AutofacMoqContainer : AccessibleContainer , IAutofacMoqContainer
4139 {
40+ private readonly MockBehavior _defaultBehavior ;
41+ private readonly MoqRegistrationSource _registrationSource ;
42+
4243 /// <summary>
43- /// Initializes a new instance of the <see cref="AutofacMoqContainer" /> class.
44+ /// Initializes a new instance of the <see cref="AutofacMoqContainer" /> class with
4445 /// </summary>
45- /// <param name="container ">The container .</param>
46- public AutofacMoqContainer ( IContainer container ) : base ( container )
46+ /// <param name="defaultBehavior ">The default <see cref="MockBehavior"/> .</param>
47+ public AutofacMoqContainer ( IContainer container , MockBehavior behavior ) : base ( container )
4748 {
49+ _defaultBehavior = behavior ;
4850 Locator = new AutofacServiceLocator ( this ) ;
49- ComponentRegistry . AddRegistrationSource ( new MoqRegistrationSource ( ) ) ;
51+ ComponentRegistry . AddRegistrationSource ( _registrationSource = new MoqRegistrationSource ( _defaultBehavior ) ) ;
52+ }
53+
54+ /// <summary>
55+ /// Initializes a new instance of the <see cref="AutofacMoqContainer" /> class.
56+ /// </summary>
57+ /// <param name="container">The container.</param>
58+ /// <param name="defaultBehavior">The default <see cref="MockBehavior"/>.</param>
59+ public AutofacMoqContainer ( IContainer container ) : this ( container , MockBehavior . Default )
60+ {
5061 }
5162
5263 /// <summary>
53- /// Initializes a new instance of the <see cref="AutofacMoqContainer" /> class.
64+ /// Initializes a new instance of the <see cref="AutofacMoqContainer" /> class.
5465 /// </summary>
55- public AutofacMoqContainer ( ) : this ( new ContainerBuilder ( ) . Build ( ) ) { }
66+ public AutofacMoqContainer ( ) : this ( new ContainerBuilder ( ) . Build ( ) )
67+ {
68+ }
5669
5770 /// <summary>
58- /// Gets the locator .
71+ /// Gets the default <see cref="MockBehavior"/> .
5972 /// </summary>
6073 /// <value>
61- /// The locator.
74+ /// The default <see cref="MockBehavior"/>.
75+ /// </value>
76+ public MockBehavior DefaultBehavior { get { return _defaultBehavior ; } }
77+
78+ /// <summary>
79+ /// Gets the locator.
80+ /// </summary>
81+ /// <value>
82+ /// The locator.
6283 /// </value>
6384 public IServiceLocator Locator { get ; private set ; }
6485
6586 /// <summary>
66- /// Retrieves the mock for the specified service type.
87+ /// Retrieves the mock for the specified service type.
6788 /// </summary>
6889 /// <typeparam name="TService">The type of the service.</typeparam>
6990 /// <returns>
70- /// The service mock.
91+ /// The service mock.
7192 /// </returns>
7293 public Mock < TService > Mock < TService > ( ) where TService : class
7394 {
74- TService service = Try . Get ( Create < TService > ) ;
95+ return Mock < TService > ( _defaultBehavior ) ;
96+ }
97+
98+ /// <summary>
99+ /// Retrieves the mock for the specified service type.
100+ /// </summary>
101+ /// <typeparam name="TService">The type of the service.</typeparam>
102+ /// <param name="mockBehavior">
103+ /// The <see cref="MockBehavior" /> of the mock.
104+ /// </param>
105+ /// <returns>
106+ /// The service mock.
107+ /// </returns>
108+ public Mock < TService > Mock < TService > ( MockBehavior mockBehavior ) where TService : class
109+ {
110+ TService service = Try . Get < TService > ( Create < TService > ) ;
75111 var existingMock = service as IMocked < TService > ;
76112 if ( existingMock != null ) return existingMock . Mock ;
77113
78- Mock < TService > mock = MoqRegistrationSource . Repository . Create < TService > ( ) ;
114+ Mock < TService > mock = _registrationSource . Repository . Create < TService > ( mockBehavior ) ;
79115 Update ( mock . Object ) ;
80116 return mock ;
81117 }
82118
83119 /// <summary>
84- /// Creates an instance of the specified service, injecting mocked objects
85- /// for all unregistered dependencies.
120+ /// Creates an instance of the specified service, injecting mocked objects
121+ /// for all unregistered dependencies.
86122 /// </summary>
87123 /// <typeparam name="TService">The type of the service.</typeparam>
88124 /// <returns>
89- /// The service instance.
125+ /// The service instance.
90126 /// </returns>
91127 public TService Create < TService > ( ) where TService : class
92128 {
93129 return Container . Resolve < TService > ( ) ;
94130 }
95131
96132 /// <summary>
97- /// Creates an instance of the specified implementation (as the specified service),
98- /// injecting mocked objects for all unregistered dependencies.
133+ /// Creates an instance of the specified implementation (as the specified service),
134+ /// injecting mocked objects for all unregistered dependencies.
99135 /// </summary>
100136 /// <typeparam name="TService">The type of the service.</typeparam>
101137 /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
@@ -107,59 +143,59 @@ public TService Create<TService, TImplementation>() where TService : class where
107143 }
108144
109145 /// <summary>
110- /// Updates this instance by registering the implementation type as the service type.
146+ /// Updates this instance by registering the implementation type as the service type.
111147 /// </summary>
112148 /// <typeparam name="TService">The type of the service.</typeparam>
113149 /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
114150 /// <returns>
115- /// The container.
151+ /// The container.
116152 /// </returns>
117153 public IMoqContainer Update < TService , TImplementation > ( ) where TService : class where TImplementation : TService
118154 {
119155 UpdateWithBuilder ( builder => builder . RegisterType < TImplementation > ( ) . As < TService > ( )
120- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
156+ . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
121157
122158 return this ;
123159 }
124160
125161 /// <summary>
126- /// Updates this instance by registering an instance of the specified service.
162+ /// Updates this instance by registering an instance of the specified service.
127163 /// </summary>
128164 /// <typeparam name="TService">The type of the service.</typeparam>
129165 /// <param name="instance">The instance.</param>
130166 /// <returns>
131- /// The container.
167+ /// The container.
132168 /// </returns>
133169 public IMoqContainer Update < TService > ( TService instance ) where TService : class
134170 {
135171 UpdateWithBuilder ( builder => builder . RegisterInstance ( instance ) . As < TService > ( )
136- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
172+ . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
137173
138174 return this ;
139175 }
140176
141177 /// <summary>
142- /// Updates this instance by registering the specified activator as the service type.
178+ /// Updates this instance by registering the specified activator as the service type.
143179 /// </summary>
144180 /// <typeparam name="TService">The type of the service.</typeparam>
145181 /// <param name="activator">The activator.</param>
146182 /// <returns>
147- /// The container
183+ /// The container
148184 /// </returns>
149185 public IMoqContainer Update < TService > ( Func < IMoqContainer , TService > activator ) where TService : class
150186 {
151187 UpdateWithBuilder ( builder => builder . Register ( c => activator ( this ) ) . As < TService > ( )
152- . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
188+ . PropertiesAutowired ( PropertyWiringOptions . PreserveSetValues ) ) ;
153189
154190 return this ;
155191 }
156192
157193 /// <summary>
158- /// Updates the container using the specified module.
194+ /// Updates the container using the specified module.
159195 /// </summary>
160196 /// <param name="module">The module.</param>
161197 /// <returns>
162- /// The container.
198+ /// The container.
163199 /// </returns>
164200 public IAutofacMoqContainer Update ( Module module )
165201 {
@@ -168,11 +204,11 @@ public IAutofacMoqContainer Update(Module module)
168204 }
169205
170206 /// <summary>
171- /// Updates the container using the specified registration.
207+ /// Updates the container using the specified registration.
172208 /// </summary>
173209 /// <param name="registration">The registration.</param>
174210 /// <returns>
175- /// The container.
211+ /// The container.
176212 /// </returns>
177213 public IAutofacMoqContainer Update ( Action < ContainerBuilder > registration )
178214 {
0 commit comments