Skip to content

Commit 17669d4

Browse files
committed
Made most of the unit test work
ViewResult objects are surprisingly complicated to create from scracth, especially with the ViewData collection. It's nearly impossible to create the collection "by hand". Instead, I decided to try to simply create a TestController class that would return ViewResults and PartialViewResults constructed like I need for the tests. It makes passing the model and the viewdata much more simple.
1 parent d2275f7 commit 17669d4

File tree

7 files changed

+706
-44
lines changed

7 files changed

+706
-44
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
3+
using Moq;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace FluentAssertions.Mvc.Tests
10+
{
11+
public class TestController : Controller
12+
{
13+
public TestController()
14+
{
15+
TempData = new Mock<ITempDataDictionary>().Object;
16+
}
17+
18+
public PartialViewResult PartialViewSimpleModel()
19+
{
20+
return PartialView(model: "hello");
21+
}
22+
23+
public ViewResult ViewSimpleModel()
24+
{
25+
return View(model: "hello");
26+
}
27+
28+
public ViewResult ViewWithOneTempData()
29+
{
30+
TempData.Add("key1", "value1");
31+
return View();
32+
}
33+
34+
public ViewResult ViewWithTwoTempData()
35+
{
36+
TempData.Add("key1", "value1");
37+
TempData.Add("key2", "value2");
38+
return View();
39+
}
40+
41+
public ViewResult ViewWithOneViewData()
42+
{
43+
ViewData.Add("key1", "value1");
44+
return View();
45+
}
46+
47+
public ViewResult ViewWithTwoViewData()
48+
{
49+
ViewData["key1"] = "value1";
50+
ViewData["key2"] = "value2";
51+
return View();
52+
}
53+
}
54+
}

tests/FluentAssertions.AspNetCore.Mvc.Tests/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"NUnit": "3.4.1",
77
"dotnet-test-nunit": "3.4.0-beta-1",
88
"Microsoft.AspNetCore.Mvc": "1.0.0",
9-
"FluentAssertions.AspNetCore.Mvc": "1.0.0-*"
9+
"FluentAssertions.AspNetCore.Mvc": "1.0.0-*",
10+
"Moq": "4.6.36-alpha"
1011
},
1112

1213
"buildOptions": {

0 commit comments

Comments
 (0)