|
1 |
| -# Session & Cache |
| 1 | +# Session & Cache |
| 2 | + |
| 3 | +In this section we will cover two of the most commonly used modules for data persistence between different requests - session and cache. |
| 4 | + |
| 5 | +## Session |
| 6 | + |
| 7 | +To use the built-in session capabilities of My Tested ASP.NET Core MVC, we need to add **"MyTested.AspNetCore.Mvc.Session"** as a dependency: |
| 8 | + |
| 9 | +```json |
| 10 | +"dependencies": { |
| 11 | + "dotnet-test-xunit": "2.2.0-*", |
| 12 | + "xunit": "2.2.0-*", |
| 13 | + "Moq": "4.6.38-*", |
| 14 | + "MyTested.AspNetCore.Mvc.Authentication": "1.0.0", |
| 15 | + "MyTested.AspNetCore.Mvc.Controllers": "1.0.0", |
| 16 | + "MyTested.AspNetCore.Mvc.DependencyInjection": "1.0.0", |
| 17 | + "MyTested.AspNetCore.Mvc.EntityFrameworkCore": "1.0.0", |
| 18 | + "MyTested.AspNetCore.Mvc.Http": "1.0.0", |
| 19 | + "MyTested.AspNetCore.Mvc.ModelState": "1.0.0", |
| 20 | + "MyTested.AspNetCore.Mvc.Models": "1.0.0", |
| 21 | + "MyTested.AspNetCore.Mvc.Options": "1.0.0", |
| 22 | + "MyTested.AspNetCore.Mvc.Session": "1.0.0", // <--- |
| 23 | + "MyTested.AspNetCore.Mvc.ViewActionResults": "1.0.0", |
| 24 | + "MusicStore": "*" |
| 25 | +}, |
| 26 | +``` |
| 27 | + |
| 28 | +Adding this package will replace the default session services with scoped mocks, which are empty at the beginning of each test. It's quite easy to test with them. Let's see! :) |
| 29 | + |
| 30 | +We will test the **"AddToCart"** action in the **"ShoppingCartController"**. If you examine the method, you will see it calls **"ShoppingCart.GetCart"**, which creates a session entry containing the cart ID: |
| 31 | + |
| 32 | +```c# |
| 33 | +// code skipped for brevity |
| 34 | +
|
| 35 | +var cartId = context.Session.GetString("Session"); |
| 36 | + |
| 37 | +if (cartId == null) |
| 38 | +{ |
| 39 | + cartId = Guid.NewGuid().ToString(); |
| 40 | + context.Session.SetString("Session", cartId); |
| 41 | +} |
| 42 | + |
| 43 | +return cartId; |
| 44 | + |
| 45 | +// code skipped for brevity |
| 46 | +``` |
| 47 | + |
| 48 | +Let's assert that if the session is initially empty, an entry with **"Session"** key should be added after the action invocation. Go to the **"ShoppingCartControllerTest"** class and add the following test: |
| 49 | + |
| 50 | +```c# |
| 51 | +[Fact] |
| 52 | +public void AddToCartShouldPopulateSessionCartIfMissing() |
| 53 | + => MyController<ShoppingCartController> |
| 54 | + .Instance() |
| 55 | + .WithDbContext(db => db |
| 56 | + .WithEntities(entities => entities |
| 57 | + .Add(new Album { AlbumId = 1 }))) |
| 58 | + .Calling(c => c.AddToCart(1)) |
| 59 | + .ShouldHave() |
| 60 | + .Session(session => session |
| 61 | + .ContainingEntryWithKey("Session")); |
| 62 | +``` |
| 63 | + |
| 64 | +Next, let's assert that the cart item is actually saved into the database. We will need to provide a custom shopping cart ID by using the **"WithSession"** method: |
| 65 | + |
| 66 | +```c# |
| 67 | +[Fact] |
| 68 | +public void AddToCartShouldSaveTheAlbumsIntoDatabaseAndSession() |
| 69 | + => MyController<ShoppingCartController> |
| 70 | + .Instance() |
| 71 | + .WithSession(session => session.WithEntry("Session", "TestCart")) // <--- |
| 72 | + .WithDbContext(db => db |
| 73 | + .WithEntities(entities => entities |
| 74 | + .Add(new Album { AlbumId = 1 }))) |
| 75 | + .Calling(c => c.AddToCart(1)) |
| 76 | + .ShouldHave() |
| 77 | + .DbContext(db => db // <--- |
| 78 | + .WithSet<CartItem>(cartItems => cartItems |
| 79 | + .Any(a => a.AlbumId == 1 && a.CartId == "TestCart"))) |
| 80 | + .AndAlso() |
| 81 | + .ShouldReturn() |
| 82 | + .Redirect() |
| 83 | + .ToAction(nameof(ShoppingCartController.Index)); |
| 84 | +``` |
| 85 | + |
| 86 | +## Cache |
| 87 | + |
| 88 | +For the caching assertions, we will need **"MyTested.AspNetCore.Mvc.Caching"** as a dependency. Go and add it to the **"project.json"**: |
| 89 | + |
| 90 | +```json |
| 91 | +"dependencies": { |
| 92 | + "dotnet-test-xunit": "2.2.0-*", |
| 93 | + "xunit": "2.2.0-*", |
| 94 | + "Moq": "4.6.38-*", |
| 95 | + "MyTested.AspNetCore.Mvc.Authentication": "1.0.0", |
| 96 | + "MyTested.AspNetCore.Mvc.Caching": "1.0.0", // <--- |
| 97 | + "MyTested.AspNetCore.Mvc.Controllers": "1.0.0", |
| 98 | + "MyTested.AspNetCore.Mvc.DependencyInjection": "1.0.0", |
| 99 | + "MyTested.AspNetCore.Mvc.EntityFrameworkCore": "1.0.0", |
| 100 | + "MyTested.AspNetCore.Mvc.Http": "1.0.0", |
| 101 | + "MyTested.AspNetCore.Mvc.ModelState": "1.0.0", |
| 102 | + "MyTested.AspNetCore.Mvc.Models": "1.0.0", |
| 103 | + "MyTested.AspNetCore.Mvc.Options": "1.0.0", |
| 104 | + "MyTested.AspNetCore.Mvc.Session": "1.0.0", |
| 105 | + "MyTested.AspNetCore.Mvc.ViewActionResults": "1.0.0", |
| 106 | + "MusicStore": "*" |
| 107 | +}, |
| 108 | +``` |
| 109 | + |
| 110 | +Since the package automatically replaces the default memory cache services with scoped mocks, we no longer need this code in the **"TestStartup"** class: |
| 111 | + |
| 112 | +```c# |
| 113 | +services.ReplaceLifetime<IMemoryCache>(ServiceLifetime.Scoped); |
| 114 | +``` |
| 115 | + |
| 116 | +Remove the above line and rerun all tests to see them pass again. Remember! All scoped services reset their state for each test. The cache ones are not an exception. |
| 117 | + |
| 118 | +Now, we are going to write three tests against the **"Index"** action in the **"HomeController"**: |
| 119 | + |
| 120 | +```c# |
| 121 | +// code skipped for brevity |
| 122 | +
|
| 123 | +var cacheKey = "topselling"; |
| 124 | +List<Album> albums; |
| 125 | +if (!cache.TryGetValue(cacheKey, out albums)) |
| 126 | +{ |
| 127 | + albums = await GetTopSellingAlbumsAsync(dbContext, 6); |
| 128 | + |
| 129 | + if (albums != null && albums.Count > 0) |
| 130 | + { |
| 131 | + if (_appSettings.CacheDbResults) |
| 132 | + { |
| 133 | + cache.Set(cacheKey, albums, new MemoryCacheEntryOptions() |
| 134 | + .SetAbsoluteExpiration(TimeSpan.FromMinutes(10)) |
| 135 | + .SetPriority(CacheItemPriority.High)); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +return View(albums); |
| 141 | + |
| 142 | +// code skipped for brevity |
| 143 | +``` |
| 144 | + |
| 145 | +Before we begin, add this helper method to the **"HomeControllerTest"** class: |
| 146 | + |
| 147 | +```c# |
| 148 | +private static Album[] Albums |
| 149 | +{ |
| 150 | + get |
| 151 | + { |
| 152 | + var genres = Enumerable.Range(1, 10).Select(n => |
| 153 | + new Genre() |
| 154 | + { |
| 155 | + GenreId = n, |
| 156 | + Name = "Genre Name " + n, |
| 157 | + }).ToArray(); |
| 158 | + |
| 159 | + var artists = Enumerable.Range(1, 10).Select(n => |
| 160 | + new Artist() |
| 161 | + { |
| 162 | + ArtistId = n + 1, |
| 163 | + Name = "Artist Name " + n, |
| 164 | + }).ToArray(); |
| 165 | + |
| 166 | + var albums = Enumerable.Range(1, 10).Select(n => |
| 167 | + new Album() |
| 168 | + { |
| 169 | + Artist = artists[n - 1], |
| 170 | + ArtistId = n, |
| 171 | + Genre = genres[n - 1], |
| 172 | + GenreId = n, |
| 173 | + }).ToArray(); |
| 174 | + |
| 175 | + return albums; |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +OK, let's assert! :) |
| 181 | + |
| 182 | +First, we should test that no cache entries are saved if the **"CacheDbResults"** setting is set to **"false"**: |
| 183 | + |
| 184 | +```c# |
| 185 | +[Fact] |
| 186 | +public void IndexShouldNotUseCacheIfOptionsDisableIt() |
| 187 | + => MyController<HomeController> |
| 188 | + .Instance() |
| 189 | + .WithOptions(options => options |
| 190 | + .For<AppSettings>(settings => settings.CacheDbResults = false)) |
| 191 | + .WithDbContext(db => db |
| 192 | + .WithEntities(entities => entities.AddRange(Albums))) |
| 193 | + .Calling(c => c.Index( |
| 194 | + From.Services<MusicStoreContext>(), |
| 195 | + From.Services<IMemoryCache>())) |
| 196 | + .ShouldHave() |
| 197 | + .NoMemoryCache(); // <--- |
| 198 | +``` |
| 199 | + |
| 200 | +Unfortunately, the **"NoMemoryCache"** call will not work: |
| 201 | + |
| 202 | +```c# |
| 203 | +When calling Index action in HomeController expected to have memory cache with no entries, but in fact it had some. |
| 204 | +``` |
| 205 | + |
| 206 | +Unfortunately with straightforward action debugging we may not see what exactly is going on because the **"CacheDbResults"** is indeed **"false"**. The reason of the error lies in [Entity Framework Core's code](https://github.com/aspnet/EntityFramework/blob/f9adcb64fdf668163377beb14251e67d17f60fa0/src/Microsoft.EntityFrameworkCore/EntityFrameworkServiceCollectionExtensions.cs#L150). It uses the same memory cache service as the web application and guess what! It caches the database query call. But how to debug such issues? |
| 207 | + |
| 208 | +Easy! Add these lines: |
| 209 | + |
| 210 | +```c# |
| 211 | +.WithDbContext(db => db |
| 212 | + .WithEntities(entities => entities.AddRange(Albums))) |
| 213 | +.Calling(c => c.Index( |
| 214 | + From.Services<MusicStoreContext>(), |
| 215 | + From.Services<IMemoryCache>())) |
| 216 | +.ShouldPassForThe<IServiceProvider>(services => // <--- add these instead of NoMemoryCache |
| 217 | +{ |
| 218 | + var memoryCache = services.GetService<IMemoryCache>(); |
| 219 | +}); // <--- and put a breakpoint here |
| 220 | +``` |
| 221 | + |
| 222 | +Running the debugger will allow you to examine the actual values in the cache. |
| 223 | + |
| 224 | +<img src="/images/tutorial/nomemorycachedebug.jpn" alt="Debugging memory cache" /> |
| 225 | + |
| 226 | +One of the possible fixes is: |
| 227 | + |
| 228 | +```c# |
| 229 | +.Calling(c => c.Index( |
| 230 | + From.Services<MusicStoreContext>(), |
| 231 | + From.Services<IMemoryCache>())) |
| 232 | +.ShouldPassForThe<IServiceProvider>(services => Assert.Null(services // <--- |
| 233 | + .GetRequiredService<IMemoryCache>().Get("topselling"))); |
| 234 | +``` |
| 235 | + |
| 236 | +You may use custom mocks too but it is not necessary. |
| 237 | + |
| 238 | +Next, we should assert that with the **"CacheDbResults"** set to **"true"**, we should have saved cache entries from the database query: |
| 239 | + |
| 240 | +```c# |
| 241 | +[Fact] |
| 242 | +public void IndexShouldSaveCorrectCacheEntriesIfOptionsEnableIt() |
| 243 | + => MyController<HomeController> |
| 244 | + .Instance() |
| 245 | + .WithOptions(options => options |
| 246 | + .For<AppSettings>(settings => settings.CacheDbResults = true)) |
| 247 | + .WithDbContext(db => db |
| 248 | + .WithEntities(entities => entities.AddRange(Albums))) |
| 249 | + .Calling(c => c.Index( |
| 250 | + From.Services<MusicStoreContext>(), |
| 251 | + From.Services<IMemoryCache>())) |
| 252 | + .ShouldHave() |
| 253 | + .MemoryCache(cache => cache // <--- |
| 254 | + .ContainingEntry(entry => entry |
| 255 | + .WithKey("topselling") |
| 256 | + .WithPriority(CacheItemPriority.High) |
| 257 | + .WithAbsoluteExpirationRelativeToNow(TimeSpan.FromMinutes(10)) |
| 258 | + .WithValueOfType<List<Album>>() |
| 259 | + .Passing(albums => albums.Count == 6))) |
| 260 | + .AndAlso() |
| 261 | + .ShouldReturn() |
| 262 | + .View() |
| 263 | + .WithModelOfType<List<Album>>() |
| 264 | + .Passing(albums => albums.Count == 6); |
| 265 | +``` |
| 266 | + |
| 267 | +Finally, we should validate that if the cache contains the albums entry, no database query should be called. We will use an empty database and assert the view model: |
| 268 | + |
| 269 | +```c# |
| 270 | +[Fact] |
| 271 | +public void IndexShouldGetAlbumsFromCacheIfEntryExists() |
| 272 | + => MyController<HomeController> |
| 273 | + .Instance() |
| 274 | + .WithOptions(options => options |
| 275 | + .For<AppSettings>(settings => settings.CacheDbResults = true)) |
| 276 | + .WithMemoryCache(cache => cache |
| 277 | + .WithEntry("topselling", Albums.Take(6).ToList())) |
| 278 | + .Calling(c => c.Index( |
| 279 | + From.Services<MusicStoreContext>(), |
| 280 | + From.Services<IMemoryCache>())) |
| 281 | + .ShouldReturn() |
| 282 | + .View() |
| 283 | + .WithModelOfType<List<Album>>() |
| 284 | + .Passing(albums => albums.Count == 6); |
| 285 | +``` |
| 286 | + |
| 287 | +This way we validate entries are retrieved from cache and from the actual database (which is empty for this particular test). |
| 288 | + |
| 289 | +## Section summary |
| 290 | + |
| 291 | +Session and cache are fun. By using the **"WithSession"** and **"WithMemoryCache"** methods, you prepare the values to be available during the action call. On the other side, the **"ShouldHave().MemoryCache()"** and **"ShouldHave().Session()"** extensions allows you to assert their values after the action call. The same principle applies to the [ViewBag, ViewData & TempData](/tutorial/viewbagviewdatatempdata.html). |
0 commit comments