Skip to content

Commit 3016d34

Browse files
committed
Added unit tests for the ViewDataPropertyHelper - to throw exception when passing null and private poco controller to the view data getter.
1 parent 5bab127 commit 3016d34

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

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

Lines changed: 28 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,30 @@ 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+
}, "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.");
86+
}
6187
}
6288
}

0 commit comments

Comments
 (0)