Skip to content

Commit eb80fed

Browse files
authored
Merge pull request #330 - Added unit tests for the view components
Added unit tests for the view components
2 parents a62ad3c + 59a7126 commit eb80fed

File tree

13 files changed

+631
-41
lines changed

13 files changed

+631
-41
lines changed

src/MyTested.AspNetCore.Mvc.ViewComponents/Builders/And/AndTestBuilderWithViewComponentResult.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/MyTested.AspNetCore.Mvc.Test.Setups/TestObjectFactory.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public static RequestModel GetValidRequestModel()
126126

127127
public static RequestModel GetRequestModelWithErrors() => new RequestModel();
128128

129+
public static ResponseModel GetValidResponseModel()
130+
=> new ResponseModel
131+
{
132+
IntegerValue = 10
133+
};
134+
129135
public static List<ResponseModel> GetListOfResponseModels()
130136
=> new List<ResponseModel>
131137
{

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.AttributesTests
22
{
3+
using System;
34
using Exceptions;
45
using Microsoft.AspNetCore.Mvc;
56
using Setups;
67
using Setups.Common;
78
using Setups.ViewComponents;
8-
using System;
99
using Xunit;
1010

1111
public class ViewComponentAttributesTestBuilderTests
@@ -105,5 +105,16 @@ public void PassingForShouldThrowExceptionWithIncorrectAttributeWithPredicate()
105105
},
106106
"When testing AttributesComponent was expected to have ActionNameAttribute, but in fact such was not found.");
107107
}
108+
109+
[Fact]
110+
public void AndAlsoShouldWorkCorrectly()
111+
{
112+
MyViewComponent<AttributesComponent>
113+
.ShouldHave()
114+
.Attributes(attributes => attributes
115+
.PassingFor<ViewComponentAttribute>(viewComponent => viewComponent.Name == "Test")
116+
.AndAlso()
117+
.ContainingAttributeOfType<ViewComponentAttribute>());
118+
}
108119
}
109120
}

test/MyTested.AspNetCore.Mvc.ViewComponents.Test/BuildersTests/InvocationsTests/ShouldHaveTests/ShouldHaveViewComponentAttributesTests.cs

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.InvocationsTests.ShouldHaveTests
22
{
33
using Exceptions;
4+
using Microsoft.AspNetCore.Mvc;
45
using Setups;
6+
using Setups.Common;
57
using Setups.ViewComponents;
68
using Xunit;
9+
using Xunit.Sdk;
710

811
public class ShouldHaveViewComponentAttributesTests
912
{
@@ -15,7 +18,7 @@ public void NoAttributesShouldNotThrowExceptionWithActionContainingNoAttributes(
1518
.ShouldHave()
1619
.NoAttributes();
1720
}
18-
21+
1922
[Fact]
2023
public void NoAttributesShouldThrowExceptionWithActionContainingAttributes()
2124
{
@@ -29,9 +32,9 @@ public void NoAttributesShouldThrowExceptionWithActionContainingAttributes()
2932
},
3033
"When testing AttributesComponent was expected to not have any attributes, but it had some.");
3134
}
32-
35+
3336
[Fact]
34-
public void AttributesShouldNotThrowEceptionWithActionContainingAttributes()
37+
public void AttributesShouldNotThrowExceptionWithActionContainingAttributes()
3538
{
3639
MyViewComponent<AttributesComponent>
3740
.InvokedWith(c => c.Invoke())
@@ -40,7 +43,38 @@ public void AttributesShouldNotThrowEceptionWithActionContainingAttributes()
4043
}
4144

4245
[Fact]
43-
public void AttributesShouldThrowEceptionWithActionContainingNoAttributes()
46+
public void AttributesShouldNotThrowExceptionWithActionContainingAttributesOfType()
47+
{
48+
MyViewComponent<AttributesComponent>
49+
.Instance()
50+
.InvokedWith(c => c.Invoke())
51+
.ShouldHave()
52+
.Attributes(attributes =>
53+
{
54+
attributes.ContainingAttributeOfType<CustomAttribute>();
55+
});
56+
}
57+
58+
[Fact]
59+
public void AttributesShouldThrowExceptionWithActionContainingAttributesOfTypeWithIncorrectValue()
60+
{
61+
Test.AssertException<AttributeAssertionException>(
62+
() =>
63+
{
64+
MyViewComponent<AttributesComponent>
65+
.Instance()
66+
.InvokedWith(c => c.Invoke())
67+
.ShouldHave()
68+
.Attributes(attributes =>
69+
{
70+
attributes.ContainingAttributeOfType<AreaAttribute>();
71+
});
72+
},
73+
"When testing AttributesComponent was expected to have AreaAttribute, but in fact such was not found.");
74+
}
75+
76+
[Fact]
77+
public void AttributesShouldThrowExceptionWithActionContainingNoAttributes()
4478
{
4579
Test.AssertException<AttributeAssertionException>(
4680
() =>
@@ -52,9 +86,24 @@ public void AttributesShouldThrowEceptionWithActionContainingNoAttributes()
5286
},
5387
"When testing NormalComponent was expected to have at least 1 attribute, but in fact none was found.");
5488
}
55-
89+
5690
[Fact]
57-
public void AttributesShouldNotThrowEceptionWithActionContainingNumberOfAttributes()
91+
public void AttributesShouldThrowExceptionWithActionContainingZeroAttributes()
92+
{
93+
Test.AssertException<AttributeAssertionException>(
94+
() =>
95+
{
96+
MyViewComponent<NormalComponent>
97+
.Instance()
98+
.InvokedWith(c => c.Invoke())
99+
.ShouldHave()
100+
.Attributes(withTotalNumberOf: 0);
101+
},
102+
"When testing NormalComponent was expected to have at least 1 attribute, but in fact none was found.");
103+
}
104+
105+
[Fact]
106+
public void AttributesShouldNotThrowExceptionWithActionContainingNumberOfAttributes()
58107
{
59108
MyViewComponent<AttributesComponent>
60109
.InvokedWith(c => c.Invoke())
@@ -63,7 +112,7 @@ public void AttributesShouldNotThrowEceptionWithActionContainingNumberOfAttribut
63112
}
64113

