Skip to content

Commit 97463e4

Browse files
authored
Merge pull request #343 - Added unit tests for MusicStore - StoreManagerController and ShoppingCartController
Added unit tests for MusicStore - StoreManagerController and ShoppingCartController
2 parents 8b0a9ee + fab42dc commit 97463e4

File tree

2 files changed

+374
-0
lines changed

2 files changed

+374
-0
lines changed

samples/MusicStore/MusicStore.Test/Controllers/ShoppingCartControllerTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,37 @@ public void RemoveFromCartShouldRemoveItemFromCart()
134134
});
135135
}
136136

137+
[Fact]
138+
public void RemoveFromCartShouldReturnNoItemsWhenWrongCartIdIsPassed()
139+
{
140+
var cartId = "CartId_А";
141+
var wrongCartItemId = -1;
142+
var numberOfItem = 3;
143+
var unitPrice = 5;
144+
145+
MyMvc
146+
.Controller<ShoppingCartController>()
147+
.WithSession(session => session.WithEntry("Session", cartId))
148+
.WithData(db => db
149+
.WithEntities(entities =>
150+
{
151+
var cartItems = CreateTestCartItems(cartId, unitPrice, numberOfItem);
152+
entities.AddRange(cartItems.Select(n => n.Album).Distinct());
153+
entities.AddRange(cartItems);
154+
}))
155+
.Calling(c => c.RemoveFromCart(wrongCartItemId, CancellationToken.None))
156+
.ShouldReturn()
157+
.Json(json => json
158+
.WithModelOfType<ShoppingCartRemoveViewModel>()
159+
.Passing(model =>
160+
{
161+
Assert.Equal(numberOfItem, model.CartCount);
162+
Assert.Equal((numberOfItem) * unitPrice, model.CartTotal);
163+
Assert.Equal(0, model.ItemCount);
164+
Assert.Equal("Could not find this item, nothing has been removed from your shopping cart.", model.Message);
165+
}));
166+
}
167+
137168
private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem)
138169
{
139170
var albums = CreateTestAlbums(itemPrice);
Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
namespace MusicStore.Test.Controllers
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.AspNetCore.Mvc.Rendering;
9+
using Microsoft.Extensions.Caching.Memory;
10+
using MusicStore.Areas.Admin.Controllers;
11+
using MusicStore.Models;
12+
using MyTested.AspNetCore.Mvc;
13+
using Xunit;
14+
15+
public class StoreManagerControllerTest
16+
{
17+
[Fact]
18+
public void ControllerShouldHaveAuthorizeAndAreaAttributes()
19+
{
20+
MyMvc
21+
.Controller<StoreManagerController>()
22+
.ShouldHave()
23+
.Attributes(attr => attr
24+
.RestrictingForAuthorizedRequests()
25+
.SpecifyingArea("Admin"));
26+
}
27+
28+
[Fact]
29+
public void IndexShouldReturnViewWithAlbums()
30+
{
31+
var itemsCount = 10;
32+
33+
MyMvc
34+
.Controller<StoreManagerController>()
35+
.WithData(db => db
36+
.WithEntities(entities =>
37+
{
38+
var albums = CreateTestAlbums(itemsCount);
39+
entities.AddRange(albums);
40+
}))
41+
.Calling(x => x.Index())
42+
.ShouldReturn()
43+
.View(view => view
44+
.WithModelOfType<List<Album>>()
45+
.Passing(model =>
46+
{
47+
Assert.NotNull(model);
48+
Assert.Equal(itemsCount, model.Count());
49+
}
50+
));
51+
}
52+
53+
[Fact]
54+
public void DetailsShouldHaveAlbumInMemoryCacheAndShouldReturnViewWithAlbum()
55+
{
56+
MyMvc
57+
.Controller<StoreManagerController>()
58+
.WithData(db => db
59+
.WithEntities(entities =>
60+
{
61+
var albums = CreateTestAlbums(10);
62+
entities.AddRange(albums);
63+
}))
64+
.Calling(x => x.Details(From.Services<IMemoryCache>(), 3))
65+
.ShouldHave()
66+
.MemoryCache(cache => cache
67+
.ContainingEntry(entry => entry
68+
.WithKey("album_3")
69+
.WithSlidingExpiration(TimeSpan.FromMinutes(10))
70+
.WithValueOfType<Album>()
71+
.Passing(a => a.AlbumId == 3)))
72+
.AndAlso()
73+
.ShouldReturn()
74+
.View(view => view
75+
.WithModelOfType<Album>()
76+
.Passing(model =>
77+
{
78+
Assert.NotNull(model);
79+
Assert.Equal(3, model.AlbumId);
80+
}
81+
));
82+
}
83+
84+
[Fact]
85+
public void DetailsShouldReturnNotFoundWhenPassingWrongAlbumId()
86+
{
87+
MyMvc
88+
.Controller<StoreManagerController>()
89+
.WithData(db => db
90+
.WithEntities(entities =>
91+
{
92+
var albums = CreateTestAlbums(10);
93+
entities.AddRange(albums);
94+
}))
95+
.Calling(x => x.Details(From.Services<IMemoryCache>(), -1))
96+
.ShouldReturn()
97+
.NotFound();
98+
}
99+
100+
[Fact]
101+
public void CreatePostShouldHaveValidateAntiForgeryTokenAndPostAttributes()
102+
{
103+
MyMvc
104+
.Controller<StoreManagerController>()
105+
.Calling(x => x.Create(
106+
new Album(),
107+
From.Services<IMemoryCache>(),
108+
CancellationToken.None))
109+
.ShouldHave()
110+
.ActionAttributes(attr => attr
111+
.ValidatingAntiForgeryToken()
112+
.RestrictingForHttpMethod<HttpPostAttribute>());
113+
}
114+
115+
[Fact]
116+
public void CreateGetShouldReturnViewWithViewBagWithGenresAndArtists()
117+
{
118+
var genres = CreateTestGenres();
119+
var artists = CreateTestArtists();
120+
121+
MyMvc
122+
.Controller<StoreManagerController>()
123+
.WithData(db => db
124+
.WithEntities(entities =>
125+
{
126+
entities.AddRange(genres);
127+
entities.AddRange(artists);
128+
}))
129+
.Calling(x => x.Create())
130+
.ShouldHave()
131+
.ViewBag(viewBag => viewBag
132+
.ContainingEntry("GenreId", new SelectList(genres, "GenreId", "Name"))
133+
.ContainingEntry("ArtistId", new SelectList(artists, "ArtistId", "Name")))
134+
.AndAlso()
135+
.ShouldReturn()
136+
.View(view => view.WithDefaultName())
137+
.AndAlso();
138+
}
139+
140+
[Fact]
141+
public void CreatePostShouldHaveInvalidModelStateAndReturnDefaultViewWhenPassedEmptyAlbum()
142+
{
143+
MyMvc
144+
.Controller<StoreManagerController>()
145+
.Calling(x => x.Create(
146+
new Album(),
147+
From.Services<IMemoryCache>(),
148+
CancellationToken.None))
149+
.ShouldHave()
150+
.InvalidModelState()
151+
.AndAlso()
152+
.ShouldReturn()
153+
.View(view => view
154+
.WithDefaultName()
155+
.WithModelOfType<Album>());
156+
}
157+
158+
[Fact]
159+
public void RemoveAlbumGetShouldReturnViewWithAlbum()
160+
{
161+
var albums = CreateTestAlbums(10);
162+
var albumId = 3;
163+
var album = albums.FirstOrDefault(x => x.AlbumId == albumId);
164+
165+
MyMvc
166+
.Controller<StoreManagerController>()
167+
.WithData(db => db
168+
.WithEntities(entities =>
169+
{
170+
entities.AddRange(albums);
171+
}))
172+
.Calling(x => x.RemoveAlbum(albumId))
173+
.ShouldReturn()
174+
.View(view => view
175+
.WithDefaultName()
176+
.WithModelOfType<Album>()
177+
.Passing(model =>
178+
{
179+
Assert.NotNull(model);
180+
Assert.Equal(album, model);
181+
}));
182+
}
183+
184+
[Fact]
185+
public void RemoveAlbumGetShouldReturnNotFoundCalledWithNotExistingAlbumId()
186+
{
187+
var albums = CreateTestAlbums(10);
188+
var albumId = -1;
189+
190+
MyMvc
191+
.Controller<StoreManagerController>()
192+
.WithData(db => db
193+
.WithEntities(entities =>
194+
{
195+
entities.AddRange(albums);
196+
}))
197+
.Calling(x => x.RemoveAlbum(albumId))
198+
.ShouldReturn()
199+
.NotFound();
200+
}
201+
202+
[Fact]
203+
public void RemoveAlbumConfirmedShouldDeleteAlbum()
204+
{
205+
var albums = CreateTestAlbums(10);
206+
var albumId = 3;
207+
208+
MyMvc
209+
.Controller<StoreManagerController>()
210+
.WithData(db => db
211+
.WithEntities(entities =>
212+
{
213+
entities.AddRange(albums);
214+
}))
215+
.Calling(x => x.RemoveAlbumConfirmed(
216+
From.Services<IMemoryCache>(),
217+
albumId,
218+
CancellationToken.None))
219+
.ShouldHave()
220+
.Data(data => data.WithEntities(context =>
221+
{
222+
var album = context.Find<Album>(albumId);
223+
Assert.Null(album);
224+
}))
225+
.AndAlso()
226+
.ShouldReturn()
227+
.Redirect(redirect => redirect
228+
.To<StoreManagerController>(c => c.Index()));
229+
}
230+
231+
[Fact]
232+
public void RemoveAlbumConfirmedShouldReturnNotFoundCalledWithNotExistingAlbumId()
233+
{
234+
var albums = CreateTestAlbums(10);
235+
var albumId = -1;
236+
237+
MyMvc
238+
.Controller<StoreManagerController>()
239+
.WithData(db => db
240+
.WithEntities(entities =>
241+
{
242+
entities.AddRange(albums);
243+
}))
244+
.Calling(x => x.RemoveAlbumConfirmed(
245+
From.Services<IMemoryCache>(),
246+
albumId,
247+
CancellationToken.None))
248+
.ShouldReturn()
249+
.NotFound();
250+
}
251+
252+
[Fact]
253+
public void EditPostShouldHaveValidateAntiForgeryTokenAndPostAttributes()
254+
{
255+
MyMvc
256+
.Controller<StoreManagerController>()
257+
.Calling(x => x.Edit(
258+
From.Services<IMemoryCache>(),
259+
new Album(),
260+
CancellationToken.None))
261+
.ShouldHave()
262+
.ActionAttributes(attr => attr
263+
.ValidatingAntiForgeryToken()
264+
.RestrictingForHttpMethod<HttpPostAttribute>());
265+
}
266+
267+
[Fact]
268+
public void EditShouldReturnNotFoundCalledWithNotExistingAlbumId()
269+
{
270+
var albums = CreateTestAlbums(10);
271+
var albumId = -1;
272+
273+
MyMvc
274+
.Controller<StoreManagerController>()
275+
.WithData(db => db
276+
.WithEntities(entities =>
277+
{
278+
entities.AddRange(albums);
279+
}))
280+
.Calling(x => x.Edit(albumId))
281+
.ShouldReturn()
282+
.NotFound();
283+
}
284+
285+
[Fact]
286+
public void EditShouldReturnDefaultViewWithAlbum()
287+
{
288+
var albums = CreateTestAlbums(10);
289+
var albumId = 3;
290+
var album = albums.FirstOrDefault(x => x.AlbumId == albumId);
291+
292+
MyMvc
293+
.Controller<StoreManagerController>()
294+
.WithData(db => db
295+
.WithEntities(entities =>
296+
{
297+
entities.AddRange(albums);
298+
}))
299+
.Calling(x => x.Edit(albumId))
300+
.ShouldReturn()
301+
.View(view => view
302+
.WithDefaultName()
303+
.WithModelOfType<Album>()
304+
.Passing(model =>
305+
{
306+
Assert.NotNull(model);
307+
Assert.Equal(album, model);
308+
}));
309+
}
310+
311+
private static Album[] CreateTestAlbums(int itemsCount)
312+
{
313+
return Enumerable.Range(1, itemsCount).Select(n =>
314+
new Album
315+
{
316+
AlbumId = n,
317+
Price = 10,
318+
Genre = new Genre(),
319+
Artist = new Artist()
320+
}).ToArray();
321+
}
322+
323+
private static Genre[] CreateTestGenres()
324+
{
325+
return Enumerable.Range(1, 10).Select(n =>
326+
new Genre
327+
{
328+
GenreId = n,
329+
Name = "Genre Name " + n,
330+
}).ToArray();
331+
}
332+
333+
private static Artist[] CreateTestArtists()
334+
{
335+
return Enumerable.Range(1, 10).Select(n =>
336+
new Artist
337+
{
338+
ArtistId = n + 1,
339+
Name = "Artist Name " + n,
340+
}).ToArray();
341+
}
342+
}
343+
}

0 commit comments

Comments
 (0)