|
1 | | -# MyWebApi |
| 1 | +# MyWebApi - ASP.NET Web API Fluent Testing Framework |
| 2 | +------------------------------------ |
| 3 | + |
| 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 | + |
| 6 | +[](https://ci.appveyor.com/project/ivaylokenov/mywebapi) |
| 7 | + |
| 8 | +## How to use |
| 9 | + |
| 10 | +### Controller instantiation |
| 11 | + |
| 12 | +You have a couple of options from which you can setup the controller you want to test. The framework gives you static `MyWebApi` class from which the test builder starts: |
| 13 | + |
| 14 | +```c# |
| 15 | +// instantiates controller with parameterless constructor |
| 16 | +MyWebApi |
| 17 | + .Controller<WebApiController>(); |
| 18 | + |
| 19 | +// instantiates controller with constructor function to resolve dependencies |
| 20 | +MyWebApi |
| 21 | + .Controller(() => new WebApiController(mockedInjectedService)); |
| 22 | +``` |
| 23 | + |
| 24 | +### Calling actions |
| 25 | + |
| 26 | +You can call any action using lambda expression. All parameter values will be resolved and model state validation will be performed on them: |
| 27 | + |
| 28 | +```c# |
| 29 | +// calls action with no parameters |
| 30 | +MyWebApi |
| 31 | + .Controller<WebApiController>() |
| 32 | + .Calling(c => c.SomeAction()); |
| 33 | + |
| 34 | +// calls action with parameters |
| 35 | +MyWebApi |
| 36 | + .Controller<WebApiController>() |
| 37 | + .Calling(c => c.SomeAction(requestModel)); |
| 38 | + |
| 39 | +// calls async action |
| 40 | +MyWebApi |
| 41 | + .Controller<WebApiController>() |
| 42 | + .CallingAsync(c => c.SomeActionAsync()); |
| 43 | +``` |
| 44 | + |
| 45 | +### Model state validation |
| 46 | + |
| 47 | +You can test whether model state is valid/invalid or contains any specific error: |
| 48 | + |
| 49 | +```c# |
| 50 | +// tests whether model state is valid |
| 51 | +MyWebApi |
| 52 | + .Controller<WebApiController>() |
| 53 | + .Calling(c => c.SomeAction(requestModel)) |
| 54 | + .ShouldHaveValidModelState(); |
| 55 | + |
| 56 | +// tests whether model state is not valid |
| 57 | +MyWebApi |
| 58 | + .Controller<WebApiController>() |
| 59 | + .Calling(c => c.SomeAction(requestModel)) |
| 60 | + .ShouldHaveInvalidModelState(); |
| 61 | + |
| 62 | +// tests whether model state error exists (or does not exist) for specific key (not recommended because of magic string) |
| 63 | +MyWebApi |
| 64 | + .Controller<WebApiController>() |
| 65 | + .Calling(c => c.SomeAction(requestModel)) |
| 66 | + .ShouldHaveModelStateFor<RequestModel>() |
| 67 | + .ContainingModelStateError("propertyName") |
| 68 | + .And() // calling And method is not necessary but provides better readability |
| 69 | + .ContainingNoModelStateError("anotherPropertyName"); |
| 70 | + |
| 71 | +// tests whether model state error exists by using lambda expression |
| 72 | +MyWebApi |
| 73 | + .Controller<WebApiController>() |
| 74 | + .Calling(c => c.SomeAction(requestModel)) |
| 75 | + .ShouldHaveModelStateFor<RequestModel>() |
| 76 | + .ContainingModelStateErrorFor(m => m.SomeProperty) |
| 77 | + .And() |
| 78 | + .ContainingNoModelStateErrorFor(m => m.AnotherProperty); |
| 79 | + |
| 80 | +// tests the error message for specific property |
| 81 | +MyWebApi |
| 82 | + .Controller<WebApiController>() |
| 83 | + .Calling(c => c.SomeAction(requestModel)) |
| 84 | + .ShouldHaveModelStateFor<RequestModel>() |
| 85 | + .ContainingModelStateErrorFor(m => m.SomeProperty).ThatEquals("Error message") // error message must be equal to the provided string |
| 86 | + .And() |
| 87 | + .ContainingModelStateErrorFor(m => m.SecondProperty).BeginningWith("Error") // error message must begin with the provided string |
| 88 | + .And() |
| 89 | + .ContainingModelStateErrorFor(m => m.ThirdProperty).EndingWith("message") // error message must end with the provided string |
| 90 | + .And() |
| 91 | + .ContainingModelStateErrorFor(m => m.SecondProperty).Containing("ror mes"); // error message must contain the provided string |
| 92 | +``` |
| 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 | +#### Ok result |
| 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 | +// tests for model state errors for the response model (not very useful in practice) |
| 171 | +MyWebApi |
| 172 | + .Controller<WebApiController>() |
| 173 | + .Calling(c => c.SomeAction()) |
| 174 | + .ShouldReturnOk() |
| 175 | + .WithResponseModel<ResponseModel>() |
| 176 | + .ContainingModelStateErrorFor(m => m.SomeProperty).ThatEquals("Error message") |
| 177 | + .And() |
| 178 | + .ContainingNoModelStateErrorFor(m => m.AnotherProperty); |
| 179 | +``` |
| 180 | + |
| 181 | +## Any questions, comments or additions? |
| 182 | + |
| 183 | +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