|
1 | 1 | # MyWebApi - ASP.NET Web API Fluent Testing Framework |
2 | | -==================================== |
| 2 | +------------------------------------ |
3 | 3 |
|
4 | 4 | MyWebApi is unit testing framework providing easy fluent interface to test the ASP.NET Web API framework. Inspired by [TestStack.FluentMVCTesting](https://github.com/TestStack/TestStack.FluentMVCTesting) and [ChaiJS](https://github.com/chaijs/chai) |
5 | 5 |
|
@@ -91,6 +91,83 @@ MyWebApi |
91 | 91 | .ContainingModelStateErrorFor(m => m.SecondProperty).Containing("ror mes"); // error message must contain the provided string |
92 | 92 | ``` |
93 | 93 |
|
| 94 | +### Action results |
| 95 | + |
| 96 | +You can test for specific return values or the default IHttpActionResult types: |
| 97 | + |
| 98 | +#### Generic result |
| 99 | + |
| 100 | +Useful where the action does not return IHttpActionResult |
| 101 | + |
| 102 | +```c# |
| 103 | +// tests whether the action returns certain type by providing generic parameter |
| 104 | +MyWebApi |
| 105 | + .Controller<WebApiController>() |
| 106 | + .Calling(c => c.SomeAction()) |
| 107 | + .ShouldReturn<ResponseModel>(); |
| 108 | + |
| 109 | +// tests whether the action returns certain type by using typeof |
| 110 | +MyWebApi |
| 111 | + .Controller<WebApiController>() |
| 112 | + .Calling(c => c.SomeAction()) |
| 113 | + .ShouldReturn(typeof(ResponseModel)); |
| 114 | + |
| 115 | +// tests whether the action returns generic model |
| 116 | +MyWebApi |
| 117 | + .Controller<WebApiController>() |
| 118 | + .Calling(c => c.SomeAction()) |
| 119 | + .ShouldReturn(typeof(IList<>)); // works with IEnumerable<> (or IList<ResponseModel>) too by using polymorphism |
| 120 | +``` |
| 121 | + |
| 122 | +#### OkResult |
| 123 | + |
| 124 | +```c# |
| 125 | +// tests whether the action returns OkResult |
| 126 | +MyWebApi |
| 127 | + .Controller<WebApiController>() |
| 128 | + .Calling(c => c.SomeAction()) |
| 129 | + .ShouldReturnOk(); |
| 130 | + |
| 131 | +// tests whether the action returns OkResult with no response model |
| 132 | +MyWebApi |
| 133 | + .Controller<WebApiController>() |
| 134 | + .Calling(c => c.SomeAction()) |
| 135 | + .ShouldReturnOk() |
| 136 | + .WithNoResponseModel(); |
| 137 | + |
| 138 | +// tests whether the action returns OkResult with specific response model type |
| 139 | +MyWebApi |
| 140 | + .Controller<WebApiController>() |
| 141 | + .Calling(c => c.SomeAction()) |
| 142 | + .ShouldReturnOk() |
| 143 | + .WithResponseModel<ResponseModel>(); |
| 144 | + |
| 145 | +// tests whether the action returns OkResult with specific object |
| 146 | +MyWebApi |
| 147 | + .Controller<WebApiController>() |
| 148 | + .Calling(c => c.SomeAction()) |
| 149 | + .ShouldReturnOk() |
| 150 | + .WithResponseModel(someResponseModelObject); |
| 151 | + |
| 152 | +// tests whether the action returns OkResult with specific response model passing certain assertions |
| 153 | +MyWebApi |
| 154 | + .Controller<WebApiController>() |
| 155 | + .Calling(c => c.SomeAction()) |
| 156 | + .ShouldReturnOk() |
| 157 | + .WithResponseModel<ResponseModel>(m => |
| 158 | + { |
| 159 | + Assert.AreEqual(1, m.Id); |
| 160 | + Assert.AreEqual("Some property value", m.SomeProperty); |
| 161 | + }); |
| 162 | + |
| 163 | +// tests whether the action returns OkResult with specific response model passing a predicate |
| 164 | +MyWebApi |
| 165 | + .Controller<WebApiController>() |
| 166 | + .Calling(c => c.SomeAction()) |
| 167 | + .ShouldReturnOk() |
| 168 | + .WithResponseModel<ResponseModel>(m => m.Id == 1); |
| 169 | +``` |
| 170 | + |
94 | 171 | ## Any questions, comments or additions? |
95 | 172 |
|
96 | 173 | Leave an issue on the [issues page](https://github.com/ivaylokenov/MyWebApi/issues) or send a [pull request](https://github.com/ivaylokenov/MyWebApi/pulls). |
0 commit comments