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
+5-250Lines changed: 5 additions & 250 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,257 +11,12 @@ The motivation behind this library is to provide a way to test MVC actions quick
11
11
12
12
This library was inspired by the [MVCContrib.TestHelper](http://mvccontrib.codeplex.com/wikipage?title=TestHelper) library.
13
13
14
-
Installation
15
-
------------
14
+
Documentation
15
+
-------------
16
16
17
-
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.
18
-
19
-
If you are using ASP.NET MVC 4 then:
20
-
21
-
Install-Package TestStack.FluentMVCTesting
22
-
23
-
If you are using ASP.NET MVC 3 then:
24
-
25
-
Install-Package TestStack.FluentMVCTesting.Mvc3
26
-
27
-
Known Issues
28
-
------------
29
-
30
-
If you get the following exception:
31
-
32
-
System.Security.VerificationException : Method FluentMVCTesting.ControllerExtensions.WithCallTo: type argument 'MyApp.Controllers.MyController' violates the constraint of type parameter 'T'.
33
-
34
-
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.
35
-
36
-
Examples
37
-
--------
38
-
39
-
### Recommended class structure
40
-
41
-
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.
42
-
43
-
using MyApp.Controllers;
44
-
using NUnit.Framework;
45
-
using TerseControllerTesting;
46
-
47
-
namespace MyApp.Tests.Controllers
48
-
{
49
-
[TestFixture]
50
-
class HomeControllerShould
51
-
{
52
-
private HomeController _controller;
53
-
54
-
[SetUp]
55
-
public void Setup()
56
-
{
57
-
_controller = new HomeController();
58
-
}
59
-
60
-
[Test]
61
-
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.
71
-
72
-
### Model binding errors
73
-
74
-
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.
99
-
100
-
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:
101
-
102
-
public ActionResult ActionWithNoParameters()
103
-
{
104
-
return new EmptyResult();
105
-
}
106
-
public ActionResult RedirectToActionWithNoParameters()
I can explicitly define whatever signatures I want to allow this terser syntax, but obviously the different permutations that are possible are mind-boggling and it wouldn't be helpful for any custom types in your project. Unfortunately, despite best efforts I couldn't figure out a way to generically specify these definitions - if anyone has ideas for how to do this let me know!
117
-
118
-
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:
119
-
120
-
public ActionResult SomeAction(string param1, int param2, bool param3) {}
121
-
122
-
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:
If you use this option (I don't recommend it because it uses a "magic" string so if you change the action name then the string won't change, although at least the test will break because the Method name will no longer be valid; in saying that if you change your parameters more often than the action name this might be a better option) be careful that you don't get an AmbiguousMatchException if there are multiple actions with that name.
138
-
139
-
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 this GitHub project and I can add them.
140
-
141
-
### Redirect to action in another controller
142
-
143
-
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):
/* Assertions on the data being turned into json (data) */}
188
-
);
189
-
190
-
### Model tests
191
-
192
-
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 */});
209
-
210
-
Note: if you use any of these model tests then it will check that the model passed through isn't null.
211
-
212
-
### Model error tests
213
-
214
-
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:
Please see [the documentation](http://teststack.github.com/pages/fluentmvctesting.html) for installation and usage instructions.
263
18
264
19
Any questions, comments or additions?
265
-
--------------------------
20
+
-------------------------------------
266
21
267
-
Leave an issue on the [GitHub project](https://github.com/TestStack/TestStack.FluentMVCTesting/issues) or send a [pull request](https://github.com/TestStack/TestStack.FluentMVCTesting/pulls).
22
+
Leave an issue on the [issues page](https://github.com/TestStack/TestStack.FluentMVCTesting/issues) or send a [pull request](https://github.com/TestStack/TestStack.FluentMVCTesting/pulls).
0 commit comments