65114
[Fact]
66-
public void AttributesShouldThrowEceptionWithActionContainingNumberOfAttributes()
115+
public void AttributesShouldThrowExceptionWithActionContainingNumberOfAttributes()
67116
{
68117
Test.AssertException<AttributeAssertionException>(
69118
() =>
@@ -77,7 +126,7 @@ public void AttributesShouldThrowEceptionWithActionContainingNumberOfAttributes(
77126
}
78127

79128
[Fact]
80-
public void AttributesShouldThrowEceptionWithActionContainingNumberOfAttributesTestingWithOne()
129+
public void AttributesShouldThrowExceptionWithActionContainingNumberOfAttributesTestingWithOne()
81130
{
82131
Test.AssertException<AttributeAssertionException>(
83132
() =>
@@ -89,5 +138,40 @@ public void AttributesShouldThrowEceptionWithActionContainingNumberOfAttributesT
89138
},
90139
"When testing AttributesComponent was expected to have 1 attribute, but in fact found 2.");
91140
}
141+
142+
[Fact]
143+
public void AndAlsoShouldWorkCorrectly()
144+
{
145+
MyViewComponent<PocoViewComponent>
146+
.Instance()
147+
.InvokedWith(c => c.Invoke())
148+
.ShouldHave()
149+
.NoAttributes()
150+
.AndAlso()
151+
.ShouldPassForThe<PocoViewComponent>((viewComponent) =>
152+
{
153+
Assert.NotNull(viewComponent);
154+
Assert.Empty(viewComponent.TempData);
155+
});
156+
}
157+
158+
[Fact]
159+
public void AndAlsoShouldThrowExceptionWithIncorrectAssertions()
160+
{
161+
Assert.Throws<TrueException>(
162+
() =>
163+
{
164+
MyViewComponent<PocoViewComponent>
165+
.Instance()
166+
.InvokedWith(c => c.Invoke())
167+
.ShouldHave()
168+
.NoAttributes()
169+
.AndAlso()
170+
.ShouldPassForThe<PocoViewComponent>((viewComponent) =>
171+
{
172+
Assert.True(viewComponent.TempData == null);
173+
});
174+
});
175+
}
92176
}
93177
}

test/MyTested.AspNetCore.Mvc.ViewComponents.Test/BuildersTests/InvocationsTests/ShouldReturnTests/ViewComponentShouldReturnContentTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Setups;
55
using Setups.ViewComponents;
66
using Xunit;
7+
using Xunit.Sdk;
78

89
public class ViewComponentShouldReturnContentTests
910
{
@@ -39,6 +40,36 @@ public void ShouldReturnContentShouldThrowExceptionWithIncorrectContent()
3940
"When invoking NormalComponent expected content result to contain 'incorrect', but instead received 'Test'.");
4041
}
4142

