Skip to content

Commit c40e30b

Browse files
committed
#283 Adding tests for without model state implementation.
1 parent 5065217 commit c40e30b

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

src/MyTested.AspNetCore.Mvc.ViewComponents/Internal/TestContexts/ViewComponentTestContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ViewComponentTestContext : ActionTestContext<ViewContext>
1515

1616
public override string ExceptionMessagePrefix => $"When invoking {this.Component.GetName()} expected";
1717

18-
public override ModelStateDictionary ModelState => this.viewComponentContext.ViewData.ModelState;
18+
public override ModelStateDictionary ModelState => this.ViewComponentContext.ViewData.ModelState;
1919

2020
protected override ViewContext DefaultComponentContext
2121
=> ViewContextMock.Default(this);

test/MyTested.AspNetCore.Mvc.ModelState.Test/BuildersTests/ModelsTests/ModelStateBuilderTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Setups.Controllers;
55
using Xunit;
66
using System.Collections.Generic;
7+
using System.Linq;
78

89
public class ModelStateBuilderTests
910
{
@@ -58,5 +59,77 @@ public void WithModelStateWithErrorsObjectShouldWorkCorrectly()
5859
.ShouldReturn()
5960
.BadRequest();
6061
}
62+
63+
[Fact]
64+
public void WithoutModelStateByProvidingExistingKeyShouldWorkCorrectly()
65+
{
66+
var keyValuePair = new KeyValuePair<string, string>("Key1", "Value1");
67+
68+
MyController<MvcController>
69+
.Instance()
70+
.WithModelState(modelState => modelState
71+
.WithError(keyValuePair.Key, keyValuePair.Value)
72+
.WithError("PseudoRandomKey", "value"))
73+
.WithoutModelState(keyValuePair.Key)
74+
.Calling(c => c.GetModelStateKeys())
75+
.ShouldReturn()
76+
.Ok(ok => ok
77+
.WithModelOfType<List<string>>()
78+
.Passing(keys => keys.Count == 1 &&
79+
keys.Any(key => !key.Equals(keyValuePair.Key))));
80+
}
81+
82+
[Fact]
83+
public void WithoutModelStateByUsingTheBuilderShouldWorkCorrectly()
84+
{
85+
var keyValuePair = new KeyValuePair<string, string>("Key1", "Value1");
86+
87+
MyController<MvcController>
88+
.Instance()
89+
.WithModelState(modelState => modelState
90+
.WithError(keyValuePair.Key, keyValuePair.Value)
91+
.WithError("PseudoRandomKey", "value"))
92+
.WithoutModelState(modelState => modelState.WithoutModelState(keyValuePair.Key))
93+
.Calling(c => c.GetModelStateKeys())
94+
.ShouldReturn()
95+
.Ok(ok => ok
96+
.WithModelOfType<List<string>>()
97+
.Passing(keys => keys.Count == 1 &&
98+
keys.Any(key => !key.Equals(keyValuePair.Key))));
99+
}
100+
101+
[Fact]
102+
public void WithoutModelStateShouldWorkCorrectly()
103+
{
104+
MyController<MvcController>
105+
.Instance()
106+
.WithModelState(modelState => modelState
107+
.WithError("PseudoRandomKey1", "value1")
108+
.WithError("PseudoRandomKey2", "value2"))
109+
.WithoutModelState()
110+
.Calling(c => c.GetModelStateKeys())
111+
.ShouldReturn()
112+
.Ok(ok => ok
113+
.WithModelOfType<List<string>>()
114+
.Passing(keys => keys.Count == 0));
115+
}
116+
117+
[Fact]
118+
public void WithoutModelStateByRemovingNonExistingKeyShouldWorkCorrectly()
119+
{
120+
var keyValuePair = new KeyValuePair<string, string>("Key1", "Value1");
121+
122+
MyController<MvcController>
123+
.Instance()
124+
.WithModelState(modelState => modelState
125+
.WithError(keyValuePair.Key, keyValuePair.Value))
126+
.WithoutModelState("NonExistingKey")
127+
.Calling(c => c.GetModelStateKeys())
128+
.ShouldReturn()
129+
.Ok(ok => ok
130+
.WithModelOfType<List<string>>()
131+
.Passing(keys => keys.Count == 1 &&
132+
keys.Any(key => key.Equals(keyValuePair.Key))));
133+
}
61134
}
62135
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,11 @@ public IActionResult BadRequestWithCustomError()
786786
return this.BadRequest(this.ResponseModel);
787787
}
788788

789+
public IActionResult GetModelStateKeys()
790+
{
791+
return this.Ok(this.ModelState.Keys.ToList());
792+
}
793+
789794
public IActionResult AcceptedAction()
790795
{
791796
return this.Accepted();

0 commit comments

Comments
 (0)