Skip to content

Commit f860188

Browse files
committed
Added ViewComponent testing to the MusicStore sample (#66)
1 parent f4a7f44 commit f860188

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed
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+
}

src/MyTested.AspNetCore.Mvc.ViewComponents/Builders/Base/BaseTestBuilderWithViewComponent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using Contracts.Base;
55
using Internal.TestContexts;
66
using Utilities.Validators;
7-
7+
88
/// <summary>
99
/// Base class for all test builders with view component.
1010
/// </summary>
11-
public class BaseTestBuilderWithViewComponent : BaseTestBuilderWithComponent, IBaseTestBuilderWithViewComponent
11+
public abstract class BaseTestBuilderWithViewComponent : BaseTestBuilderWithComponent, IBaseTestBuilderWithViewComponent
1212
{
1313
private ViewComponentTestContext testContext;
1414

src/MyTested.AspNetCore.Mvc.ViewComponents/Builders/Invocations/ShouldHave/ViewComponentShouldHaveTestBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public partial class ViewComponentShouldHaveTestBuilder<TInvocationResult>
1313
public ViewComponentShouldHaveTestBuilder(ViewComponentTestContext testContext)
1414
: base(testContext)
1515
{
16+
this.TestContext = testContext;
1617
}
1718

1819
/// <summary>

src/MyTested.AspNetCore.Mvc.ViewComponents/Builders/ViewComponents/ViewComponentInvocationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private void SetViewComponentDescriptor(MethodInfo methodInfo)
3939
if (viewComponentContext.ViewComponentDescriptor?.MethodInfo == null)
4040
{
4141
var viewComponentDescriptorCache = this.Services.GetService<IViewComponentDescriptorCache>();
42-
if (viewComponentDescriptorCache == null)
42+
if (viewComponentDescriptorCache != null)
4343
{
4444
viewComponentContext.ViewComponentDescriptor
4545
= viewComponentDescriptorCache.TryGetViewComponentDescriptor(methodInfo);

src/MyTested.AspNetCore.Mvc.ViewComponents/Plugins/ViewComponentsTestPlugins.cs renamed to src/MyTested.AspNetCore.Mvc.ViewComponents/Plugins/ViewComponentsTestPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Internal.TestContexts;
66
using Utilities;
77

8-
public class ViewComponentsTestPlugins : BaseTestPlugin, IDefaultRegistrationPlugin, IServiceRegistrationPlugin, IShouldPassForPlugin
8+
public class ViewComponentsTestPlugin : BaseTestPlugin, IDefaultRegistrationPlugin, IServiceRegistrationPlugin, IShouldPassForPlugin
99
{
1010
private readonly Type viewComponentAttributesType = typeof(ViewComponentAttributes);
1111

0 commit comments

Comments
 (0)