Skip to content

Commit 25f04ad

Browse files
committed
Added tests for string overloads (#247)
1 parent 748ded9 commit 25f04ad

File tree

5 files changed

+297
-9
lines changed

5 files changed

+297
-9
lines changed

test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/DataTests/DistributedCacheBuilderTests.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ public void WithEntryShouldSetCorrectValues()
5555
.Ok();
5656
}
5757

58+
[Fact]
59+
public void WithEntryWithStringValueShouldSetCorrectValues()
60+
{
61+
MyController<DistributedCacheController>
62+
.Instance()
63+
.WithDistributedCache(distributedCache => distributedCache
64+
.WithEntry("FirstEntry", "FirstValue")
65+
.WithEntry("SecondEntry", "SecondValue")
66+
.WithEntry("ThirdEntry", "ThirdValue"))
67+
.Calling(c => c.ValidDistributedCacheEntriesAction(From.Services<IDistributedCache>()))
68+
.ShouldReturn()
69+
.Ok();
70+
}
71+
5872
[Fact]
5973
public void WithEntriesShouldSetCorrectValues()
6074
{
@@ -74,6 +88,23 @@ public void WithEntriesShouldSetCorrectValues()
7488
.Ok();
7589
}
7690

91+
[Fact]
92+
public void WithEntriesWithStringValuesShouldSetCorrectValues()
93+
{
94+
MyController<DistributedCacheController>
95+
.Instance()
96+
.WithDistributedCache(distributedCache => distributedCache
97+
.WithEntries(new Dictionary<string, string>()
98+
{
99+
{"FirstEntry", "FirstValue"},
100+
{"SecondEntry", "SecondValue"},
101+
{"ThirdEntry", "ThirdValue"},
102+
}))
103+
.Calling(c => c.ValidDistributedCacheEntriesAction(From.Services<IDistributedCache>()))
104+
.ShouldReturn()
105+
.Ok();
106+
}
107+
77108
[Fact]
78109
public void WithEntryAndEntriesShouldSetCorrectValues()
79110
{
@@ -115,6 +146,26 @@ public void AndAlsoShouldWorkCorrectly()
115146
.Ok();
116147
}
117148

149+
[Fact]
150+
public void AndAlsoWithStringsShouldWorkCorrectly()
151+
{
152+
MyController<DistributedCacheController>
153+
.Instance()
154+
.WithDistributedCache(distributedCache => distributedCache
155+
.WithEntry("FirstEntry", "FirstValue")
156+
.AndAlso()
157+
.WithEntry("SecondEntry", "SecondValue")
158+
.AndAlso()
159+
.WithEntries(new Dictionary<string, string>()
160+
{
161+
{"ThirdEntry", "ThirdValue"},
162+
{"FourthEntry", "FourthValue"},
163+
}))
164+
.Calling(c => c.ValidDistributedCacheEntriesAction(From.Services<IDistributedCache>()))
165+
.ShouldReturn()
166+
.Ok();
167+
}
168+
118169
[Fact]
119170
public void WithCacheBuilderWithKeyShouldSetCorrectValues()
120171
{
@@ -144,6 +195,20 @@ public void WithCacheBuilderWithKeyWithValueShouldSetCorrectValues()
144195
.Ok();
145196
}
146197

198+
[Fact]
199+
public void WithCacheBuilderWithKeyWithStringValueShouldSetCorrectValues()
200+
{
201+
MyController<DistributedCacheController>
202+
.Instance()
203+
.WithDistributedCache(distributedCache => distributedCache
204+
.WithEntry(entry => entry
205+
.WithKey("FirstEntry")
206+
.WithValue("FirstValue")))
207+
.Calling(c => c.ValidDistributedCacheEntryAction(From.Services<IDistributedCache>()))
208+
.ShouldReturn()
209+
.Ok();
210+
}
211+
147212
[Fact]
148213
public void WithCacheBuilderWithKeyBuilderShouldSetCorrectValues()
149214
{
@@ -163,6 +228,23 @@ public void WithCacheBuilderWithKeyBuilderShouldSetCorrectValues()
163228
.Ok();
164229
}
165230

