Skip to content

Commit 2728dd1

Browse files
committed
Added controller sample tests (#132)
1 parent a9ee545 commit 2728dd1

File tree

31 files changed

+610
-83
lines changed

31 files changed

+610
-83
lines changed

samples/MusicStore/MusicStore.Test/Controllers/CheckoutControllerTest.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MusicStore.Test.Controllers
22
{
33
using System.Threading;
4+
using System.Linq;
45
using Models;
56
using MusicStore.Controllers;
67
using MyTested.AspNetCore.Mvc;
@@ -51,6 +52,40 @@ public void PostAddressAndPaymentShouldHaveValidAttributes()
5152
.View(With.Default<Order>());
5253
}
5354

55+
[Fact]
56+
public void PostAddressAndPaymentShouldRedirectToCompleteWhenSuccessful()
57+
{
58+
int orderId = 10;
59+
string cartId = "CartId_A";
60+
61+
MyMvc
62+
.Controller<CheckoutController>()
63+
.WithHttpRequest(request => request
64+
.WithFormField("PromoCode", "FREE"))
65+
.WithSession(session => session
66+
.WithEntry("Session", cartId))
67+
.WithAuthenticatedUser()
68+
.WithRouteData()
69+
.WithDbContext(db => db
70+
.WithEntities<MusicStoreContext>(entities =>
71+
{
72+
var cartItems = CreateTestCartItems(
73+
cartId,
74+
itemPrice: 10,
75+
numberOfItem: 1);
76+
77+
entities.AddRange(cartItems.Select(n => n.Album).Distinct());
78+
entities.AddRange(cartItems);
79+
}))
80+
.Calling(c => c.AddressAndPayment(
81+
From.Services<MusicStoreContext>(),
82+
new Order { OrderId = orderId },
83+
CancellationToken.None))
84+
.ShouldReturn()
85+
.Redirect()
86+
.To<CheckoutController>(c => c.Complete(With.No<MusicStoreContext>(), orderId));
87+
}
88+
5489
[Fact]
5590
public void PostAddressAndPaymentShouldReturnViewWithInvalidPromoCode()
5691
{
@@ -66,6 +101,19 @@ public void PostAddressAndPaymentShouldReturnViewWithInvalidPromoCode()
66101
.View(With.Default<Order>());
67102
}
68103

104+
[Fact]
105+
public void PostAddressAndPaymentShouldReturnViewWithTheOrderIfRequestIsCancelled()
106+
{
107+
MyMvc
108+
.Controller<CheckoutController>()
109+
.Calling(c => c.AddressAndPayment(
110+
From.Services<MusicStoreContext>(),
111+
With.Default<Order>(),
112+
new CancellationToken(true)))
113+
.ShouldReturn()
114+
.View(With.Default<Order>());
115+
}
116+
69117
[Fact]
70118
public void CompleteShouldReturnViewWithCorrectIdWithCorrectOrder()
71119
{
@@ -82,14 +130,14 @@ public void CompleteShouldReturnViewWithCorrectIdWithCorrectOrder()
82130
.ShouldReturn()
83131
.View(1);
84132
}
85-
133+
86134
[Fact]
87135
public void CompleteShouldReturnViewWithErrorWithIncorrectOrder()
88136
{
89137
MyMvc
90138
.Controller<CheckoutController>()
91139
.WithAuthenticatedUser(user => user.WithUsername("TestUser"))
92-
.WithDbContext(dbContext =>
140+
.WithDbContext(dbContext =>
93141
dbContext.WithSet<MusicStoreContext, Order>(o => o.Add(new Order
94142
{
95143
OrderId = 1,
@@ -99,5 +147,26 @@ public void CompleteShouldReturnViewWithErrorWithIncorrectOrder()
99147
.ShouldReturn()
100148
.View("Error");
101149
}
150+
151+
private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem)
152+
{
153+
var albums = Enumerable.Range(1, 10).Select(n =>
154+
new Album()
155+
{
156+
AlbumId = n,
157+
Price = itemPrice,
158+
}).ToArray();
159+
160+
var cartItems = Enumerable.Range(1, numberOfItem).Select(n =>
161+
new CartItem()
162+
{
163+
Count = 1,
164+
CartId = cartId,
165+
AlbumId = n % 10,
166+
Album = albums[n % 10],
167+
}).ToArray();
168+
169+
return cartItems;
170+
}
102171
}
103172
}

samples/MusicStore/MusicStore.Test/Controllers/HomeControllerTest.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
namespace MusicStore.Test.Controllers
22
{
3+
using Microsoft.Extensions.Caching.Memory;
4+
using Models;
35
using MusicStore.Controllers;
46
using MyTested.AspNetCore.Mvc;
7+
using System.Collections.Generic;
8+
using System.Linq;
59
using Xunit;
610

711
public class HomeControllerTest
812
{
13+
[Fact]
14+
public void IndexShouldReturnTopSellingAlbumsAndSaveThenIntoCache()
15+
{
16+
MyMvc
17+
.Controller<HomeController>()
18+
.WithOptions(options => options
19+
.For<AppSettings>(settings => settings.CacheDbResults = true))
20+
.WithDbContext(dbContext => dbContext
21+
.WithSet<MusicStoreContext, Album>(albums => albums
22+
.AddRange(TestAlbumDataProvider.GetAlbums())))
23+
.Calling(c => c.Index(
24+
From.Services<MusicStoreContext>(),
25+
From.Services<IMemoryCache>()))
26+
.ShouldHave()
27+
.MemoryCache(cache => cache
28+
.ContainingEntryOfType<List<Album>>("topselling"))
29+
.AndAlso()
30+
.ShouldReturn()
31+
.View()
32+
.WithModelOfType<List<Album>>()
33+
.Passing(albums =>
34+
{
35+
Assert.Equal(6, albums.Count);
36+
});
37+
}
38+
939
[Fact]
1040
public void ErrorShouldReturnCorrectView()
1141
{
@@ -35,5 +65,36 @@ public void AccessDeniedShouldReturnCorrectView()
3565
.ShouldReturn()
3666
.View("~/Views/Shared/AccessDenied.cshtml");
3767
}
68+
69+
private class TestAlbumDataProvider
70+
{
71+
public static Album[] GetAlbums()
72+
{
73+
var generes = Enumerable.Range(1, 10).Select(n =>
74+
new Genre()
75+
{
76+
GenreId = n,
77+
Name = "Genre Name " + n,
78+
}).ToArray();
79+
80+
var artists = Enumerable.Range(1, 10).Select(n =>
81+
new Artist()
82+
{
83+
ArtistId = n + 1,
84+
Name = "Artist Name " + n,
85+
}).ToArray();
86+
87+
var albums = Enumerable.Range(1, 10).Select(n =>
88+
new Album()
89+
{
90+
Artist = artists[n - 1],
91+
ArtistId = n,
92+
Genre = generes[n - 1],
93+
GenreId = n,
94+
}).ToArray();
95+
96+
return albums;
97+
}
98+
}
3899
}
39100
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
namespace MusicStore.Test.Controllers
2+
{
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Models;
6+
using MusicStore.Controllers;
7+
using MyTested.AspNetCore.Mvc;
8+
using ViewModels;
9+
using Xunit;
10+
using System.Threading;
11+
public class ShoppingCartControllerTest
12+
{
13+
[Fact]
14+
public void IndexShouldReturnNoCartItemsWhenSessionIsEmpty()
15+
{
16+
MyMvc
17+
.Controller<ShoppingCartController>()
18+
.Calling(c => c.Index())
19+
.ShouldReturn()
20+
.View()
21+
.WithModelOfType<ShoppingCartViewModel>()
22+
.Passing(model =>
23+
{
24+
Assert.Equal(0, model.CartItems.Count);
25+
Assert.Equal(0, model.CartTotal);
26+
});
27+
}
28+
29+
[Fact]
30+
public void IndexShouldReturnNoCartItemsWhenNoCartItemsInCart()
31+
{
32+
MyMvc
33+
.Controller<ShoppingCartController>()
34+
.WithSession(session => session.WithEntry("Session", "CartId_A"))
35+
.Calling(c => c.Index())
36+
.ShouldReturn()
37+
.View()
38+
.WithModelOfType<ShoppingCartViewModel>()
39+
.Passing(model =>
40+
{
41+
Assert.Equal(0, model.CartItems.Count);
42+
Assert.Equal(0, model.CartTotal);
43+
});
44+
}
45+
46+
[Fact]
47+
public void IndexShouldReturnCartItemsWhenItemsInCart()
48+
{
49+
var cartId = "CartId_A";
50+
51+
MyMvc
52+
.Controller<ShoppingCartController>()
53+
.WithSession(session => session.WithEntry("Session", cartId))
54+
.WithDbContext(db => db
55+
.WithEntities<MusicStoreContext>(entities =>
56+
{
57+
var cartItems = CreateTestCartItems(
58+
cartId,
59+
itemPrice: 10,
60+
numberOfItem: 5);
61+
62+
entities.AddRange(cartItems.Select(n => n.Album).Distinct());
63+
entities.AddRange(cartItems);
64+
}))
65+
.Calling(c => c.Index())
66+
.ShouldReturn()
67+
.View()
68+
.WithModelOfType<ShoppingCartViewModel>()
69+
.Passing(model =>
70+
{
71+
Assert.Equal(5, model.CartItems.Count);
72+
Assert.Equal(5 * 10, model.CartTotal);
73+
});
74+
}
75+
76+
[Fact]
77+
public void AddToCartShouldAddItemsToCart()
78+
{
79+
int albumId = 3;
80+
81+
MyMvc
82+
.Controller<ShoppingCartController>()
83+
.WithSession(session => session.WithEntry("Session", "CartId_A"))
84+
.WithDbContext(db => db
85+
.WithEntities<MusicStoreContext>(entities => entities
86+
.AddRange(CreateTestAlbums(itemPrice: 10))))
87+
.Calling(c => c.AddToCart(albumId, CancellationToken.None))
88+
.ShouldReturn()
89+
.Redirect()
90+
.To<ShoppingCartController>(c => c.Index())
91+
.AndAlso()
92+
.ShouldPassFor()
93+
.TheHttpContext(async httpContext =>
94+
{
95+
var cart = ShoppingCart.GetCart(From.Services<MusicStoreContext>(), httpContext);
96+
Assert.Equal(1, (await cart.GetCartItems()).Count);
97+
Assert.Equal(albumId, (await cart.GetCartItems()).Single().AlbumId);
98+
});
99+
}
100+
101+
[Fact]
102+
public void RemoveFromCartShouldRemoveItemFromCart()
103+
{
104+
var cartId = "CartId_А";
105+
var cartItemId = 3;
106+
var numberOfItem = 5;
107+
var unitPrice = 10;
108+
109+
MyMvc
110+
.Controller<ShoppingCartController>()
111+
.WithSession(session => session.WithEntry("Session", cartId))
112+
.WithDbContext(db => db
113+
.WithEntities<MusicStoreContext>(entities =>
114+
{
115+
var cartItems = CreateTestCartItems(cartId, unitPrice, numberOfItem);
116+
entities.AddRange(cartItems.Select(n => n.Album).Distinct());
117+
entities.AddRange(cartItems);
118+
}))
119+
.Calling(c => c.RemoveFromCart(cartItemId, CancellationToken.None))
120+
.ShouldReturn()
121+
.Json()
122+
.WithResponseModelOfType<ShoppingCartRemoveViewModel>()
123+
.Passing(model =>
124+
{
125+
Assert.Equal(numberOfItem - 1, model.CartCount);
126+
Assert.Equal((numberOfItem - 1) * 10, model.CartTotal);
127+
Assert.Equal(" has been removed from your shopping cart.", model.Message);
128+
})
129+
.AndAlso()
130+
.ShouldPassFor()
131+
.TheHttpContext(async httpContext =>
132+
{
133+
var cart = ShoppingCart.GetCart(From.Services<MusicStoreContext>(), httpContext);
134+
Assert.False((await cart.GetCartItems()).Any(c => c.CartItemId == cartItemId));
135+
});
136+
}
137+
138+
private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem)
139+
{
140+
var albums = CreateTestAlbums(itemPrice);
141+
142+
var cartItems = Enumerable.Range(1, numberOfItem).Select(n =>
143+
new CartItem()
144+
{
145+
Count = 1,
146+
CartId = cartId,
147+
AlbumId = n % albums.Length,
148+
Album = albums[n % albums.Length],
149+
}).ToArray();
150+
151+
return cartItems;
152+
}
153+
154+
private static Album[] CreateTestAlbums(decimal itemPrice)
155+
{
156+
return Enumerable.Range(1, 10).Select(n =>
157+
new Album()
158+
{
159+
AlbumId = n,
160+
Price = itemPrice,
161+
}).ToArray();
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)