Skip to content

Commit 2467b38

Browse files
Add unit tests for IncludingInherited for view components
1 parent 9243f7e commit 2467b38

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.Setups.ViewComponents
2+
{
3+
using Microsoft.AspNetCore.Authorization;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
[ResponseCache]
7+
public class InheritViewComponent : BaseInheritViewComponent
8+
{
9+
}
10+
11+
[AllowAnonymous]
12+
[ValidateAntiForgeryToken]
13+
public class BaseInheritViewComponent : ViewComponent
14+
{
15+
16+
}
17+
}

test/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.Test/BuildersTests/AttributesTests/ViewComponentAttributesTestBuilderTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.AttributesTests
22
{
33
using Exceptions;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
46
using Setups;
7+
using Setups.Controllers;
58
using Setups.ViewComponents;
69
using Xunit;
710

@@ -66,5 +69,32 @@ public void DisablingActionCallShouldThrowExceptionWithActionWithoutTheAttribute
6669
},
6770
"When testing ComponentWithCustomAttribute was expected to have ViewComponentAttribute, but in fact such was not found.");
6871
}
72+
73+
[Fact]
74+
public void IncludingInheritedShouldIncludeAllCustomInheritedAttributesFromBaseViewComponentAndNotThrowException()
75+
{
76+
MyViewComponent<InheritViewComponent>
77+
.Instance()
78+
.ShouldHave()
79+
.Attributes(attributes => attributes.IncludingInherited()
80+
.ContainingAttributeOfType<ValidateAntiForgeryTokenAttribute>()
81+
.ContainingAttributeOfType<AllowAnonymousAttribute>()
82+
.ContainingAttributeOfType<ResponseCacheAttribute>());
83+
}
84+
85+
[Fact]
86+
public void TryingToAssertInheritedAttributesWithoutIncludingInheritedShouldThrowException()
87+
{
88+
Test.AssertException<AttributeAssertionException>(
89+
() =>
90+
{
91+
MyViewComponent<InheritViewComponent>
92+
.Instance()
93+
.ShouldHave()
94+
.Attributes(attributes => attributes//.IncludingInherited()
95+
.ContainingAttributeOfType<ValidateAntiForgeryTokenAttribute>()
96+
.ContainingAttributeOfType<ResponseCacheAttribute>());
97+
}, "When testing InheritViewComponent was expected to have ValidateAntiForgeryTokenAttribute, but in fact such was not found.");
98+
}
6999
}
70100
}

0 commit comments

Comments
 (0)