231+
[Fact]
232+
public void WithCacheBuilderWithKeyBuilderAndStringValueShouldSetCorrectValues()
233+
{
234+
MyController<DistributedCacheController>
235+
.Instance()
236+
.WithDistributedCache(distributedCache => distributedCache
237+
.WithEntry(entry => entry
238+
.WithKey("FirstEntry")
239+
.WithValue("FirstValue")
240+
.WithAbsoluteExpiration(DateTimeOffset.MaxValue)
241+
.WithAbsoluteExpirationRelativeToNow(TimeSpan.MaxValue)
242+
.WithSlidingExpiration(TimeSpan.MaxValue)))
243+
.Calling(c => c.ValidDistributedCacheEntryAction(From.Services<IDistributedCache>()))
244+
.ShouldReturn()
245+
.Ok();
246+
}
247+
166248
[Fact]
167249
public void WithCacheBuilderWithKeyBuilderAndAlsoShouldSetCorrectValues()
168250
{

test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/DataTests/DistributedCacheEntryTestBuilderTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,43 @@ public void WithValueShouldThrowExceptionWithIncorrectValue()
9393
"When calling AddDistributedCacheAction action in MvcController expected distributed cache to have an entry with 'test' key and the given value, but in fact it was different.");
9494
}
9595

96+
[Fact]
97+
public void WithValueShouldNotThrowExceptionWithCorrectStringValue()
98+
{
99+
MyController<MvcController>
100+
.Instance()
101+
.Calling(c => c.AddDistributedCacheActionWithStringValueEntries())
102+
.ShouldHave()
103+
.DistributedCache(distributedCache => distributedCache
104+
.ContainingEntry(entry => entry
105+
.WithKey("test")
106+
.WithValue("testValue")))
107+
.AndAlso()
108+
.ShouldReturn()
109+
.Ok();
110+
}
111+
112+
[Fact]
113+
public void WithValueShouldThrowExceptionWithIncorrectStringValue()
114+
{
115+
Test.AssertException<DataProviderAssertionException>(
116+
() =>
117+
{
118+
MyController<MvcController>
119+
.Instance()
120+
.Calling(c => c.AddDistributedCacheActionWithStringValueEntries())
121+
.ShouldHave()
122+
.DistributedCache(distributedCache => distributedCache
123+
.ContainingEntry(entry => entry
124+
.WithKey("test")
125+
.WithValue("invalidValue")))
126+
.AndAlso()
127+
.ShouldReturn()
128+
.Ok();
129+
},
130+
"When calling AddDistributedCacheActionWithStringValueEntries action in MvcController expected distributed cache to have an entry with 'test' key and the given value, but in fact it was different.");
131+
}
132+
96133
[Fact]
97134
public void WithAbsoluteExpirationShouldNotThrowExceptionWithCorrectValue()
98135
{

test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/DataTests/DistributedCacheTestBuilderTests.cs

Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,39 @@ public void ContainingEntryShouldThrowExceptionWithIncorrectEntry()
5555
"When calling AddDistributedCacheAction action in MvcController expected distributed cache to have an entry with the given key, but such was not found.");
5656
}
5757

58+
[Fact]
59+
public void ContainingEntryWithStringValueShouldNotThrowExceptionWithCorrectEntry()
60+
{
61+
MyController<MvcController>
62+
.Instance()
63+
.Calling(c => c.AddDistributedCacheActionWithStringValueEntries())
64+
.ShouldHave()
65+
.DistributedCache(cache => cache
66+
.ContainingEntry("test", "testValue"))
67+
.AndAlso()
68+
.ShouldReturn()
69+
.Ok();
70+
}
71+
72+
[Fact]
73+
public void ContainingEntryWithStringValueShouldThrowExceptionWithIncorrectEntry()
74+
{
75+
Test.AssertException<DataProviderAssertionException>(
76+
() =>
77+
{
78+
MyController<MvcController>
79+
.Instance()
80+
.Calling(c => c.AddDistributedCacheActionWithStringValueEntries())
81+
.ShouldHave()
82+
.DistributedCache(cache => cache
83+
.ContainingEntry("invalid", "bad"))
84+
.AndAlso()
85+
.ShouldReturn()
86+
.Ok();
87+
},
88+
"When calling AddDistributedCacheActionWithStringValueEntries action in MvcController expected distributed cache to have an entry with the given key, but such was not found.");
89+
}
90+
5891
[Fact]
5992
public void ContainingEntryWithKeyShouldNotThrowExceptionWithCorrectKey()
6093
{
@@ -127,6 +160,39 @@ public void ContainingEntryWithValueShouldThrowExceptionWithIncorrectValue()
127160
"When calling AddDistributedCacheAction action in MvcController expected distributed cache to have an entry with the given value, but such was not found.");
128161
}
129162

