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
Copy file name to clipboardExpand all lines: README.md
+258-1Lines changed: 258 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,264 @@ 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 installation and usage instructions.
17
+
Please see [the documentation](http://teststack.github.com/pages/fluentmvctesting.html) for full installation and usage instructions.
18
+
19
+
20
+
Installation
21
+
------------
22
+
23
+
You can install this library using NuGet into your Test Library; it will automatically reference System.Web and System.Web.Mvc (via NuGet packages, sorry it also installs a heap of other dependencies - it would be cool if Microsoft provided a package with just the MVC dll!) for you.
24
+
25
+
If you are using ASP.NET MVC 4 then:
26
+
27
+
Install-Package TestStack.FluentMVCTesting
28
+
29
+
If you are using ASP.NET MVC 3 then:
30
+
31
+
Install-Package TestStack.FluentMVCTesting.Mvc3
32
+
33
+
Known Issues
34
+
------------
35
+
36
+
If you get the following exception:
37
+
38
+
System.Security.VerificationException : Method FluentMVCTesting.ControllerExtensions.WithCallTo: type argument 'MyApp.Controllers.MyController' violates the constraint of type parameter 'T'.
39
+
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
+
42
+
Examples
43
+
--------
44
+
45
+
### Recommended class structure
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.
48
+
49
+
using MyApp.Controllers;
50
+
using NUnit.Framework;
51
+
using TestStack.FluentMVCTesting;
52
+
53
+
namespace MyApp.Tests.Controllers
54
+
{
55
+
[TestFixture]
56
+
class HomeControllerShould
57
+
{
58
+
private HomeController _controller;
59
+
60
+
[SetUp]
61
+
public void Setup()
62
+
{
63
+
_controller = new HomeController();
64
+
}
65
+
66
+
[Test]
67
+
public void Render_default_view_for_get_to_index()
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):
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