44using System . Linq ;
55using System . Net ;
66using Consul ;
7+ using FluentAssertions ;
78using Microsoft . Extensions . Configuration ;
89using Moq ;
9- using NUnit . Framework ;
1010using Winton . Extensions . Configuration . Consul . Parsers ;
11+ using Xunit ;
1112
1213namespace Winton . Extensions . Configuration . Consul
1314{
14- [ TestFixture ]
15- [ TestOf ( nameof ( ConsulConfigurationProvider ) ) ]
16- internal class ConsulConfigurationProviderTests
15+ public class ConsulConfigurationProviderTests
1716 {
1817 private readonly Mock < IConfigurationParser > _configParserMock =
1918 new Mock < IConfigurationParser > ( MockBehavior . Strict ) ;
@@ -26,35 +25,37 @@ internal class ConsulConfigurationProviderTests
2625
2726 private ConsulConfigurationProvider _consulConfigProvider ;
2827
29- internal sealed class Constructor : ConsulConfigurationProviderTests
28+ public sealed class Constructor : ConsulConfigurationProviderTests
3029 {
31- [ Test ]
30+ [ Fact ]
3231 public void ShouldThrowIfParserIsNull ( )
3332 {
3433 _consulConfigSourceMock . Setup ( ccs => ccs . Parser ) . Returns ( ( IConfigurationParser ) null ) ;
3534
36- Assert . That (
35+ // ReSharper disable once ObjectCreationAsStatement
36+ Action constructing =
3737 ( ) =>
38- new ConsulConfigurationProvider ( _consulConfigSourceMock . Object , _consulConfigClientMock . Object ) ,
39- Throws . TypeOf < ArgumentNullException > ( )
40- . And . Message . Contains ( nameof ( _consulConfigSourceMock . Object . Parser ) ) ) ;
38+ new ConsulConfigurationProvider ( _consulConfigSourceMock . Object , _consulConfigClientMock . Object ) ;
39+
40+ constructing . ShouldThrow < ArgumentNullException > ( )
41+ . And . Message . Should ( ) . Contain ( nameof ( _consulConfigSourceMock . Object . Parser ) ) ;
4142 }
4243 }
4344
44- internal sealed class Load : ConsulConfigurationProviderTests
45+ public sealed class Load : ConsulConfigurationProviderTests
4546 {
46- [ SetUp ]
47- public void SetUp ( )
47+ public Load ( )
4848 {
4949 _consulConfigSourceMock . Setup ( ccs => ccs . Parser ) . Returns ( _configParserMock . Object ) ;
5050 _consulConfigSourceMock . Setup ( ccs => ccs . ReloadOnChange ) . Returns ( false ) ;
5151
52- _consulConfigProvider =
53- new ConsulConfigurationProvider ( _consulConfigSourceMock . Object , _consulConfigClientMock . Object ) ;
52+ _consulConfigProvider = new ConsulConfigurationProvider (
53+ _consulConfigSourceMock . Object ,
54+ _consulConfigClientMock . Object ) ;
5455 }
5556
56- [ Test ]
57- public void ShouldCallSourceOnLoadExceptionActionWhenException ( )
57+ [ Fact ]
58+ private void ShouldCallSourceOnLoadExceptionActionWhenException ( )
5859 {
5960 var calledOnLoadException = false ;
6061
@@ -70,11 +71,11 @@ public void ShouldCallSourceOnLoadExceptionActionWhenException()
7071
7172 _consulConfigProvider . Load ( ) ;
7273
73- Assert . That ( calledOnLoadException , Is . True ) ;
74+ calledOnLoadException . Should ( ) . BeTrue ( ) ;
7475 }
7576
76- [ Test ]
77- public void ShouldHaveEmptyDataIfConfigDoesNotExistAndIsOptional ( )
77+ [ Fact ]
78+ private void ShouldHaveEmptyDataIfConfigDoesNotExistAndIsOptional ( )
7879 {
7980 _consulConfigSourceMock . Setup ( ccs => ccs . Optional ) . Returns ( true ) ;
8081 _consulConfigClientMock
@@ -83,13 +84,11 @@ public void ShouldHaveEmptyDataIfConfigDoesNotExistAndIsOptional()
8384
8485 _consulConfigProvider . Load ( ) ;
8586
86- Assert . That (
87- _consulConfigProvider . GetChildKeys ( Enumerable . Empty < string > ( ) , string . Empty ) ,
88- Is . EqualTo ( Enumerable . Empty < string > ( ) ) . AsCollection ) ;
87+ _consulConfigProvider . GetChildKeys ( Enumerable . Empty < string > ( ) , string . Empty ) . Should ( ) . BeEmpty ( ) ;
8988 }
9089
91- [ Test ]
92- public void ShouldNotParseIfConfigBytesIsNull ( )
90+ [ Fact ]
91+ private void ShouldNotParseIfConfigBytesIsNull ( )
9392 {
9493 _consulConfigSourceMock . Setup ( ccs => ccs . Optional ) . Returns ( true ) ;
9594 _consulConfigClientMock
@@ -103,30 +102,30 @@ public void ShouldNotParseIfConfigBytesIsNull()
103102
104103 _consulConfigProvider . Load ( ) ;
105104
106- Assert . That (
107- ( ) => _configParserMock . Verify ( cp => cp . Parse ( It . IsAny < MemoryStream > ( ) ) , Times . Never ) ,
108- Throws . Nothing ) ;
105+ Action verifying = ( ) => _configParserMock . Verify ( cp => cp . Parse ( It . IsAny < MemoryStream > ( ) ) , Times . Never ) ;
106+ verifying . ShouldNotThrow ( ) ;
109107 }
110108
111- [ Test ]
112- public void ShouldNotThrowExceptionIfOnLoadExceptionIsSetToIgnore ( )
109+ [ Fact ]
110+ private void ShouldNotThrowExceptionIfOnLoadExceptionIsSetToIgnore ( )
113111 {
114112 _consulConfigClientMock . Setup ( ccc => ccc . GetConfig ( ) )
115113 . ThrowsAsync ( new Exception ( "Failed to load from Consul agent" ) ) ;
116114 _consulConfigSourceMock
117115 . Setup ( ccs => ccs . OnLoadException )
118116 . Returns ( exceptionContext => { exceptionContext . Ignore = true ; } ) ;
119117
120- Assert . That ( ( ) => _consulConfigProvider . Load ( ) , Throws . Nothing ) ;
118+ Action loading = ( ) => _consulConfigProvider . Load ( ) ;
119+ loading . ShouldNotThrow ( ) ;
121120 }
122121
123- [ Test ]
124- [ TestCase ( "Key" , "Key" ) ]
125- [ TestCase ( "KEY" , "key" , TestName = "ShouldParseLoadedConfigIntoCaseInsensitiveDictionary ") ]
126- public void ShouldParseLoadedConfigIntoDictionary ( string actualKey , string lookupKey )
122+ [ Theory ]
123+ [ InlineData ( "Key" , "Key" ) ]
124+ [ InlineData ( "KEY" , "key" ) ]
125+ private void ShouldParseLoadedConfigIntoDictionary ( string key , string lookupKey )
127126 {
128127 _configParserMock . Setup ( cp => cp . Parse ( It . IsAny < MemoryStream > ( ) ) )
129- . Returns ( new Dictionary < string , string > { { actualKey , "Value" } } ) ;
128+ . Returns ( new Dictionary < string , string > { { key , "Value" } } ) ;
130129 _consulConfigClientMock
131130 . Setup ( ccc => ccc . GetConfig ( ) )
132131 . ReturnsAsync (
@@ -138,14 +137,14 @@ public void ShouldParseLoadedConfigIntoDictionary(string actualKey, string looku
138137
139138 _consulConfigProvider . Load ( ) ;
140139
141- _consulConfigProvider . TryGet ( lookupKey , out string actualValue ) ;
142- Assert . That ( actualValue , Is . EqualTo ( "Value" ) ) ;
140+ _consulConfigProvider . TryGet ( lookupKey , out string value ) ;
141+ value . Should ( ) . Be ( "Value" ) ;
143142 }
144143
145- [ Test ]
146- public void ShouldSetExceptionInLoadExceptionContextWhenExceptionDuringLoad ( )
144+ [ Fact ]
145+ private void ShouldSetExceptionInLoadExceptionContextWhenExceptionDuringLoad ( )
147146 {
148- ConsulLoadExceptionContext actualExceptionContext = null ;
147+ ConsulLoadExceptionContext exceptionContext = null ;
149148 var expectedException = new Exception ( "Failed to load from Consul agent" ) ;
150149
151150 _consulConfigClientMock . Setup ( ccc => ccc . GetConfig ( ) ) . ThrowsAsync ( expectedException ) ;
@@ -155,49 +154,48 @@ public void ShouldSetExceptionInLoadExceptionContextWhenExceptionDuringLoad()
155154 context =>
156155 {
157156 context . Ignore = true ;
158- actualExceptionContext = context ;
157+ exceptionContext = context ;
159158 } ) ;
160159
161160 _consulConfigProvider . Load ( ) ;
162161
163- Assert . That ( actualExceptionContext . Exception , Is . SameAs ( expectedException ) ) ;
162+ exceptionContext . Exception . Should ( ) . BeSameAs ( expectedException ) ;
164163 }
165164
166- [ Test ]
167- public void ShouldSetSourceInLoadExceptionContextWhenExceptionDuringLoad ( )
165+ [ Fact ]
166+ private void ShouldSetSourceInLoadExceptionContextWhenExceptionDuringLoad ( )
168167 {
169- ConsulLoadExceptionContext actualExceptionContext = null ;
168+ ConsulLoadExceptionContext exceptionContext = null ;
170169 _consulConfigClientMock . Setup ( ccc => ccc . GetConfig ( ) ) . ThrowsAsync ( new Exception ( ) ) ;
171170 _consulConfigSourceMock
172171 . Setup ( ccs => ccs . OnLoadException )
173172 . Returns (
174173 context =>
175174 {
176175 context . Ignore = true ;
177- actualExceptionContext = context ;
176+ exceptionContext = context ;
178177 } ) ;
179178
180179 _consulConfigProvider . Load ( ) ;
181180
182- Assert . That ( actualExceptionContext . Source , Is . SameAs ( _consulConfigSourceMock . Object ) ) ;
181+ exceptionContext . Source . Should ( ) . BeSameAs ( _consulConfigSourceMock . Object ) ;
183182 }
184183
185- [ Test ]
186- public void ShouldThrowExceptionIfOnLoadExceptionDoesNotSetIgnoreWhenExceptionDuringLoad ( )
184+ [ Fact ]
185+ private void ShouldThrowExceptionIfOnLoadExceptionDoesNotSetIgnoreWhenExceptionDuringLoad ( )
187186 {
188187 _consulConfigClientMock . Setup ( ccc => ccc . GetConfig ( ) )
189188 . ThrowsAsync ( new Exception ( "Error" ) ) ;
190189 _consulConfigSourceMock
191190 . Setup ( ccs => ccs . OnLoadException )
192191 . Returns ( exceptionContext => { exceptionContext . Ignore = false ; } ) ;
193192
194- Assert . That (
195- ( ) => _consulConfigProvider . Load ( ) ,
196- Throws . TypeOf < Exception > ( ) . And . Message . Matches ( "Error" ) ) ;
193+ Action loading = _consulConfigProvider . Invoking ( ccp => ccp . Load ( ) ) ;
194+ loading . ShouldThrow < Exception > ( ) . WithMessage ( "Error" ) ;
197195 }
198196
199- [ Test ]
200- public void ShouldThrowIfConfigDoesNotExistAndIsNotOptonalWhenLoad ( )
197+ [ Fact ]
198+ private void ShouldThrowIfConfigDoesNotExistAndIsNotOptonalWhenLoad ( )
201199 {
202200 _consulConfigSourceMock . Setup ( ccs => ccs . Optional ) . Returns ( false ) ;
203201 _consulConfigClientMock
@@ -208,19 +206,17 @@ public void ShouldThrowIfConfigDoesNotExistAndIsNotOptonalWhenLoad()
208206 . Setup ( ccs => ccs . OnLoadException )
209207 . Returns ( context => context . Ignore = false ) ;
210208
211- Assert . That (
212- ( ) => _consulConfigProvider . Load ( ) ,
213- Throws . TypeOf < Exception > ( ) . And . Message . Matches (
214- "The configuration for key Test was not found and is not optional." ) ) ;
209+ Action loading = _consulConfigProvider . Invoking ( ccp => ccp . Load ( ) ) ;
210+ loading . ShouldThrow < Exception > ( )
211+ . WithMessage ( "The configuration for key Test was not found and is not optional." ) ;
215212 }
216213 }
217214
218- internal sealed class Reload : ConsulConfigurationProviderTests
215+ public sealed class Reload : ConsulConfigurationProviderTests
219216 {
220- private ConfigurationReloadToken _firstChangeToken ;
217+ private readonly ConfigurationReloadToken _firstChangeToken ;
221218
222- [ SetUp ]
223- public void SetUp ( )
219+ public Reload ( )
224220 {
225221 _firstChangeToken = new ConfigurationReloadToken ( ) ;
226222 _consulConfigClientMock
@@ -236,8 +232,8 @@ public void SetUp()
236232 _consulConfigClientMock . Object ) ;
237233 }
238234
239- [ Test ]
240- public void ShouldNotOverwriteNonOptionalConfigIfDoesNotExist ( )
235+ [ Fact ]
236+ private void ShouldNotOverwriteNonOptionalConfigIfDoesNotExist ( )
241237 {
242238 _consulConfigSourceMock . Setup ( ccs => ccs . Optional ) . Returns ( false ) ;
243239 _consulConfigClientMock
@@ -257,43 +253,47 @@ public void ShouldNotOverwriteNonOptionalConfigIfDoesNotExist()
257253
258254 _firstChangeToken . OnReload ( ) ;
259255
260- Assert . That ( _consulConfigProvider . TryGet ( "Key" , out string _ ) , Is . True ) ;
256+ _consulConfigProvider . TryGet ( "Key" , out string _ ) . Should ( ) . BeTrue ( ) ;
261257 }
262258
263- [ Test ]
264- [ TestCase ( false , TestName = "ShouldNotThrowIfDoesNotExistOnReloadWhenConfigOptional" ) ]
265- [ TestCase ( false , TestName = "ShouldNotThrowIfDoesNotExistOnReloadWhenConfigIsNotOptional" ) ]
266- public void ShouldNotThrowIfDoesNotExist ( bool optional )
259+ [ Theory ]
260+ [ InlineData ( true ) ]
261+ [ InlineData ( false ) ]
262+ private void ShouldNotThrowIfDoesNotExist ( bool optional )
267263 {
268264 _consulConfigSourceMock . Setup ( ccs => ccs . Optional ) . Returns ( optional ) ;
269265 _consulConfigSourceMock . Setup ( ccs => ccs . ReloadOnChange ) . Returns ( true ) ;
270266 _consulConfigClientMock
271267 . Setup ( ccc => ccc . GetConfig ( ) )
272268 . ReturnsAsync ( new QueryResult < KVPair > { StatusCode = HttpStatusCode . NotFound } ) ;
273269
274- Assert . That ( _firstChangeToken . OnReload , Throws . Nothing ) ;
270+ Action reloading = _firstChangeToken . Invoking ( ct => ct . OnReload ( ) ) ;
271+ reloading . ShouldNotThrow ( ) ;
275272 }
276273
277- [ Test ]
278- public void ShouldReloadConfigIfReloadOnChangeAndDataInConsulHasChanged ( )
274+ [ Fact ]
275+ private void ShouldReloadConfigIfReloadOnChangeAndDataInConsulHasChanged ( )
279276 {
277+ _consulConfigSourceMock . Setup ( ccs => ccs . Optional ) . Returns ( true ) ;
280278 _consulConfigClientMock
281279 . Setup ( ccc => ccc . GetConfig ( ) )
282280 . ReturnsAsync ( new QueryResult < KVPair > { StatusCode = HttpStatusCode . OK } ) ;
283281
284282 _firstChangeToken . OnReload ( ) ;
285283
286- Assert . That ( ( ) => _consulConfigClientMock . Verify ( ccc => ccc . GetConfig ( ) ) , Throws . Nothing ) ;
284+ Action verifying = ( ) => _consulConfigClientMock . Verify ( ccc => ccc . GetConfig ( ) , Times . Once ) ;
285+ verifying . ShouldNotThrow ( ) ;
287286 }
288287
289- [ Test ]
290- public void ShouldWatchForChangesIfSourceReloadOnChangesIsTrue ( )
288+ [ Fact ]
289+ private void ShouldWatchForChangesIfSourceReloadOnChangesIsTrue ( )
291290 {
292- Assert . That (
291+ Action verifying =
293292 ( ) =>
294293 _consulConfigClientMock . Verify (
295- ccs => ccs . Watch ( _consulConfigSourceMock . Object . OnWatchException ) ) ,
296- Throws . Nothing ) ;
294+ ccs => ccs . Watch ( _consulConfigSourceMock . Object . OnWatchException ) ,
295+ Times . Once ) ;
296+ verifying . ShouldNotThrow ( ) ;
297297 }
298298 }
299299 }
0 commit comments