163+
[Fact]
164+
public void ContainingEntryWithStringValueShouldNotThrowExceptionWithCorrectValue()
165+
{
166+
MyController<MvcController>
167+
.Instance()
168+
.Calling(c => c.AddDistributedCacheActionWithStringValueEntries())
169+
.ShouldHave()
170+
.DistributedCache(cache => cache
171+
.ContainingEntryWithValue("testValue"))
172+
.AndAlso()
173+
.ShouldReturn()
174+
.Ok();
175+
}
176+
177+
[Fact]
178+
public void ContainingEntryWithStringValueShouldThrowExceptionWithIncorrectValue()
179+
{
180+
Test.AssertException<DataProviderAssertionException>(
181+
() =>
182+
{
183+
MyController<MvcController>
184+
.Instance()
185+
.Calling(c => c.AddDistributedCacheActionWithStringValueEntries())
186+
.ShouldHave()
187+
.DistributedCache(cache => cache
188+
.ContainingEntryWithValue("badValue"))
189+
.AndAlso()
190+
.ShouldReturn()
191+
.Ok();
192+
},
193+
"When calling AddDistributedCacheActionWithStringValueEntries action in MvcController expected distributed cache to have an entry with the given value, but such was not found.");
194+
}
195+
130196
[Fact]
131197
public void ContainingEntryWithOptionsShouldNotThrowExceptionWithCorrectEntry()
132198
{
@@ -152,7 +218,7 @@ public void ContainingEntryWithOptionsShouldNotThrowExceptionWithCorrectEntry()
152218
public void ContainingEntryWithOptionsShouldThrowExceptionWithIncorrectAbsoluteExpiration()
153219
{
154220
var cacheValue = new byte[] { 127, 127, 127 };
155-
221+
156222
Test.AssertException<DataProviderAssertionException>(
157223
() =>
158224
{
@@ -178,7 +244,7 @@ public void ContainingEntryWithOptionsShouldThrowExceptionWithIncorrectAbsoluteE
178244
public void ContainingEntryWithOptionsShouldThrowExceptionWithIncorrectAbsoluteExpirationRelativeToNow()
179245
{
180246
var cacheValue = new byte[] { 127, 127, 127 };
181-
247+
182248
Test.AssertException<DataProviderAssertionException>(
183249
() =>
184250
{
@@ -204,7 +270,7 @@ public void ContainingEntryWithOptionsShouldThrowExceptionWithIncorrectAbsoluteE
204270
public void ContainingEntryWithOptionsShouldThrowExceptionWithIncorrectSlidingExpiration()
205271
{
206272
var cacheValue = new byte[] { 127, 127, 127 };
207-
273+
208274
Test.AssertException<DataProviderAssertionException>(
209275
() =>
210276
{
@@ -229,8 +295,6 @@ public void ContainingEntryWithOptionsShouldThrowExceptionWithIncorrectSlidingEx
229295
[Fact]
230296
public void ContainingEntriesShouldNotThrowExceptionWithCorrectEntries()
231297
{
232-
var cacheValue = new byte[] { 127, 127, 127 };
233-
234298
MyController<MvcController>
235299
.Instance()
236300
.Calling(c => c.AddDistributedCacheAction())
@@ -249,8 +313,6 @@ public void ContainingEntriesShouldNotThrowExceptionWithCorrectEntries()
249313
[Fact]
250314
public void ContainingEntriesShouldThrowExceptionWithIncorrectFewEntriesCount()
251315
{
252-
var cacheValue = new byte[] { 127, 127, 127 };
253-
254316
Test.AssertException<DataProviderAssertionException>(
255317
() =>
256318
{
@@ -273,8 +335,6 @@ public void ContainingEntriesShouldThrowExceptionWithIncorrectFewEntriesCount()
273335
[Fact]
274336
public void ContainingEntriesShouldThrowExceptionWithIncorrectManyEntriesCount()
275337
{
276-
var cacheValue = new byte[] { 127, 127, 127 };
277-
278338
Test.AssertException<DataProviderAssertionException>(
279339
() =>
280340
{
@@ -296,6 +356,70 @@ public void ContainingEntriesShouldThrowExceptionWithIncorrectManyEntriesCount()
296356
"When calling AddDistributedCacheAction action in MvcController expected distributed cache to have 3 entries, but in fact found 2.");
297357
}
298358

359+
[Fact]
360+
public void ContainingEntriesWithStringValuesShouldNotThrowExceptionWithCorrectEntries()
361+
{
362+
MyController<MvcController>
363+
.Instance()
364+
.Calling(c => c.AddDistributedCacheActionWithStringValueEntries())
365+
.ShouldHave()
366+
.DistributedCache(cache => cache
367+
.ContainingEntries(new Dictionary<string, string>
368+
{
369+
{"test", "testValue"},
370+
{"another", "anotherValue"},
371+
}))
372+
.AndAlso()
373+
.ShouldReturn()
374+
.Ok();
375+
}
376+
377+
[Fact]
378+
public void ContainingEntriesWithStringValuesShouldThrowExceptionWithIncorrectFewEntriesCount()
379+
{
380+
Test.AssertException<DataProviderAssertionException>(
381+
() =>
382+
{
383+
MyController<MvcController>
384+
.Instance()
385+
.Calling(c => c.AddDistributedCacheActionWithStringValueEntries())
386+
.ShouldHave()
387+
.DistributedCache(cache => cache
388+
.ContainingEntries(new Dictionary<string, string>
389+
{
390+
{"test", "testValue"}
391+
}))
392+
.AndAlso()
393+
.ShouldReturn()
394+
.Ok();
395+
},
396+
"When calling AddDistributedCacheActionWithStringValueEntries action in MvcController expected distributed cache to have 1 entry, but in fact found 2.");
397+
}
398+
399+
[Fact]
400+
public void ContainingEntriesWithStringValuesShouldThrowExceptionWithIncorrectManyEntriesCount()
401+
{
402+
Test.AssertException<DataProviderAssertionException>(
403+
() =>
404+
{
405+
MyController<MvcController>
406+
.Instance()
407+
.Calling(c => c.AddDistributedCacheActionWithStringValueEntries())
408+
.ShouldHave()
409+
.DistributedCache(cache => cache
410+
.ContainingEntries(new Dictionary<string, string>
411+
{
412+
{"test", "testValue"},
413+
{"another", "anotherValue"},
414+
{"missing", "missingValue"}
415+
}))
416+
.AndAlso()
417+
.ShouldReturn()
418+
.Ok();
419+
},
420+
"When calling AddDistributedCacheActionWithStringValueEntries action in MvcController expected distributed cache to have 3 entries, but in fact found 2.");
421+
}
422+
299423
[Fact]
300424
public void AndAlsoShouldWorkCorrectly()
301425
{
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.Utilities
2+
{
3+
using Mvc.Utilities;
4+
using Xunit;
5+
6+
public class BytesHelperTests
7+
{
8+
[Fact]
9+
public void BytesHelperShouldReturnEmptyArrayOnEmptyString()
10+
{
11+
var input = string.Empty;
12+
var expectedBytes = new byte[0];
13+
14+
var actualBytes = BytesHelper.GetBytes(input);
15+
16+
Assert.Equal(expectedBytes, actualBytes);
17+
}
18+
19+
[Fact]
20+
public void BytesHelperShouldReturnCorrectBytesForString()
21+
{
22+
var input = "Test";
23+
var expectedBytes = new byte[4] {84, 101, 115, 116};
24+
25+
var actualBytes = BytesHelper.GetBytes(input);
26+
27+
Assert.Equal(expectedBytes, actualBytes);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)