Skip to content

Commit 30679a0

Browse files
committed
Merge pull request #40 from ivaylokenov/development
Development
2 parents 5f0bd60 + 2f58498 commit 30679a0

File tree

84 files changed

+3908
-534
lines changed

Some content is hidden

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

84 files changed

+3908
-534
lines changed

MyWebApi.Tests/BuildersTests/ActionsTests/ShouldHaveModelStateTests.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
namespace MyWebApi.Tests.BuildersTests.ActionsTests
22
{
33
using Exceptions;
4+
45
using NUnit.Framework;
6+
57
using Setups;
68
using Setups.Models;
79

@@ -35,7 +37,7 @@ public void ShouldHaveValidModelStateShouldBeValidWithValidRequestModel()
3537

3638
[Test]
3739
[ExpectedException(
38-
typeof(ResponseModelErrorAssertionException),
40+
typeof(ModelErrorAssertionException),
3941
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have valid model state with no errors, but it had some.")]
4042
public void ShouldHaveValidModelStateShouldThrowExceptionWithInvalidRequestModel()
4143
{
@@ -60,7 +62,7 @@ public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModel()
6062

6163
[Test]
6264
[ExpectedException(
63-
typeof(ResponseModelErrorAssertionException),
65+
typeof(ModelErrorAssertionException),
6466
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state, but was in fact valid.")]
6567
public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModel()
6668
{
@@ -71,5 +73,31 @@ public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModel
7173
.Calling(c => c.ModelStateCheck(requestModel))
7274
.ShouldHaveInvalidModelState();
7375
}
76+
77+
[Test]
78+
public void AndShouldWorkCorrectlyWithValidModelState()
79+
{
80+
var requestModel = TestObjectFactory.GetValidRequestModel();
81+
82+
MyWebApi
83+
.Controller<WebApiController>()
84+
.Calling(c => c.ModelStateCheck(requestModel))
85+
.ShouldHaveValidModelState()
86+
.AndAlso()
87+
.ShouldReturnOk();
88+
}
89+
90+
[Test]
91+
public void AndShouldWorkCorrectlyWithInvalidModelState()
92+
{
93+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
94+
95+
MyWebApi
96+
.Controller<WebApiController>()
97+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
98+
.ShouldHaveInvalidModelState()
99+
.AndAlso()
100+
.ShouldReturnOk();
101+
}
74102
}
75103
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace MyWebApi.Tests.BuildersTests.ActionsTests
2+
{
3+
using NUnit.Framework;
4+
using Setups;
5+
6+
[TestFixture]
7+
public class ShouldReturnBadRequestTests
8+
{
9+
[Test]
10+
public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsBadRequest()
11+
{
12+
MyWebApi
13+
.Controller<WebApiController>()
14+
.Calling(c => c.BadRequestAction())
15+
.ShouldReturnBadRequest();
16+
}
17+
18+
[Test]
19+
public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsInvalidModelStateResult()
20+
{
21+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
22+
23+
MyWebApi
24+
.Controller<WebApiController>()
25+
.Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
26+
.ShouldReturnBadRequest();
27+
}
28+
29+
[Test]
30+
public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsBadRequestErrorMessageResult()
31+
{
32+
MyWebApi
33+
.Controller<WebApiController>()
34+
.Calling(c => c.BadRequestWithErrorAction())
35+
.ShouldReturnBadRequest();
36+
}
37+
}
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace MyWebApi.Tests.BuildersTests.ActionsTests
2+
{
3+
using Exceptions;
4+
5+
using NUnit.Framework;
6+
7+
using Setups;
8+
9+
[TestFixture]
10+
public class ShouldReturnNotFoundTests
11+
{
12+
[Test]
13+
public void ShouldReturnNotFoundShouldNotThrowExceptionWhenActionReturnsNotFound()
14+
{
15+
MyWebApi
16+
.Controller<WebApiController>()
17+
.Calling(c => c.NotFoundAction())
18+
.ShouldReturnNotFound();
19+
}
20+
21+
[Test]
22+
[ExpectedException(
23+
typeof(HttpActionResultAssertionException),
24+
ExpectedMessage = "When calling BadRequestAction action in WebApiController expected action result to be NotFoundResult, but instead received BadRequestResult.")]
25+
public void ShouldReturnNotFoundShouldThrowExceptionWhenActionDoesNotReturnNotFound()
26+
{
27+
MyWebApi
28+
.Controller<WebApiController>()
29+
.Calling(c => c.BadRequestAction())
30+
.ShouldReturnNotFound();
31+
}
32+
}
33+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
namespace MyWebApi.Tests.BuildersTests.ActionsTests
2+
{
3+
using System.Net;
4+
5+
using Exceptions;
6+
7+
using NUnit.Framework;
8+
9+
using Setups;
10+
11+
[TestFixture]
12+
public class ShouldReturnStatusCodeTests
13+
{
14+
[Test]
15+
public void ShouldReturnStatusCodeShouldNotThrowExceptionWhenActionReturnsStatusCode()
16+
{
17+
MyWebApi
18+
.Controller<WebApiController>()
19+
.Calling(c => c.StatusCodeAction())
20+
.ShouldReturnStatusCode();
21+
}
22+
23+
[Test]
24+
[ExpectedException(
25+
typeof(HttpActionResultAssertionException),
26+
ExpectedMessage = "When calling BadRequestAction action in WebApiController expected action result to be StatusCodeResult, but instead received BadRequestResult.")]
27+
public void ShouldReturnStatusCodeShouldThrowExceptionWhenActionDoesNotReturnStatusCode()
28+
{
29+
MyWebApi
30+
.Controller<WebApiController>()
31+
.Calling(c => c.BadRequestAction())
32+
.ShouldReturnStatusCode();
33+
}
34+
35+
[Test]
36+
public void ShouldReturnStatusCodeShouldNotThrowExceptionWhenActionReturnsCorrectStatusCode()
37+
{
38+
MyWebApi
39+
.Controller<WebApiController>()
40+
.Calling(c => c.StatusCodeAction())
41+
.ShouldReturnStatusCode(HttpStatusCode.Found);
42+
}
43+
44+
[Test]
45+
[ExpectedException(
46+
typeof(HttpStatusCodeAssertionException),
47+
ExpectedMessage = "When calling StatusCodeAction action in WebApiController expected to have 201 (Created) status code, but received 302 (Redirect).")]
48+
public void ShouldReturnStatusCodeShouldThrowExceptionWhenActionReturnsWrongStatusCode()
49+
{
50+
MyWebApi
51+
.Controller<WebApiController>()
52+
.Calling(c => c.StatusCodeAction())
53+
.ShouldReturnStatusCode(HttpStatusCode.Created);
54+
}
55+
56+
[Test]
57+
[ExpectedException(
58+
typeof(HttpActionResultAssertionException),
59+
ExpectedMessage = "When calling BadRequestAction action in WebApiController expected action result to be StatusCodeResult, but instead received BadRequestResult.")]
60+
public void ShouldReturnStatusCodeShouldThrowExceptionWhenActionDoesNotReturnStatusCodeAndPassingStatusCode()
61+
{
62+
MyWebApi
63+
.Controller<WebApiController>()
64+
.Calling(c => c.BadRequestAction())
65+
.ShouldReturnStatusCode(HttpStatusCode.Created);
66+
}
67+
}
68+
}
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 ShouldReturnUnauthorizedTests
9+
{
10+
[Test]
11+
public void ShouldReturnUnauthorizedShouldNotThrowExceptionWhenActionReturnsUnauthorizedResult()
12+
{
13+
MyWebApi
14+
.Controller<WebApiController>()
15+
.Calling(c => c.UnauthorizedAction())
16+
.ShouldReturnUnauthorized();
17+
}
18+
19+
[Test]
20+
[ExpectedException(
21+
typeof(HttpActionResultAssertionException),
22+
ExpectedMessage = "When calling BadRequestAction action in WebApiController expected action result to be UnauthorizedResult, but instead received BadRequestResult.")]
23+
public void ShouldReturnUnauthorizedShouldThrowExceptionWhenActionDoesNotReturnUnauthorizedResult()
24+
{
25+
MyWebApi
26+
.Controller<WebApiController>()
27+
.Calling(c => c.BadRequestAction())
28+
.ShouldReturnUnauthorized();
29+
}
30+
}
31+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
namespace MyWebApi.Tests.BuildersTests.BadRequestsTests
2+
{
3+
using Exceptions;
4+
5+
using NUnit.Framework;
6+
7+
using Setups;
8+
9+
[TestFixture]
10+
public class BadRequestErrorMessageTestBuilder
11+
{
12+
[Test]
13+
public void ThatEqualsShouldNotThrowExceptionWithProperErrorMessage()
14+
{
15+
MyWebApi
16+
.Controller<WebApiController>()
17+
.Calling(c => c.BadRequestWithErrorAction())
18+
.ShouldReturnBadRequest()
19+
.WithErrorMessage()
20+
.ThatEquals("Bad request");
21+
}
22+
23+
[Test]
24+
[ExpectedException(
25+
typeof(BadRequestResultAssertionException),
26+
ExpectedMessage = "When calling BadRequestWithErrorAction action in WebApiController expected bad request error message to be 'Bad', but instead found 'Bad request'.")]
27+
public void ThatEqualsShouldThrowExceptionWithIncorrectErrorMessage()
28+
{
29+
MyWebApi
30+
.Controller<WebApiController>()
31+
.Calling(c => c.BadRequestWithErrorAction())
32+
.ShouldReturnBadRequest()
33+
.WithErrorMessage()
34+
.ThatEquals("Bad");
35+
}
36+
37+
[Test]
38+
public void BeginningWithShouldNotThrowExceptionWithProperErrorMessage()
39+
{
40+
MyWebApi
41+
.Controller<WebApiController>()
42+
.Calling(c => c.BadRequestWithErrorAction())
43+
.ShouldReturnBadRequest()
44+
.WithErrorMessage()
45+
.BeginningWith("Bad ");
46+
}
47+
48+
[Test]
49+
[ExpectedException(
50+
typeof(BadRequestResultAssertionException),
51+
ExpectedMessage = "When calling BadRequestWithErrorAction action in WebApiController expected bad request error message to begin with 'request', but instead found 'Bad request'.")]
52+
public void BeginningWithShouldThrowExceptionWithIncorrectErrorMessage()
53+
{
54+
MyWebApi
55+
.Controller<WebApiController>()
56+
.Calling(c => c.BadRequestWithErrorAction())
57+
.ShouldReturnBadRequest()
58+
.WithErrorMessage()
59+
.BeginningWith("request");
60+
}
61+
62+
[Test]
63+
public void EndingWithShouldNotThrowExceptionWithProperErrorMessage()
64+
{
65+
MyWebApi
66+
.Controller<WebApiController>()
67+
.Calling(c => c.BadRequestWithErrorAction())
68+
.ShouldReturnBadRequest()
69+
.WithErrorMessage()
70+
.EndingWith("request");
71+
}
72+
73+
[Test]
74+
[ExpectedException(
75+
typeof(BadRequestResultAssertionException),
76+
ExpectedMessage = "When calling BadRequestWithErrorAction action in WebApiController expected bad request error message to end with 'Bad', but instead found 'Bad request'.")]
77+
public void EndingWithShouldThrowExceptionWithIncorrectErrorMessage()
78+
{
79+
MyWebApi
80+
.Controller<WebApiController>()
81+
.Calling(c => c.BadRequestWithErrorAction())
82+
.ShouldReturnBadRequest()
83+
.WithErrorMessage()
84+
.EndingWith("Bad");
85+
}
86+
87+
[Test]
88+
public void ContainingShouldNotThrowExceptionWithProperErrorMessage()
89+
{
90+
MyWebApi
91+
.Controller<WebApiController>()
92+
.Calling(c => c.BadRequestWithErrorAction())
93+
.ShouldReturnBadRequest()
94+
.WithErrorMessage()
95+
.Containing("d r");
96+
}
97+
98+
[Test]
99+
[ExpectedException(
100+
typeof(BadRequestResultAssertionException),
101+
ExpectedMessage = "When calling BadRequestWithErrorAction action in WebApiController expected bad request error message to contain 'Another', but instead found 'Bad request'.")]
102+
public void ContainingShouldThrowExceptionWithIncorrectErrorMessage()
103+
{
104+
MyWebApi
105+
.Controller<WebApiController>()
106+
.Calling(c => c.BadRequestWithErrorAction())
107+
.ShouldReturnBadRequest()
108+
.WithErrorMessage()
109+
.Containing("Another");
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)