43+
[Fact]
44+
public void ShouldReturnContentShouldThrowExceptionWithEmptyContent()
45+
{
46+
Test.AssertException<ContentViewComponentResultAssertionException>(
47+
() =>
48+
{
49+
MyViewComponent<NormalComponent>
50+
.Instance()
51+
.InvokedWith(c => c.Invoke())
52+
.ShouldReturn()
53+
.Content(string.Empty);
54+
},
55+
"When invoking NormalComponent expected content result to contain '', but instead received 'Test'.");
56+
}
57+
58+
[Fact]
59+
public void ShouldReturnContentShouldThrowExceptionWithNull()
60+
{
61+
Test.AssertException<ContentViewComponentResultAssertionException>(
62+
() =>
63+
{
64+
MyViewComponent<NormalComponent>
65+
.Instance()
66+
.InvokedWith(c => c.Invoke())
67+
.ShouldReturn()
68+
.Content(content: null);
69+
},
70+
"When invoking NormalComponent expected content result to contain '', but instead received 'Test'.");
71+
}
72+
4273
[Fact]
4374
public void ShouldReturnContentShouldThrowExceptionWithBadRequestResult()
4475
{
@@ -87,5 +118,22 @@ public void ShouldReturnContentWithAssertionsShouldNotThrowExceptionWithValidPre
87118
Assert.StartsWith("Te", content);
88119
});
89120
}
121+
122+
[Fact]
123+
public void ShouldReturnContentWithAssertionsShouldThrowExceptionWithInvalidPredicate()
124+
{
125+
Assert.Throws<StartsWithException>(
126+
() =>
127+
{
128+
MyViewComponent<NormalComponent>
129+
.Instance()
130+
.InvokedWith(c => c.Invoke())
131+
.ShouldReturn()
132+
.Content(content =>
133+
{
134+
Assert.StartsWith("te", content);
135+
});
136+
});
137+
}
90138
}
91139
}

test/MyTested.AspNetCore.Mvc.ViewComponents.Test/BuildersTests/InvocationsTests/ShouldReturnTests/ViewComponentShouldReturnHtmlContentTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Setups;
55
using Setups.ViewComponents;
66
using Xunit;
7+
using Xunit.Sdk;
78

89
public class ViewComponentShouldReturnHtmlContentTests
910
{
@@ -25,6 +26,36 @@ public void ShouldReturnContentShouldNotThrowExceptionWithContentResultAndValue(
2526
.HtmlContent("<input type='button' />");
2627
}
2728

29+
[Fact]
30+
public void ShouldReturnContentShouldThrowExceptionWithEmptyContent()
31+
{
32+
Test.AssertException<ContentViewComponentResultAssertionException>(
33+
() =>
34+
{
35+
MyViewComponent<HtmlContentComponent>
36+
.Instance()
37+
.InvokedWith(c => c.Invoke())
38+
.ShouldReturn()
39+
.HtmlContent(string.Empty);
40+
},
41+
"When invoking HtmlContentComponent expected content result to contain '', but instead received '<input type='button' />'.");
42+
}
43+
44+
[Fact]
45+
public void ShouldReturnContentShouldThrowExceptionWithNull()
46+
{
47+
Test.AssertException<ContentViewComponentResultAssertionException>(
48+
() =>
49+
{
50+
MyViewComponent<HtmlContentComponent>
51+
.Instance()
52+
.InvokedWith(c => c.Invoke())
53+
.ShouldReturn()
54+
.HtmlContent(htmlContent: null);
55+
},
56+
"When invoking HtmlContentComponent expected content result to contain '', but instead received '<input type='button' />'.");
57+
}
58+
2859
[Fact]
2960
public void ShouldReturnContentShouldThrowExceptionWithIncorrectContent()
3061
{
@@ -87,5 +118,22 @@ public void ShouldReturnContentWithAssertionsShouldNotThrowExceptionWithValidPre
87118
Assert.StartsWith("<input ", content);
88119
});
89120
}
121+
122+
[Fact]
123+
public void ShouldReturnContentWithAssertionsShouldThrowExceptionWithInvalidPredicate()
124+
{
125+
Assert.Throws<StartsWithException>(
126+
() =>
127+
{
128+
MyViewComponent<HtmlContentComponent>
129+
.Instance()
130+
.InvokedWith(c => c.Invoke())
131+
.ShouldReturn()
132+
.HtmlContent(content =>
133+
{
134+
Assert.StartsWith("<button ", content);
135+
});
136+
});
137+
}
90138
}
91139
}

test/MyTested.AspNetCore.Mvc.ViewComponents.Test/BuildersTests/InvocationsTests/ShouldReturnTests/ViewComponentShouldReturnNullOrDefaultTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.InvocationsTests.ShouldReturnTests
22
{
3+
using Exceptions;
34
using Setups;
45
using Setups.ViewComponents;
56
using Xunit;
6-
using Exceptions;
77

88
public class ViewComponentShouldReturnNullOrDefaultTests
99
{
@@ -15,7 +15,7 @@ public void ShouldReturnNullShouldNotThrowExceptionWhenReturnValueNull()
1515
.ShouldReturn()
1616
.Null();
1717
}
18-
18+
1919
[Fact]
2020
public void ShouldReturnNullShouldThrowExceptionWhenReturnValueNotNull()
2121
{

test/MyTested.AspNetCore.Mvc.ViewComponents.Test/BuildersTests/InvocationsTests/ShouldThrowExceptionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.InvocationsTests
22
{
3+
using System;
34
using Exceptions;
45
using Setups;
56
using Setups.ViewComponents;
6-
using System;
77
using Xunit;
88

99
public class ShouldThrowExceptionTests

0 commit comments

Comments
 (0)