Skip to content

Commit 0285a8a

Browse files
Add unit tests for IncludingInherited for component attributes
1 parent 32ce029 commit 0285a8a

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

test/MyTested.AspNetCore.Mvc.Controllers.Test/BuildersTests/AttributesTests/ControllerAttributesTestBuilderTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,35 @@ public void PassingForShouldThrowExceptionWithIncorrectAttributeWithPredicate()
105105
},
106106
"When testing MvcController was expected to have ActionNameAttribute, but in fact such was not found.");
107107
}
108+
109+
110+
111+
[Fact]
112+
public void IncludingInheritedShouldIncludeAllCustomInheritedAttributesFromBaseClassesAndNotThrowException()
113+
{
114+
MyController<InheritAttributesController>
115+
.Instance()
116+
.ShouldHave()
117+
.Attributes(attributes => attributes.IncludingInherited()
118+
.ContainingAttributeOfType<ValidateAntiForgeryTokenAttribute>()
119+
.ContainingAttributeOfType<AllowAnonymousAttribute>()
120+
.ContainingAttributeOfType<ResponseCacheAttribute>());
121+
}
122+
123+
[Fact]
124+
public void TryingToAssertInheritedAttributesWithoutIncludingInheritedShouldThrowException()
125+
{
126+
Test.AssertException<AttributeAssertionException>(
127+
() =>
128+
{
129+
MyController<InheritAttributesController>
130+
.Instance()
131+
.ShouldHave()
132+
.Attributes(attributes => attributes//.IncludingInherited()
133+
.ContainingAttributeOfType<ValidateAntiForgeryTokenAttribute>()
134+
.ContainingAttributeOfType<AllowAnonymousAttribute>()
135+
.ContainingAttributeOfType<ResponseCacheAttribute>());
136+
}, "When testing InheritAttributesController was expected to have ValidateAntiForgeryTokenAttribute, but in fact such was not found.");
137+
}
108138
}
109139
}
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.Controllers
2+
{
3+
using Microsoft.AspNetCore.Authorization;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
[ResponseCache]
7+
public class InheritAttributesController : InheritAttributesBaseController
8+
{
9+
}
10+
11+
[AllowAnonymous]
12+
[ValidateAntiForgeryToken]
13+
public class InheritAttributesBaseController : ControllerBase
14+
{
15+
16+
}
17+
}

0 commit comments

Comments
 (0)