Skip to content

Commit de87272

Browse files
authored
Merge pull request #309 - Added Unit Tests for the ViewData and the ViewBag Builders
Added Unit Tests for the ViewData and the ViewBag Builders
2 parents 59e6339 + 58df54f commit de87272

File tree

5 files changed

+208
-2
lines changed

5 files changed

+208
-2
lines changed

test/MyTested.AspNetCore.Mvc.ViewData.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveViewBagTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,47 @@ public void ViewBagWithBuilderShouldWorkCorrectly()
131131
.ShouldReturn()
132132
.Ok();
133133
}
134+
135+
[Fact]
136+
public void ViewBagWithBuilderShouldContainMultipleEntries()
137+
{
138+
MyController<MvcController>
139+
.Instance()
140+
.Calling(c => c.AddViewBagAction())
141+
.ShouldHave()
142+
.ViewBag(viewBag => viewBag
143+
.ContainingEntries(new { Test = "BagValue", Another = "AnotherValue" }))
144+
.AndAlso()
145+
.ShouldReturn()
146+
.Ok();
147+
}
148+
149+
[Fact]
150+
public void ViewBagWithBuilderShouldContainEntryWithKey()
151+
{
152+
MyController<MvcController>
153+
.Instance()
154+
.Calling(c => c.AddViewBagAction())
155+
.ShouldHave()
156+
.ViewBag(viewBag => viewBag
157+
.ContainingEntryWithKey("Another"))
158+
.AndAlso()
159+
.ShouldReturn()
160+
.Ok();
161+
}
162+
163+
[Fact]
164+
public void ViewBagWithBuilderShouldContainEntryOfTypeString()
165+
{
166+
MyController<MvcController>
167+
.Instance()
168+
.Calling(c => c.AddViewBagAction())
169+
.ShouldHave()
170+
.ViewBag(viewBag => viewBag
171+
.ContainingEntryOfType<string>())
172+
.AndAlso()
173+
.ShouldReturn()
174+
.Ok();
175+
}
134176
}
135177
}

test/MyTested.AspNetCore.Mvc.ViewData.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveViewDataTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,47 @@ public void ViewDataWithBuilderShouldWorkCorrectly()
131131
.ShouldReturn()
132132
.Ok();
133133
}
134+
135+
[Fact]
136+
public void ViewDataWithBuilderShouldContainMultipleEntries()
137+
{
138+
MyController<MvcController>
139+
.Instance()
140+
.Calling(c => c.AddViewDataAction())
141+
.ShouldHave()
142+
.ViewData(viewData => viewData
143+
.ContainingEntries(new { Test = "DataValue", Another = "AnotherValue" }))
144+
.AndAlso()
145+
.ShouldReturn()
146+
.Ok();
147+
}
148+
149+
[Fact]
150+
public void ViewDataWithBuilderShouldContainEntryWithKey()
151+
{
152+
MyController<MvcController>
153+
.Instance()
154+
.Calling(c => c.AddViewDataAction())
155+
.ShouldHave()
156+
.ViewData(viewData => viewData
157+
.ContainingEntryWithKey("Another"))
158+
.AndAlso()
159+
.ShouldReturn()
160+
.Ok();
161+
}
162+
163+
[Fact]
164+
public void ViewDataWithBuilderShouldContainEntryOfTypeString()
165+
{
166+
MyController<MvcController>
167+
.Instance()
168+
.Calling(c => c.AddViewDataAction())
169+
.ShouldHave()
170+
.ViewData(viewData => viewData
171+
.ContainingEntryOfType<string>())
172+
.AndAlso()
173+
.ShouldReturn()
174+
.Ok();
175+
}
134176
}
135177
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.InternalTests
2+
{
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.Extensions.DependencyInjection.Extensions;
5+
using Setups;
6+
using Setups.Controllers;
7+
using Setups.ViewComponents;
8+
using System;
9+
using Xunit;
10+
11+
public class ComponentTestContextViewDataExtensionsTests
12+
{
13+
[Fact]
14+
public void GetViewDataShouldThrowInvalidOperationExceptionWithPocoViewComponent()
15+
{
16+
Test.AssertException<InvalidOperationException>(
17+
() =>
18+
{
19+
MyViewComponent<PocoViewComponent>
20+
.Instance()
21+
.InvokedWith(c => c.Invoke())
22+
.ShouldHave()
23+
.NoViewData()
24+
.AndAlso()
25+
.ShouldReturn()
26+
.View();
27+
},
28+
"ViewDataDictionary could not be found on the provided PocoViewComponent. The property should be specified manually by providing component instance or using the specified helper methods.");
29+
}
30+
31+
[Fact]
32+
public void GetViewDataShouldHaveNoViewDataForPocoController()
33+
{
34+
MyApplication
35+
.StartsFrom<DefaultStartup>()
36+
.WithServices(services =>
37+
{
38+
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
39+
});
40+
41+
MyController<FullPocoController>
42+
.Instance()
43+
.Calling(c => c.OkResultAction())
44+
.ShouldHave()
45+
.NoViewData()
46+
.AndAlso()
47+
.ShouldReturn()
48+
.Ok();
49+
50+
MyApplication.StartsFrom<DefaultStartup>();
51+
}
52+
}
53+
}

