Skip to content

Commit 8ddda4a

Browse files
author
VGGeorgiev
committed
Added unit tests for MusicStore.Web.Admin.StoreManagerController and MusicStore.Web.ShoppingCartController
1 parent 8b0a9ee commit 8ddda4a

File tree

2 files changed

+359
-0
lines changed

2 files changed

+359
-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: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.Rendering;
3+
using Microsoft.Extensions.Caching.Memory;
4+
using MusicStore.Areas.Admin.Controllers;
5+
using MusicStore.Models;
6+
using MyTested.AspNetCore.Mvc;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Threading;
11+
using Xunit;
12+
13+
namespace MusicStore.Test.Controllers
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(new Album(), From.Services<IMemoryCache>(), CancellationToken.None))
106+
.ShouldHave()
107+
.ActionAttributes(attr => attr
108+
.ValidatingAntiForgeryToken()
109+
.RestrictingForHttpMethod<HttpPostAttribute>());
110+
}
111+
112+
[Fact]
113+
public void CreateGetShouldReturnViewWithViewBagWithGenresAndArtists()
114+
{
115+
var genres = CreateTestGenres();
116+
var artists = CreateTestArtists();
117+
118+
MyMvc
119+
.Controller<StoreManagerController>()
120+
.WithData(db => db
121+
.WithEntities(entities =>
122+
{
123+
entities.AddRange(genres);
124+
entities.AddRange(artists);
125+
}))
126+
.Calling(x => x.Create())
127+
.ShouldHave()
128+
.ViewBag(viewBag => viewBag
129+
.ContainingEntry("GenreId", new SelectList(genres, "GenreId", "Name"))
130+
.ContainingEntry("ArtistId", new SelectList(artists, "ArtistId", "Name")))
131+
.AndAlso()
132+
.ShouldReturn()
133+
.View(view => view.WithDefaultName())
134+
.AndAlso();
135+
}
136+
137+
[Fact]
138+
public void CreatePostShouldHaveInvalidModelStateAndReturnDefaultViewWhenPassedEmptyAlbum()
139+
{
140+
MyMvc
141+
.Controller<StoreManagerController>()
142+
.Calling(x => x.Create(new Album(), From.Services<IMemoryCache>(), CancellationToken.None))
143+
.ShouldHave()
144+
.InvalidModelState()
145+
.AndAlso()
146+
.ShouldReturn()
147+
.View(view => view
148+
.WithDefaultName()
149+
.WithModelOfType<Album>());
150+
}
151+
152+
[Fact]
153+
public void RemoveAlbumGetShouldReturnViewWithAlbum()
154+
{
155+
var albums = CreateTestAlbums(10);
156+
var albumId = 3;
157+
var album = albums.FirstOrDefault(x => x.AlbumId == albumId);
158+
159+
MyMvc
160+
.Controller<StoreManagerController>()
161+
.WithData(db => db
162+
.WithEntities(entities =>
163+
{
164+
entities.AddRange(albums);
165+
}))
166+
.Calling(x => x.RemoveAlbum(albumId))
167+
.ShouldReturn()
168+
.View(view => view
169+
.WithDefaultName()
170+
.WithModelOfType<Album>()
171+
.Passing(model =>
172+
{
173+
Assert.NotNull(model);
174+
Assert.Equal(album, model);
175+
}));
176+
}
177+
178+
[Fact]
179+
public void RemoveAlbumGetShouldReturnNotFoundCalledWithNotExistingAlbumId()
180+
{
181+
var albums = CreateTestAlbums(10);
182+
var albumId = -1;
183+
184+
MyMvc
185+
.Controller<StoreManagerController>()
186+
.WithData(db => db
187+
.WithEntities(entities =>
188+
{
189+
entities.AddRange(albums);
190+
}))
191+
.Calling(x => x.RemoveAlbum(albumId))
192+
.ShouldReturn()
193+
.NotFound();
194+
}
195+
196+
[Fact]
197+
public void RemoveAlbumConfirmedShouldDeleteAlbum()
198+
{
199+
var albums = CreateTestAlbums(10);
200+
var albumId = 3;
201+
202+
MyMvc
203+
.Controller<StoreManagerController>()
204+
.WithData(db => db
205+
.WithEntities(entities =>
206+
{
207+
entities.AddRange(albums);
208+
}))
209+
.Calling(x => x.RemoveAlbumConfirmed(From.Services<IMemoryCache>(), albumId, CancellationToken.None))
210+
.ShouldHave()
211+
.Data(data => data.WithEntities(context =>
212+
{
213+
var album = context.Find<Album>(albumId);
214+
Assert.Null(album);
215+
}))
216+
.AndAlso()
217+
.ShouldReturn()
218+
.Redirect(redirect => redirect
219+
.To<StoreManagerController>(c => c.Index()));
220+
}
221+
222+
[Fact]
223+
public void RemoveAlbumConfirmedShouldReturnNotFoundCalledWithNotExistingAlbumId()
224+
{
225+
var albums = CreateTestAlbums(10);
226+
var albumId = -1;
227+
228+
MyMvc
229+
.Controller<StoreManagerController>()
230+
.WithData(db => db
231+
.WithEntities(entities =>
232+
{
233+
entities.AddRange(albums);
234+
}))
235+
.Calling(x => x.RemoveAlbumConfirmed(From.Services<IMemoryCache>(), albumId, CancellationToken.None))
236+
.ShouldReturn()
237+
.NotFound();
238+
}
239+
240+
[Fact]
241+
public void EditPostShouldHaveValidateAntiForgeryTokenAndPostAttributes()
242+
{
243+
MyMvc
244+
.Controller<StoreManagerController>()
245+
.Calling(x => x.Edit(From.Services<IMemoryCache>(), new Album(), CancellationToken.None))
246+
.ShouldHave()
247+
.ActionAttributes(attr => attr
248+
.ValidatingAntiForgeryToken()
249+
.RestrictingForHttpMethod<HttpPostAttribute>());
250+
}
251+
252+
[Fact]
253+
public void EditShouldReturnNotFoundCalledWithNotExistingAlbumId()
254+
{
255+
var albums = CreateTestAlbums(10);
256+
var albumId = -1;
257+
258+
MyMvc
259+
.Controller<StoreManagerController>()
260+
.WithData(db => db
261+
.WithEntities(entities =>
262+
{
263+
entities.AddRange(albums);
264+
}))
265+
.Calling(x => x.Edit(albumId))
266+
.ShouldReturn()
267+
.NotFound();
268+
}
269+
270+
[Fact]
271+
public void EditShouldReturnDefaultViewWithAlbum()
272+
{
273+
var albums = CreateTestAlbums(10);
274+
var albumId = 3;
275+
var album = albums.FirstOrDefault(x => x.AlbumId == albumId);
276+
277+
MyMvc
278+
.Controller<StoreManagerController>()
279+
.WithData(db => db
280+
.WithEntities(entities =>
281+
{
282+
entities.AddRange(albums);
283+
}))
284+
.Calling(x => x.Edit(albumId))
285+
.ShouldReturn()
286+
.View(view => view
287+
.WithDefaultName()
288+
.WithModelOfType<Album>()
289+
.Passing(model =>
290+
{
291+
Assert.NotNull(model);
292+
Assert.Equal(album, model);
293+
}));
294+
}
295+
296+
private static Album[] CreateTestAlbums(int itemsCount)
297+
{
298+
return Enumerable.Range(1, itemsCount).Select(n =>
299+
new Album
300+
{
301+
AlbumId = n,
302+
Price = 10,
303+
Genre = new Genre(),
304+
Artist = new Artist()
305+
}).ToArray();
306+
}
307+
308+
private static Genre[] CreateTestGenres()
309+
{
310+
return Enumerable.Range(1, 10).Select(n =>
311+
new Genre
312+
{
313+
GenreId = n,
314+
Name = "Genre Name " + n,
315+
}).ToArray();
316+
}
317+
318+
private static Artist[] CreateTestArtists()
319+
{
320+
return Enumerable.Range(1, 10).Select(n =>
321+
new Artist
322+
{
323+
ArtistId = n + 1,
324+
Name = "Artist Name " + n,
325+
}).ToArray();
326+
}
327+
}
328+
}

0 commit comments

Comments
 (0)