Skip to content

Commit ebd46b4

Browse files
authored
Merge pull request #223 from ivaylokenov/view-component-testing
View component testing
2 parents f316231 + bab330d commit ebd46b4

File tree

442 files changed

+70602
-3123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

442 files changed

+70602
-3123
lines changed

MyTested.AspNetCore.Mvc.sln

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Vie
110110
EndProject
111111
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.ViewData.Test", "test\MyTested.AspNetCore.Mvc.ViewData.Test\MyTested.AspNetCore.Mvc.ViewData.Test.xproj", "{C5B868E3-3428-42CB-B628-DF11A5F1B882}"
112112
EndProject
113+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Universe", "src\MyTested.AspNetCore.Mvc.Universe\MyTested.AspNetCore.Mvc.Universe.xproj", "{62A15D6A-47B5-4B0F-9B2A-CB41A137F14B}"
114+
EndProject
115+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.Universe.Test", "test\MyTested.AspNetCore.Mvc.Universe.Test\MyTested.AspNetCore.Mvc.Universe.Test.xproj", "{F298003E-AB7F-4C30-98B6-F5C9838152C6}"
116+
EndProject
117+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.ViewComponents", "src\MyTested.AspNetCore.Mvc.ViewComponents\MyTested.AspNetCore.Mvc.ViewComponents.xproj", "{40CF14D6-7937-4143-9A02-A6E638F36C82}"
118+
EndProject
119+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.ViewComponents.Test", "test\MyTested.AspNetCore.Mvc.ViewComponents.Test\MyTested.AspNetCore.Mvc.ViewComponents.Test.xproj", "{16FC9EF0-F27E-4814-9FAC-C2054314CE9E}"
120+
EndProject
121+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.ModelState", "src\MyTested.AspNetCore.Mvc.ModelState\MyTested.AspNetCore.Mvc.ModelState.xproj", "{13A781F9-8766-4016-B72E-FA7E61DCF85F}"
122+
EndProject
123+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MyTested.AspNetCore.Mvc.ModelState.Test", "test\MyTested.AspNetCore.Mvc.ModelState.Test\MyTested.AspNetCore.Mvc.ModelState.Test.xproj", "{85915D02-3635-4BF3-9795-9FAD378AFE94}"
124+
EndProject
113125
Global
114126
GlobalSection(SolutionConfigurationPlatforms) = preSolution
115127
Debug|Any CPU = Debug|Any CPU
@@ -300,6 +312,30 @@ Global
300312
{C5B868E3-3428-42CB-B628-DF11A5F1B882}.Debug|Any CPU.Build.0 = Debug|Any CPU
301313
{C5B868E3-3428-42CB-B628-DF11A5F1B882}.Release|Any CPU.ActiveCfg = Release|Any CPU
302314
{C5B868E3-3428-42CB-B628-DF11A5F1B882}.Release|Any CPU.Build.0 = Release|Any CPU
315+
{62A15D6A-47B5-4B0F-9B2A-CB41A137F14B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
316+
{62A15D6A-47B5-4B0F-9B2A-CB41A137F14B}.Debug|Any CPU.Build.0 = Debug|Any CPU
317+
{62A15D6A-47B5-4B0F-9B2A-CB41A137F14B}.Release|Any CPU.ActiveCfg = Release|Any CPU
318+
{62A15D6A-47B5-4B0F-9B2A-CB41A137F14B}.Release|Any CPU.Build.0 = Release|Any CPU
319+
{F298003E-AB7F-4C30-98B6-F5C9838152C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
320+
{F298003E-AB7F-4C30-98B6-F5C9838152C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
321+
{F298003E-AB7F-4C30-98B6-F5C9838152C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
322+
{F298003E-AB7F-4C30-98B6-F5C9838152C6}.Release|Any CPU.Build.0 = Release|Any CPU
323+
{40CF14D6-7937-4143-9A02-A6E638F36C82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
324+
{40CF14D6-7937-4143-9A02-A6E638F36C82}.Debug|Any CPU.Build.0 = Debug|Any CPU
325+
{40CF14D6-7937-4143-9A02-A6E638F36C82}.Release|Any CPU.ActiveCfg = Release|Any CPU
326+
{40CF14D6-7937-4143-9A02-A6E638F36C82}.Release|Any CPU.Build.0 = Release|Any CPU
327+
{16FC9EF0-F27E-4814-9FAC-C2054314CE9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
328+
{16FC9EF0-F27E-4814-9FAC-C2054314CE9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
329+
{16FC9EF0-F27E-4814-9FAC-C2054314CE9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
330+
{16FC9EF0-F27E-4814-9FAC-C2054314CE9E}.Release|Any CPU.Build.0 = Release|Any CPU
331+
{13A781F9-8766-4016-B72E-FA7E61DCF85F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
332+
{13A781F9-8766-4016-B72E-FA7E61DCF85F}.Debug|Any CPU.Build.0 = Debug|Any CPU
333+
{13A781F9-8766-4016-B72E-FA7E61DCF85F}.Release|Any CPU.ActiveCfg = Release|Any CPU
334+
{13A781F9-8766-4016-B72E-FA7E61DCF85F}.Release|Any CPU.Build.0 = Release|Any CPU
335+
{85915D02-3635-4BF3-9795-9FAD378AFE94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
336+
{85915D02-3635-4BF3-9795-9FAD378AFE94}.Debug|Any CPU.Build.0 = Debug|Any CPU
337+
{85915D02-3635-4BF3-9795-9FAD378AFE94}.Release|Any CPU.ActiveCfg = Release|Any CPU
338+
{85915D02-3635-4BF3-9795-9FAD378AFE94}.Release|Any CPU.Build.0 = Release|Any CPU
303339
EndGlobalSection
304340
GlobalSection(SolutionProperties) = preSolution
305341
HideSolutionNode = FALSE
@@ -353,5 +389,11 @@ Global
353389
{DE25BE38-9AF4-4BB4-A12A-B2FB1D5D8C75} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
354390
{86D4B1F6-3ACA-415E-A3C5-1DF3648F1EDC} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
355391
{C5B868E3-3428-42CB-B628-DF11A5F1B882} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
392+
{62A15D6A-47B5-4B0F-9B2A-CB41A137F14B} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
393+
{F298003E-AB7F-4C30-98B6-F5C9838152C6} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
394+
{40CF14D6-7937-4143-9A02-A6E638F36C82} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
395+
{16FC9EF0-F27E-4814-9FAC-C2054314CE9E} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
396+
{13A781F9-8766-4016-B72E-FA7E61DCF85F} = {09353A03-2B0C-496B-8EB1-2CB6A22D758B}
397+
{85915D02-3635-4BF3-9795-9FAD378AFE94} = {D140FA14-A6C2-4279-8A41-35BC55279DA8}
356398
EndGlobalSection
357399
EndGlobal

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@ It is strongly advised to start with the [tutorial](http://docs.mytestedasp.net/
1313

1414
You can install this library using NuGet into your test project (or reference it directly in your `project.json` file). Currently MyTested.AspNetCore.Mvc works with ASP.NET Core MVC 1.0.0.
1515

16-
Install-Package MyTested.AspNetCore.Mvc -Pre
16+
Install-Package MyTested.AspNetCore.Mvc.Universe
1717

18-
This package will include all available assertion methods in your test project. If you prefer, you can be more specific by including only some of the features:
18+
This package will include all available assertion methods in your test project, including ones for authentication, database, session, caching and more. If you want only the MVC related features, install `MyTested.AspNetCore.Mvc`. Additionally, if you prefer, you can be more specific by including only some of the packages:
1919

2020
- `MyTested.AspNetCore.Mvc.Controllers` - contains setup and assertion methods for controllers
2121
- `MyTested.AspNetCore.Mvc.Routing` - contains setup and assertion methods for routes
2222
- `MyTested.AspNetCore.Mvc.Core` - contains setup and assertion methods for MVC core features
2323
- `MyTested.AspNetCore.Mvc.TempData` - contains setup and assertion methods for `ITempDataDictionary`
2424
- `MyTested.AspNetCore.Mvc.ViewData` - contains assertion methods for `ViewDataDictionary` and dynamic `ViewBag`
2525
- `MyTested.AspNetCore.Mvc.ViewActionResults` - contains setup and assertion methods for view action results
26+
- `MyTested.AspNetCore.Mvc.ViewComponents` - contains setup and assertion methods for view components
2627
- `MyTested.AspNetCore.Mvc.ViewFeatures` - contains setup and assertion methods for MVC view features
2728
- `MyTested.AspNetCore.Mvc.Http` - contains setup and assertion methods for HTTP context, request and response
2829
- `MyTested.AspNetCore.Mvc.Authentication` - contains setup methods for `ClaimsPrincipal`
29-
- `MyTested.AspNetCore.Mvc.DataAnnotations` - contains setup and assertion methods for `ModelState` validations
30+
- `MyTested.AspNetCore.Mvc.ModelState` - contains setup and assertion methods for `ModelStateDictionary` validations
31+
- `MyTested.AspNetCore.Mvc.DataAnnotations` - contains setup and assertion methods for data annotation validations
3032
- `MyTested.AspNetCore.Mvc.EntityFrameworkCore` - contains setup and assertion methods for `DbContext`
3133
- `MyTested.AspNetCore.Mvc.DependencyInjection` - contains setup methods for dependency injection services
3234
- `MyTested.AspNetCore.Mvc.Caching` - contains setup and assertion methods for `IMemoryCache`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace MusicStore.Test.Components
2+
{
3+
using Models;
4+
using MusicStore.Components;
5+
using MyTested.AspNetCore.Mvc;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using Xunit;
9+
10+
public class CartSummaryComponentTest
11+
{
12+
[Fact]
13+
public void ComponentShouldReturnCartedItems()
14+
{
15+
var cartId = "CartId_A";
16+
var albumName = "AlbumA";
17+
var totalCartItems = 10;
18+
19+
MyMvc
20+
.ViewComponent<CartSummaryComponent>()
21+
.WithSession(session => session.WithEntry("Session", cartId))
22+
.WithDbContext(dbContext => dbContext
23+
.WithEntities(entities => entities
24+
.AddRange(GetCartItems(cartId, albumName, totalCartItems))))
25+
.InvokedWith(vc => vc.InvokeAsync())
26+
.ShouldHave()
27+
.ViewBag(viewBag => viewBag
28+
.ContainingEntry("CartCount", totalCartItems)
29+
.ContainingEntry("CartSummary", albumName))
30+
.AndAlso()
31+
.ShouldReturn()
32+
.View();
33+
}
34+
35+
private static IEnumerable<CartItem> GetCartItems(string cartId, string albumTitle, int itemCount)
36+
{
37+
var album = new Album()
38+
{
39+
AlbumId = 1,
40+
Title = albumTitle,
41+
};
42+
43+
return Enumerable
44+
.Range(1, itemCount)
45+
.Select(n => new CartItem
46+
{
47+
AlbumId = 1,
48+
Album = album,
49+
Count = 1,
50+
CartId = cartId,
51+
})
52+
.ToArray();
53+
}
54+
}
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace MusicStore.Test.Components
2+
{
3+
using Models;
4+
using MusicStore.Components;
5+
using MyTested.AspNetCore.Mvc;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using Xunit;
9+
10+
public class GenreMenuComponentTest
11+
{
12+
[Fact]
13+
public void ComponentShouldReturnNineGenres()
14+
{
15+
MyMvc
16+
.ViewComponent<GenreMenuComponent>()
17+
.WithDbContext(dbContext => dbContext
18+
.WithEntities(entities => entities.AddRange(GetGenres)))
19+
.InvokedWith(vc => vc.InvokeAsync())
20+
.ShouldReturn()
21+
.View()
22+
.WithModelOfType<IEnumerable<string>>()
23+
.Passing(g => g.Count() == 9);
24+
}
25+
26+
private static IEnumerable<Genre> GetGenres => Enumerable.Range(1, 10).Select(n => new Genre { GenreId = n });
27+
}
28+
}

samples/MusicStore/MusicStore.Test/Controllers/AccountControllerTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public void PostLoginShouldReturnRedirectToActionWithValidUserName()
8686
{
8787
var model = new LoginViewModel
8888
{
89-
Email = MockedSignInManager.ValidUser,
90-
Password = MockedSignInManager.ValidUser
89+
Email = SignInManagerMock.ValidUser,
90+
Password = SignInManagerMock.ValidUser
9191
};
9292

9393
MyMvc
@@ -107,8 +107,8 @@ public void PostLoginShouldReturnRedirectToLocalWithValidUserNameAndReturnUrl()
107107
{
108108
var model = new LoginViewModel
109109
{
110-
Email = MockedSignInManager.ValidUser,
111-
Password = MockedSignInManager.ValidUser
110+
Email = SignInManagerMock.ValidUser,
111+
Password = SignInManagerMock.ValidUser
112112
};
113113

114114
var returnUrl = "/Store/Index";
@@ -128,8 +128,8 @@ public void PostLoginShouldReturnRedirectWithTwoFactor()
128128
{
129129
var model = new LoginViewModel
130130
{
131-
Email = MockedSignInManager.TwoFactorRequired,
132-
Password = MockedSignInManager.TwoFactorRequired,
131+
Email = SignInManagerMock.TwoFactorRequired,
132+
Password = SignInManagerMock.TwoFactorRequired,
133133
RememberMe = true
134134
};
135135

@@ -150,8 +150,8 @@ public void PostLoginShouldReturnViewWithLockout()
150150
{
151151
var model = new LoginViewModel
152152
{
153-
Email = MockedSignInManager.LockedOutUser,
154-
Password = MockedSignInManager.LockedOutUser
153+
Email = SignInManagerMock.LockedOutUser,
154+
Password = SignInManagerMock.LockedOutUser
155155
};
156156

157157
MyMvc

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public void IndexShouldReturnTopSellingAlbumsAndSaveThenIntoCache()
3030
.ShouldReturn()
3131
.View()
3232
.WithModelOfType<List<Album>>()
33-
.Passing(albums =>
34-
{
35-
Assert.Equal(6, albums.Count);
36-
});
33+
.Passing(albums => Assert.Equal(6, albums.Count));
3734
}
3835

3936
[Fact]

samples/MusicStore/MusicStore.Test/Mocks/MockedSignInManager.cs renamed to samples/MusicStore/MusicStore.Test/Mocks/SignInManagerMock.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
using Microsoft.Extensions.Options;
99
using Models;
1010

11-
public class MockedSignInManager : SignInManager<ApplicationUser>
11+
public class SignInManagerMock : SignInManager<ApplicationUser>
1212
{
1313
internal const string ValidUser = "[email protected]";
1414
internal const string TwoFactorRequired = "[email protected]";
1515
internal const string LockedOutUser = "[email protected]";
1616

17-
public MockedSignInManager(
17+
public SignInManagerMock(
1818
UserManager<ApplicationUser> userManager,
1919
IHttpContextAccessor contextAccessor,
2020
IUserClaimsPrincipalFactory<ApplicationUser> claimsFactory,

samples/MusicStore/MusicStore.Test/TestStartup.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void ConfigureTestServices(IServiceCollection services)
2424
{
2525
base.ConfigureServices(services);
2626

27-
services.ReplaceSingleton<SignInManager<ApplicationUser>, MockedSignInManager>();
27+
services.ReplaceSingleton<SignInManager<ApplicationUser>, SignInManagerMock>();
2828

2929
// temporary workaround while DependencyContext issues are fixed for .NET 4.5.1
3030
// controller type validation is also removed for .NET 4.5.1
@@ -37,10 +37,11 @@ public void ConfigureTestServices(IServiceCollection services)
3737
TestHelper.HttpFeatureRegistrationPlugins.Add(new SessionTestPlugin());
3838

3939
TestHelper.ShouldPassForPlugins.Add(new AbstractionsTestPlugin());
40+
TestHelper.ShouldPassForPlugins.Add(new HttpTestPlugin());
4041
TestHelper.ShouldPassForPlugins.Add(new ControllersTestPlugin());
4142

4243
services
43-
.AddMvcTesting()
44+
.AddMvcUniverseTesting()
4445
.AddRoutingTesting();
4546
#endif
4647
}

samples/MusicStore/MusicStore.Test/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"dotnet-test-xunit": "2.2.0-*",
1313
"xunit": "2.2.0-*",
14-
"MyTested.AspNetCore.Mvc": "1.0.0-*",
14+
"MyTested.AspNetCore.Mvc.Universe": "1.0.0-*",
1515
"MusicStore.Web": "1.0.0-*"
1616
},
1717
"frameworks": {

0 commit comments

Comments
 (0)