Skip to content

Commit 21e0153

Browse files
committed
Updated README.md
1 parent 9975d1a commit 21e0153

File tree

312 files changed

+30272
-1
lines changed

Some content is hidden

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

312 files changed

+30272
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h1><img src="https://raw.githubusercontent.com/ivaylokenov/MyTested.WebApi/master/documentation/logo.png" align="left" alt="MyTested.WebApi" width="100">&nbsp;&nbsp;&nbsp; MyTested.WebApi - ASP.NET Web API <br />&nbsp;&nbsp;&nbsp; Fluent Testing Framework</h1>
1+
<h1><img src="https://raw.githubusercontent.com/ivaylokenov/MyTested.WebApi/master/documentation/logo.png" align="left" alt="MyTested.WebApi" width="100">&nbsp;&nbsp;&nbsp; MyTested.WebApi - ASP.NET<br />&nbsp;&nbsp;&nbsp; Web API Fluent Testing Framework</h1>
22
====================================
33

44
MyTested.WebApi is unit testing library providing easy fluent interface to test the ASP.NET Web API 2 framework. It is testing framework agnostic, so you can combine it with the testing library of your choice (e.g. NUnit, xUnit, etc.). Inspired by [ChaiJS](https://github.com/chaijs/chai).
90.6 KB
Binary file not shown.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// MyTested.WebApi - ASP.NET Web API Fluent Testing Framework
2+
// Copyright (C) 2015 Ivaylo Kenov.
3+
//
4+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
5+
namespace MyTested.WebApi.Tests.BuildersTests.ActionsTests.ShouldHave
6+
{
7+
using Exceptions;
8+
using NUnit.Framework;
9+
using Setups.Controllers;
10+
11+
[TestFixture]
12+
public class ShouldHaveActionAttributesTests
13+
{
14+
[Test]
15+
public void NoActionAttributesShouldNotThrowExceptionWithActionContainingNoAttributes()
16+
{
17+
MyWebApi
18+
.Controller<WebApiController>()
19+
.Calling(c => c.OkResultAction())
20+
.ShouldHave()
21+
.NoActionAttributes();
22+
}
23+
24+
[Test]
25+
public void NoActionAttributesShouldNotThrowExceptionWithVoidActionContainingNoAttributes()
26+
{
27+
MyWebApi
28+
.Controller<WebApiController>()
29+
.Calling(c => c.EmptyAction())
30+
.ShouldHave()
31+
.NoActionAttributes();
32+
}
33+
34+
[Test]
35+
[ExpectedException(
36+
typeof(AttributeAssertionException),
37+
ExpectedMessage = "When calling NormalActionWithAttributes action in WebApiController expected action to not have any attributes, but it had some.")]
38+
public void NoActionAttributesShouldThrowExceptionWithActionContainingAttributes()
39+
{
40+
MyWebApi
41+
.Controller<WebApiController>()
42+
.Calling(c => c.NormalActionWithAttributes())
43+
.ShouldHave()
44+
.NoActionAttributes();
45+
}
46+
47+
[Test]
48+
[ExpectedException(
49+
typeof(AttributeAssertionException),
50+
ExpectedMessage = "When calling EmptyActionWithAttributes action in WebApiController expected action to not have any attributes, but it had some.")]
51+
public void NoActionAttributesShouldThrowExceptionWithVoidActionContainingAttributes()
52+
{
53+
MyWebApi
54+
.Controller<WebApiController>()
55+
.Calling(c => c.EmptyActionWithAttributes())
56+
.ShouldHave()
57+
.NoActionAttributes();
58+
}
59+
60+
[Test]
61+
public void ActionAttributesShouldNotThrowEceptionWithActionContainingAttributes()
62+
{
63+
MyWebApi
64+
.Controller<WebApiController>()
65+
.Calling(c => c.NormalActionWithAttributes())
66+
.ShouldHave()
67+
.ActionAttributes();
68+
}
69+
70+
[Test]
71+
[ExpectedException(
72+
typeof(AttributeAssertionException),
73+
ExpectedMessage = "When calling OkResultAction action in WebApiController expected action to have at least 1 attribute, but in fact none was found.")]
74+
public void ActionAttributesShouldThrowEceptionWithActionContainingNoAttributes()
75+
{
76+
MyWebApi
77+
.Controller<WebApiController>()
78+
.Calling(c => c.OkResultAction())
79+
.ShouldHave()
80+
.ActionAttributes();
81+
}
82+
83+
[Test]
84+
public void ActionAttributesShouldNotThrowEceptionWithVoidActionContainingAttributes()
85+
{
86+
MyWebApi
87+
.Controller<WebApiController>()
88+
.Calling(c => c.EmptyActionWithAttributes())
89+
.ShouldHave()
90+
.ActionAttributes();
91+
}
92+
93+
[Test]
94+
[ExpectedException(
95+
typeof(AttributeAssertionException),
96+
ExpectedMessage = "When calling EmptyAction action in WebApiController expected action to have at least 1 attribute, but in fact none was found.")]
97+
public void ActionAttributesShouldThrowEceptionWithVoidActionContainingNoAttributes()
98+
{
99+
MyWebApi
100+
.Controller<WebApiController>()
101+
.Calling(c => c.EmptyAction())
102+
.ShouldHave()
103+
.ActionAttributes();
104+
}
105+
106+
[Test]
107+
public void ActionAttributesShouldNotThrowEceptionWithActionContainingNumberOfAttributes()
108+
{
109+
MyWebApi
110+
.Controller<WebApiController>()
111+
.Calling(c => c.NormalActionWithAttributes())
112+
.ShouldHave()
113+
.ActionAttributes(withTotalNumberOf: 3);
114+
}
115+
116+
[Test]
117+
[ExpectedException(
118+
typeof(AttributeAssertionException),
119+
ExpectedMessage = "When calling NormalActionWithAttributes action in WebApiController expected action to have 10 attributes, but in fact found 3.")]
120+
public void ActionAttributesShouldThrowEceptionWithActionContainingNumberOfAttributes()
121+
{
122+
MyWebApi
123+
.Controller<WebApiController>()
124+
.Calling(c => c.NormalActionWithAttributes())
125+
.ShouldHave()
126+
.ActionAttributes(withTotalNumberOf: 10);
127+
}
128+
129+
[Test]
130+
[ExpectedException(
131+
typeof(AttributeAssertionException),
132+
ExpectedMessage = "When calling NormalActionWithAttributes action in WebApiController expected action to have 1 attribute, but in fact found 3.")]
133+
public void ActionAttributesShouldThrowEceptionWithActionContainingNumberOfAttributesTestingWithOne()
134+
{
135+
MyWebApi
136+
.Controller<WebApiController>()
137+
.Calling(c => c.NormalActionWithAttributes())
138+
.ShouldHave()
139+
.ActionAttributes(withTotalNumberOf: 1);
140+
}
141+
}
142+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// MyTested.WebApi - ASP.NET Web API Fluent Testing Framework
2+
// Copyright (C) 2015 Ivaylo Kenov.
3+
//
4+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
5+
namespace MyTested.WebApi.Tests.BuildersTests.ActionsTests.ShouldHave
6+
{
7+
using System;
8+
using Exceptions;
9+
using NUnit.Framework;
10+
using Setups;
11+
using Setups.Controllers;
12+
using Setups.Models;
13+
14+
[TestFixture]
15+
public class ShouldHaveModelStateTests
16+
{
17+
[Test]
18+
public void ShouldHaveModelStateForShouldChainCorrectly()
19+
{
20+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
21+
22+
MyWebApi
23+
.Controller<WebApiController>()
24+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
25+
.ShouldHave()
26+
.ModelStateFor<RequestModel>()
27+
.ContainingNoModelStateErrorFor(r => r.NonRequiredString)
28+
.ContainingModelStateErrorFor(r => r.Integer)
29+
.ContainingModelStateErrorFor(r => r.RequiredString);
30+
}
31+
32+
[Test]
33+
public void ShouldHaveValidModelStateShouldBeValidWithValidRequestModel()
34+
{
35+
var requestModel = TestObjectFactory.GetValidRequestModel();
36+
37+
MyWebApi
38+
.Controller<WebApiController>()
39+
.Calling(c => c.ModelStateCheck(requestModel))
40+
.ShouldHave()
41+
.ValidModelState();
42+
}
43+
44+
[Test]
45+
[ExpectedException(
46+
typeof(ModelErrorAssertionException),
47+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have valid model state with no errors, but it had some.")]
48+
public void ShouldHaveValidModelStateShouldThrowExceptionWithInvalidRequestModel()
49+
{
50+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
51+
52+
MyWebApi
53+
.Controller<WebApiController>()
54+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
55+
.ShouldHave()
56+
.ValidModelState();
57+
}
58+
59+
[Test]
60+
public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModel()
61+
{
62+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
63+
64+
MyWebApi
65+
.Controller<WebApiController>()
66+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
67+
.ShouldHave()
68+
.InvalidModelState();
69+
}
70+
71+
[Test]
72+
public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModelAndCorrectNumberOfErrors()
73+
{
74+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
75+
76+
MyWebApi
77+
.Controller<WebApiController>()
78+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
79+
.ShouldHave()
80+
.InvalidModelState(2);
81+
}
82+
83+
[Test]
84+
[ExpectedException(
85+
typeof(ModelErrorAssertionException),
86+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but in fact contained 2.")]
87+
public void ShouldHaveInvalidModelStateShouldBeInvalidWithInvalidRequestModelAndIncorrectNumberOfErrors()
88+
{
89+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
90+
91+
MyWebApi
92+
.Controller<WebApiController>()
93+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
94+
.ShouldHave()
95+
.InvalidModelState(5);
96+
}
97+
98+
[Test]
99+
[ExpectedException(
100+
typeof(ModelErrorAssertionException),
101+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state, but was in fact valid.")]
102+
public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModel()
103+
{
104+
var requestModel = TestObjectFactory.GetValidRequestModel();
105+
106+
MyWebApi
107+
.Controller<WebApiController>()
108+
.Calling(c => c.ModelStateCheck(requestModel))
109+
.ShouldHave()
110+
.InvalidModelState();
111+
}
112+
113+
[Test]
114+
[ExpectedException(
115+
typeof(ModelErrorAssertionException),
116+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but in fact contained 0.")]
117+
public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModelAndProvidedNumberOfErrors()
118+
{
119+
var requestModel = TestObjectFactory.GetValidRequestModel();
120+
121+
MyWebApi
122+
.Controller<WebApiController>()
123+
.Calling(c => c.ModelStateCheck(requestModel))
124+
.ShouldHave()
125+
.InvalidModelState(withNumberOfErrors: 5);
126+
}
127+
128+
[Test]
129+
public void AndShouldWorkCorrectlyWithValidModelState()
130+
{
131+
var requestModel = TestObjectFactory.GetValidRequestModel();
132+
133+
MyWebApi
134+
.Controller<WebApiController>()
135+
.Calling(c => c.ModelStateCheck(requestModel))
136+
.ShouldHave()
137+
.ValidModelState()
138+
.AndAlso()
139+
.ShouldReturn()
140+
.Ok();
141+
}
142+
143+
[Test]
144+
public void AndShouldWorkCorrectlyWithInvalidModelState()
145+
{
146+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
147+
148+
MyWebApi
149+
.Controller<WebApiController>()
150+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
151+
.ShouldHave()
152+
.InvalidModelState()
153+
.AndAlso()
154+
.ShouldReturn()
155+
.Ok();
156+
}
157+
158+
[Test]
159+
[ExpectedException(
160+
typeof(InvalidOperationException),
161+
ExpectedMessage = "AndProvideTheModel can be used when there is response model from the action.")]
162+
public void AndProvideModelShouldThrowExceptionWhenIsCalledOnTheRequest()
163+
{
164+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
165+
166+
MyWebApi
167+
.Controller<WebApiController>()
168+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
169+
.ShouldHave()
170+
.ModelStateFor<RequestModel>()
171+
.ContainingNoModelStateErrorFor(r => r.NonRequiredString)
172+
.ContainingModelStateErrorFor(r => r.Integer)
173+
.ContainingModelStateErrorFor(r => r.RequiredString)
174+
.AndProvideTheModel();
175+
}
176+
}
177+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// MyTested.WebApi - ASP.NET Web API Fluent Testing Framework
2+
// Copyright (C) 2015 Ivaylo Kenov.
3+
//
4+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
5+
namespace MyTested.WebApi.Tests.BuildersTests.ActionsTests.ShouldReturn
6+
{
7+
using Exceptions;
8+
using NUnit.Framework;
9+
using Setups;
10+
using Setups.Controllers;
11+
12+
[TestFixture]
13+
public class ShouldReturnBadRequestTests
14+
{
15+
[Test]
16+
public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsBadRequest()
17+
{
18+
MyWebApi
19+
.Controller<WebApiController>()
20+
.Calling(c => c.BadRequestAction())
21+
.ShouldReturn()
22+
.BadRequest();
23+
}
24+
25+
[Test]
26+
public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsInvalidModelStateResult()
27+
{
28+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
29+
30+
MyWebApi
31+
.Controller<WebApiController>()
32+
.Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
33+
.ShouldReturn()
34+
.BadRequest();
35+
}
36+
37+
[Test]
38+
public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsBadRequestErrorMessageResult()
39+
{
40+
MyWebApi
41+
.Controller<WebApiController>()
42+
.Calling(c => c.BadRequestWithErrorAction())
43+
.ShouldReturn()
44+
.BadRequest();
45+
}
46+
47+
[Test]
48+
[ExpectedException(
49+
typeof(HttpActionResultAssertionException),
50+
ExpectedMessage = "When calling NotFoundAction action in WebApiController expected action result to be BadRequestResult, but instead received NotFoundResult.")]
51+
public void ShouldReturnNotFoundShouldThrowExceptionWhenActionDoesNotReturnNotFound()
52+
{
53+
MyWebApi
54+
.Controller<WebApiController>()
55+
.Calling(c => c.NotFoundAction())
56+
.ShouldReturn()
57+
.BadRequest();
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)