You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library provides a fluent interface for creating terse and expressive tests against ASP.NET MVC controllers. This library is part of [TestStack](http://teststack.github.com/).
4
+
This library provides a fluent interface for creating terse and expressive tests against ASP.NET MVC controllers. This library is part of [TestStack](http://teststack.net/).
5
5
6
6
This library is testing framework agnostic, so you can combine it with the testing library of your choice (e.g. NUnit, xUnit, etc.).
7
7
@@ -14,7 +14,7 @@ This library was inspired by the [MVCContrib.TestHelper](http://mvccontrib.codep
14
14
Documentation
15
15
-------------
16
16
17
-
Please see [the documentation](http://teststack.github.com/pages/fluentmvctesting.html) for full installation and usage instructions.
17
+
Please see [the documentation](http://docs.teststack.net/fluentmvctesting/) for full installation and usage instructions.
18
18
19
19
20
20
Installation
@@ -39,13 +39,14 @@ If you get the following exception:
39
39
40
40
It means you are referencing a version of System.Web.Mvc that isn't compatible with the one that was used to build the dll that was generated for the NuGet package. Ensure that you are using the correct package for your version of MVC and that you are using the [AspNetMvc packages on nuget.org](https://nuget.org/packages/aspnetmvc) rather than the dll from the GAC.
41
41
42
-
Examples
43
-
--------
42
+
Show me the code!
43
+
-----------------
44
44
45
-
### Recommended class structure
45
+
Make sure to check out [the documentation](http://docs.teststack.net/fluentmvctesting/) for full usage instructions.
46
46
47
-
The following code snippet is an example for how to set up a test class to use FluentMVCTesting using NUnit, this is simply to get you started quickly, in reality you can use it how you like and with any unit testing framework of your choice.
47
+
Say you set up the following test class (this example with NUnit, but it will work for any test framework).
48
48
49
+
```c#
49
50
usingMyApp.Controllers;
50
51
usingNUnit.Framework;
51
52
usingTestStack.FluentMVCTesting;
@@ -62,216 +63,63 @@ The following code snippet is an example for how to set up a test class to use F
As you might have gathered from the previous code snippet the entry point to the library is the `WithCallTo` extension method on the MVC `Controller` class; inside of that method call you simply pass in a lamdba expression that takes the controller to make the action call that you are trying to test. From there you can use intellisense to guide your testing.
77
-
78
-
### Model binding errors
79
-
80
-
If you want to test what happens when the default model binding (or indeed any custom model binding or validation that you perform) has left errors in `ModelState` when your action is called you can simply use the `WithModelErrors()` extension method, e.g.:
There are a number of ways that you can specify this test, depending on the signature of the action you are redirecting to.
105
-
106
-
If you are redirecting to an action that takes no parameters, or takes a single int parameter, then you can use a method group, which is the tersest specification (you don't need to specify the parameters to the action), e.g. if these are two actions in the controller you are testing:
107
-
108
-
public ActionResult ActionWithNoParameters()
109
-
{
110
-
return new EmptyResult();
111
-
}
112
-
public ActionResult RedirectToActionWithNoParameters()
We can explicitly define whatever signatures we want to allow this terser syntax, but the different permutations that are possible are infinite and not possible for any custom types in your project. If anyone has ideas for how to do this better let us know!
123
-
124
-
If you have 1-3 parameters being passed into the action being redirected to then you can still use the method group, but you need to specify the types being passed into the action, e.g. if you have the following controller action being redirected to:
125
-
126
-
public ActionResult SomeAction(string param1, int param2, bool param3) {}
127
-
128
-
Then you can write the following test for the redirect:
If you have more than three parameters, or you are uncomfortable with that syntax then you can specify a lambda with a call to the action you want and pass in dummy values for the parameters, e.g. for the previous example:
We don't recommend using this latter option because it uses a "magic" string so if you change the action name then the string won't change and the test will break and also need to be updated. In saying that, if you change your parameters more often than the action name this might be a better option. If you do use this option be careful that you don't get an AmbiguousMatchException if there are multiple actions with the same name.
144
-
145
-
At this stage there isn't support for the `[ActionName()]` attribute or simply passing through a string to check against the action name, but if either are important to you feel free to [add an issue in the GitHub project](https://github.com/TestStack/TestStack.FluentMVCTesting/issues).
146
-
147
-
### Redirect to action in another controller
148
-
149
-
If you are redirecting to an action in another controller, then there are two syntaxes that you can currently use (similar to the last two mentioned above):
This checks that when `_controller.Index()` is called then the `ActionResult` that is returned is of type `ViewResult` and that the view name returned is either "Index" or "" (the default view for the Index action); easy huh?
If you assert that the action returns a view of some sort there are some other methods that you can call (seen easily using intellisense). These allow you to check the model, e.g.:
.WithModel<ModelType>(m => {/* Make assertions on m */});
216
-
217
-
Note: if you use any of these model tests then it will check that the model passed through isn't null.
218
-
219
-
### Model error tests
220
-
221
-
Once you have made assertions about the model you can then make assertions that particular model errors are present for properties of that model. While it's not generally the best idea to add validation logic to controllers ([doing it unobtrusively is best](http://robdmoore.id.au/blog/2012/04/27/unobtrusive-validation-in-asp-net-mvc-3-and-4/)), sometimes it's useful.
You can also make assertions on the content of the error message(s); these methods will look for any error messages against that particular model state key that match the given criteria:
0 commit comments