test/MyTested.AspNetCore.Mvc.ViewData.Test/InternalTests/ViewDataPropertyHelperTests.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
using Internal.Services;
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.AspNetCore.Mvc;
7-
using Setups.Controllers;
8-
using Xunit;
97
using Microsoft.Extensions.DependencyInjection.Extensions;
108
using Setups;
9+
using Setups.Controllers;
10+
using System;
11+
using Xunit;
1112

1213
public class ViewDataPropertyHelperTests
1314
{
@@ -58,5 +59,31 @@ public void GetPropertiesShouldNotThrowExceptionForPocoController()
5859

5960
MyApplication.StartsFrom<DefaultStartup>();
6061
}
62+
63+
[Fact]
64+
public void ViewDataGetterShouldThrowNullReferenceExceptionWithNull()
65+
{
66+
MyApplication.StartsFrom<DefaultStartup>();
67+
68+
var helper = ViewDataPropertyHelper.GetViewDataProperties<MvcController>();
69+
70+
Assert.Throws<NullReferenceException>(() => helper.ViewDataGetter(null));
71+
}
72+
73+
[Fact]
74+
public void ViewDataGetterShouldThrowInvalidOperationExceptionForPrivatePocoController()
75+
{
76+
MyApplication.StartsFrom<DefaultStartup>();
77+
78+
var helper = ViewDataPropertyHelper.GetViewDataProperties<PrivatePocoController>();
79+
var controller = new PrivatePocoController(TestServiceProvider.Global);
80+
81+
Test.AssertException<InvalidOperationException>(
82+
() =>
83+
{
84+
helper.ViewDataGetter(controller);
85+
},
86+
"ViewDataDictionary could not be found on the provided PrivatePocoController. The property should be specified manually by providing component instance or using the specified helper methods.");
87+
}
6188
}
6289
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.PluginsTests
2+
{
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Plugins;
5+
using System;
6+
using Xunit;
7+
8+
public class ViewDataTestPluginTests
9+
{
10+
[Fact]
11+
public void ShouldHavePriorityWithDefaultValue()
12+
{
13+
var testPlugin = new ViewDataTestPlugin();
14+
15+
Assert.IsAssignableFrom<IDefaultRegistrationPlugin>(testPlugin);
16+
Assert.NotNull(testPlugin);
17+
Assert.Equal(-1000, testPlugin.Priority);
18+
}
19+
20+
[Fact]
21+
public void ShouldThrowArgumentNullExceptionWithInvalidServiceCollection()
22+
{
23+
var testPlugin = new ViewDataTestPlugin();
24+
25+
Assert.Throws<ArgumentNullException>(() => testPlugin.DefaultServiceRegistrationDelegate(null));
26+
}
27+
28+
[Fact]
29+
public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection()
30+
{
31+
var testPlugin = new ViewDataTestPlugin();
32+
var serviceCollection = new ServiceCollection();
33+
34+
testPlugin.DefaultServiceRegistrationDelegate(serviceCollection);
35+
36+
var methodReturnType = testPlugin.DefaultServiceRegistrationDelegate.Method.ReturnType.Name;
37+
38+
Assert.True(methodReturnType == "Void");
39+
Assert.True(serviceCollection.Count == 126);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)