Skip to content

Commit c5c5a3c

Browse files
committed
Merge pull request #18 from ivaylokenov/action-result-test-builder
Action result test builder
2 parents 30f7bd7 + ef41a22 commit c5c5a3c

File tree

53 files changed

+3455
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3455
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace MyWebApi.Tests.BuildersTests.ActionsTests
2+
{
3+
using Exceptions;
4+
using NUnit.Framework;
5+
using Setups;
6+
using Setups.Models;
7+
8+
[TestFixture]
9+
public class ShouldHaveModelStateTests
10+
{
11+
[Test]
12+
public void ShouldHaveModelStateForShouldChainCorrectly()
13+
{
14+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
15+
16+
MyWebApi
17+
.Controller<WebApiController>()
18+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
19+
.ShouldHaveModelStateFor<RequestModel>()
20+
.ContainingNoModelStateErrorFor(r => r.NonRequiredString)
21+
.ContainingModelStateErrorFor(r => r.Integer)
22+
.ContainingModelStateErrorFor(r => r.RequiredString);
23+
}
24+
25+
[Test]
26+
public void ShouldHaveValidModelStateShouldBeValidWithValidRequestModel()
27+
{
28+
var requestModel = TestObjectFactory.GetValidRequestModel();
29+
30+
MyWebApi
31+
.Controller<WebApiController>()
32+
.Calling(c => c.ModelStateCheck(requestModel))
33+
.ShouldHaveValidModelState();
34+
}
35+
36+
[Test]
37+
[ExpectedException(
38+
typeof(ResponseModelErrorAssertionException),
39+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have valid model state with no errors, but it had some.")]
40+
public void ShouldHaveValidModelStateShouldThrowExceptionWithInvalidRequestModel()
41+
{
42+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
43+
44+
MyWebApi
45+
.Controller<WebApiController>()
46+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
47+
.ShouldHaveValidModelState();
48+
}
49+
50+
[Test]
51+
public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModel()
52+
{
53+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
54+
55+
MyWebApi
56+
.Controller<WebApiController>()
57+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
58+
.ShouldHaveInvalidModelState();
59+
}
60+
61+
[Test]
62+
[ExpectedException(
63+
typeof(ResponseModelErrorAssertionException),
64+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state, but was in fact valid.")]
65+
public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModel()
66+
{
67+
var requestModel = TestObjectFactory.GetValidRequestModel();
68+
69+
MyWebApi
70+
.Controller<WebApiController>()
71+
.Calling(c => c.ModelStateCheck(requestModel))
72+
.ShouldHaveInvalidModelState();
73+
}
74+
}
75+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace MyWebApi.Tests.BuildersTests.ActionsTests
2+
{
3+
using Exceptions;
4+
using NUnit.Framework;
5+
using Setups;
6+
7+
[TestFixture]
8+
public class ShouldReturnOkResultTests
9+
{
10+
[Test]
11+
public void ShouldReturnOkResultShouldNotThrowExceptionWithOkResult()
12+
{
13+
MyWebApi
14+
.Controller<WebApiController>()
15+
.Calling(c => c.OkResultAction())
16+
.ShouldReturnOk();
17+
}
18+
19+
[Test]
20+
[ExpectedException(
21+
typeof(HttpActionResultAssertionException),
22+
ExpectedMessage = "When calling BadRequestAction action in WebApiController expected action result to be OkNegotiatedContentResult<T>, but instead received BadRequestResult.")]
23+
public void ShouldReturnOkResultShouldThrowExceptionWithOtherThanOkResult()
24+
{
25+
MyWebApi
26+
.Controller<WebApiController>()
27+
.Calling(c => c.BadRequestAction())
28+
.ShouldReturnOk();
29+
}
30+
}
31+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
namespace MyWebApi.Tests.BuildersTests.ActionsTests
2+
{
3+
using System.Collections.Generic;
4+
5+
using Exceptions;
6+
7+
using NUnit.Framework;
8+
9+
using Setups;
10+
using Setups.Models;
11+
12+
[TestFixture]
13+
public class ShouldReturnTests
14+
{
15+
[Test]
16+
public void ShouldReturnShouldNotThrowExceptionWithStructTypes()
17+
{
18+
MyWebApi
19+
.Controller<WebApiController>()
20+
.Calling(c => c.GenericStructAction())
21+
.ShouldReturn<bool>();
22+
}
23+
24+
[Test]
25+
public void ShouldReturnShouldNotThrowExceptionWithStructTypesAndTypeOf()
26+
{
27+
MyWebApi
28+
.Controller<WebApiController>()
29+
.Calling(c => c.GenericStructAction())
30+
.ShouldReturn(typeof(bool));
31+
}
32+
33+
[Test]
34+
public void ShouldReturnShouldNotThrowExceptionWithDifferentInheritedGenericResult()
35+
{
36+
MyWebApi
37+
.Controller<WebApiController>()
38+
.Calling(c => c.GenericAction())
39+
.ShouldReturn<IList<ResponseModel>>();
40+
}
41+
42+
[Test]
43+
public void ShouldReturnShouldNotThrowExceptionWithDifferentInheritedGenericResultAndTypeOf()
44+
{
45+
MyWebApi
46+
.Controller<WebApiController>()
47+
.Calling(c => c.GenericAction())
48+
.ShouldReturn(typeof(IList<ResponseModel>));
49+
}
50+
51+
[Test]
52+
public void ShouldReturnShouldNotThrowExceptionWithDifferentInheritedGenericDefinitionResultAndTypeOf()
53+
{
54+
MyWebApi
55+
.Controller<WebApiController>()
56+
.Calling(c => c.GenericAction())
57+
.ShouldReturn(typeof(IList<>));
58+
}
59+
60+
[Test]
61+
public void ShouldReturnShouldNotThrowExceptionWithClassTypes()
62+
{
63+
MyWebApi
64+
.Controller<WebApiController>()
65+
.Calling(c => c.GenericAction())
66+
.ShouldReturn<ICollection<ResponseModel>>();
67+
}
68+
69+
[Test]
70+
public void ShouldReturnShouldNotThrowExceptionWithClassTypesAndTypeOf()
71+
{
72+
MyWebApi
73+
.Controller<WebApiController>()
74+
.Calling(c => c.GenericAction())
75+
.ShouldReturn(typeof(ICollection<ResponseModel>));
76+
}
77+
78+
[Test]
79+
public void ShouldReturnShouldNotThrowExceptionWithClassGenericDefinitionTypesAndTypeOf()
80+
{
81+
MyWebApi
82+
.Controller<WebApiController>()
83+
.Calling(c => c.GenericAction())
84+
.ShouldReturn(typeof(ICollection<>));
85+
}
86+
87+
[Test]
88+
[ExpectedException(
89+
typeof(HttpActionResultAssertionException),
90+
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be ResponseModel, but instead received List<ResponseModel>.")]
91+
public void ShouldReturnShouldThrowExceptionWithDifferentResult()
92+
{
93+
MyWebApi
94+
.Controller<WebApiController>()
95+
.Calling(c => c.GenericAction())
96+
.ShouldReturn<ResponseModel>();
97+
}
98+
99+
[Test]
100+
[ExpectedException(
101+
typeof(HttpActionResultAssertionException),
102+
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be ResponseModel, but instead received List<ResponseModel>.")]
103+
public void ShouldReturnShouldThrowExceptionWithDifferentResultAndTypeOf()
104+
{
105+
MyWebApi
106+
.Controller<WebApiController>()
107+
.Calling(c => c.GenericAction())
108+
.ShouldReturn(typeof(ResponseModel));
109+
}
110+
111+
[Test]
112+
[ExpectedException(
113+
typeof(HttpActionResultAssertionException),
114+
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be ICollection<Int32>, but instead received List<ResponseModel>.")]
115+
public void ShouldReturnShouldThrowExceptionWithDifferentGenericResult()
116+
{
117+
MyWebApi
118+
.Controller<WebApiController>()
119+
.Calling(c => c.GenericAction())
120+
.ShouldReturn<ICollection<int>>();
121+
}
122+
123+
[Test]
124+
[ExpectedException(
125+
typeof(HttpActionResultAssertionException),
126+
ExpectedMessage = "When calling GenericAction action in WebApiController expected action result to be ICollection<Int32>, but instead received List<ResponseModel>.")]
127+
public void ShouldReturnShouldThrowExceptionWithDifferentGenericResultAndTypeOf()
128+
{
129+
MyWebApi
130+
.Controller<WebApiController>()
131+
.Calling(c => c.GenericAction())
132+
.ShouldReturn(typeof(ICollection<int>));
133+
}
134+
}
135+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
namespace MyWebApi.Tests.BuildersTests
2+
{
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using System.Web.Http.Results;
6+
7+
using Builders.Contracts;
8+
using NUnit.Framework;
9+
using Setups;
10+
11+
[TestFixture]
12+
public class ControllerBuilderTests
13+
{
14+
[Test]
15+
public void CallingShouldPopulateCorrectActionNameAndActionResultWithNormalActionCall()
16+
{
17+
var actionResultTestBuilder = MyWebApi
18+
.Controller<WebApiController>()
19+
.Calling(c => c.OkResultAction());
20+
21+
CheckActionResultTestBuilder(actionResultTestBuilder, "OkResultAction");
22+
}
23+
24+
[Test]
25+
public void CallingShouldPopulateCorrectActionNameAndActionResultWithAsyncActionCall()
26+
{
27+
var actionResultTestBuilder = MyWebApi
28+
.Controller<WebApiController>()
29+
.CallingAsync(c => c.AsyncOkResultAction());
30+
31+
CheckActionResultTestBuilder(actionResultTestBuilder, "AsyncOkResultAction");
32+
}
33+
34+
[Test]
35+
public void CallingShouldPopulateModelStateWhenThereAreModelErrors()
36+
{
37+
var requestBody = TestObjectFactory.GetRequestModelWithErrors();
38+
39+
var actionResultTestBuilder = MyWebApi
40+
.Controller<WebApiController>()
41+
.Calling(c => c.OkResultActionWithRequestBody(1, requestBody));
42+
43+
var modelState = actionResultTestBuilder.Controller.ModelState;
44+
45+
Assert.IsFalse(modelState.IsValid);
46+
Assert.AreEqual(2, modelState.Values.Count);
47+
Assert.AreEqual("Integer", modelState.Keys.First());
48+
Assert.AreEqual("RequiredString", modelState.Keys.Last());
49+
}
50+
51+
[Test]
52+
public void CallingShouldHaveValidModelStateWhenThereAreNoModelErrors()
53+
{
54+
var requestBody = TestObjectFactory.GetValidRequestModel();
55+
56+
var actionResultTestBuilder = MyWebApi
57+
.Controller<WebApiController>()
58+
.Calling(c => c.OkResultActionWithRequestBody(1, requestBody));
59+
60+
var modelState = actionResultTestBuilder.Controller.ModelState;
61+
62+
Assert.IsTrue(modelState.IsValid);
63+
Assert.AreEqual(0, modelState.Values.Count);
64+
Assert.AreEqual(0, modelState.Keys.Count);
65+
}
66+
67+
private void CheckActionResultTestBuilder<TActionResult>(
68+
IActionResultTestBuilder<TActionResult> actionResultTestBuilder,
69+
string expectedActionName)
70+
{
71+
var actionName = actionResultTestBuilder.ActionName;
72+
var actionResult = actionResultTestBuilder.ActionResult;
73+
74+
Assert.IsNotNullOrEmpty(actionName);
75+
Assert.IsNotNull(actionResult);
76+
77+
Assert.AreEqual(expectedActionName, actionName);
78+
Assert.IsAssignableFrom<OkResult>(actionResult);
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)