Skip to content

Commit bebb5fb

Browse files
committed
#283 Adding tests for without temp data builder
1 parent 2a70081 commit bebb5fb

3 files changed

Lines changed: 104 additions & 1 deletion

File tree

src/MyTested.AspNetCore.Mvc.TempData/Builders/Data/WithoutTempDataBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public IAndWithoutTempDataBuilder WithoutEntries(IEnumerable<string> entriesKeys
3232
/// <inheritdoc />
3333
public IAndWithoutTempDataBuilder WithoutEntry(string key)
3434
{
35-
this.TempData.Remove(key);
35+
if (this.TempData.ContainsKey(key))
36+
this.TempData.Remove(key);
37+
3638
return this;
3739
}
3840

test/MyTested.AspNetCore.Mvc.TempData.Test/BuildersTests/DataTests/TempDataBuilderTests.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,101 @@ public void WithEntriesObjectShouldSetCorrectValues()
5050
.Ok(ok => ok
5151
.WithModel("Valid"));
5252
}
53+
54+
[Fact]
55+
public void WithoutEntryShouldReturnCorrectValues()
56+
{
57+
MyController<MvcController>
58+
.Instance()
59+
.WithTempData(tempData => tempData
60+
.WithEntries(new Dictionary<string, object>
61+
{
62+
["Test"] = "Valid",
63+
["Second"] = "SecondValue",
64+
}))
65+
.WithoutTempData("Test")
66+
.Calling(c => c.GetTempDataKeys())
67+
.ShouldReturn()
68+
.Ok(ok => ok
69+
.WithModel(new List<string>
70+
{
71+
"Second"
72+
}));
73+
}
74+
75+
[Fact]
76+
public void WithoutEntriesShouldReturnCorrectValues()
77+
{
78+
MyController<MvcController>
79+
.Instance()
80+
.WithTempData(tempData => tempData
81+
.WithEntries(new Dictionary<string, object>
82+
{
83+
["Test"] = "Valid",
84+
["Second"] = "SecondValue",
85+
}))
86+
.WithoutTempData(new List<string>() { "Second" })
87+
.Calling(c => c.GetTempDataKeys())
88+
.ShouldReturn()
89+
.Ok(ok => ok
90+
.WithModel(new List<string>
91+
{
92+
"Test"
93+
}));
94+
}
95+
96+
[Fact]
97+
public void WithoutEntriesByProvidingParamsShouldReturnCorrectValues()
98+
{
99+
MyController<MvcController>
100+
.Instance()
101+
.WithTempData(tempData => tempData
102+
.WithEntries(new Dictionary<string, object>
103+
{
104+
["Test"] = "Valid",
105+
["Second"] = "SecondValue",
106+
}))
107+
.WithoutTempData("Second", "Test")
108+
.Calling(c => c.GetTempDataKeys())
109+
.ShouldReturn()
110+
.Ok(ok => ok
111+
.WithModel(new List<string>()));
112+
}
113+
114+
[Fact]
115+
public void WithoutEntriesShouldRemoveAllEntities()
116+
{
117+
MyController<MvcController>
118+
.Instance()
119+
.WithTempData(tempData => tempData
120+
.WithEntries(new Dictionary<string, object>
121+
{
122+
["Test"] = "Valid",
123+
["Second"] = "SecondValue",
124+
}))
125+
.WithoutTempData()
126+
.Calling(c => c.GetTempDataKeys())
127+
.ShouldReturn()
128+
.Ok(ok => ok
129+
.WithModel(new List<string>()));
130+
}
131+
132+
[Fact]
133+
public void WithoutEntryNonExistingItemShouldReturnCorrectValues()
134+
{
135+
MyController<MvcController>
136+
.Instance()
137+
.WithTempData(tempData => tempData
138+
.WithEntries(new Dictionary<string, object>
139+
{
140+
["Test"] = "Valid",
141+
["Second"] = "SecondValue",
142+
}))
143+
.WithoutTempData("NonExisting")
144+
.Calling(c => c.GetTempDataKeys())
145+
.ShouldReturn()
146+
.Ok(ok => ok
147+
.WithModel(new List<string>() { "Test" , "Second"}));
148+
}
53149
}
54150
}

test/MyTested.AspNetCore.Mvc.Test.Setups/Controllers/MvcController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,11 @@ public IActionResult TempDataAction()
11801180
return this.BadRequest();
11811181
}
11821182

1183+
public IActionResult GetTempDataKeys()
1184+
{
1185+
return this.Ok(this.TempData.Keys.ToList());
1186+
}
1187+
11831188
public IActionResult SessionAction()
11841189
{
11851190
if (this.HttpContext.Session.GetString("test") != null)

0 commit comments

Comments
 (0)