Skip to content

Commit d294334

Browse files
committed
Reverting accidental change to readme
1 parent ef40826 commit d294334

File tree

1 file changed

+258
-1
lines changed

1 file changed

+258
-1
lines changed

README.md

Lines changed: 258 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,264 @@ This library was inspired by the [MVCContrib.TestHelper](http://mvccontrib.codep
1414
Documentation
1515
-------------
1616

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()
68+
{
69+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView();
70+
}
71+
}
72+
}
73+
74+
### Basic syntax
75+
76+
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.:
81+
82+
_controller.WithModelErrors().WithCallTo(c => c.Index()).ShouldRenderDefaultView();
83+
84+
### Child actions
85+
86+
If you want to check that the action being called has the `[ChildActionOnly]` attribute then use `WithCallToChild()` rather than `WithCallTo()`, e.g.:
87+
88+
_controller.WithCallToChild(c => c.Index()).ShouldRenderDefaultView();
89+
90+
### Empty result
91+
92+
_controller.WithCallTo(c => c.Index()).ShouldReturnEmptyResult();
93+
94+
### Redirect to url
95+
96+
_controller.WithCallTo(c => c.Index()).ShouldRedirectTo("http://www.google.com.au/");
97+
98+
### Redirect to route name
99+
100+
_controller.WithCallTo(c => c.Index()).ShouldRedirectToRoute("RouteName");
101+
102+
### Redirect to action within the same controller
103+
104+
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()
113+
{
114+
return RedirectToAction("ActionWithNoParameters");
115+
}
116+
117+
Then you can write this test:
118+
119+
_controller.WithCallTo(c => c.RedirectToActionWithNoParameters())
120+
.ShouldRedirectTo(c => c.ActionWithNoParameters);
121+
122+
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:
129+
130+
_controller.WithCallTo(c => c.Index())
131+
.ShouldRedirectTo<string, int, bool>(c => c.SomeAction);
132+
133+
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:
134+
135+
_controller.WithCallTo(c => c.Index())
136+
.ShouldRedirectTo(c => c.SomeAction(null, 0, false));
137+
138+
You can also pass through a MethodInfo object against the method you are redirecting to, e.g.:
139+
140+
_controller.WithCallTo(c => c.Index())
141+
.ShouldRedirectTo(typeof(HomeController).GetMethod("SomeAction"));
142+
143+
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):
150+
151+
_controller.WithCallTo(c => c.Index())
152+
.ShouldRedirectTo<SomeOtherController>(c2 => c2.SomeAction());
153+
154+
_controller.WithCallTo(c => c.Index())
155+
.ShouldRedirectTo<SomeOtherController>(typeof(SomeOtherController).GetMethod("SomeAction"));
156+
157+
### View results (where the view name is the same as the action name - explicitly or via an empty string)
158+
159+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView();
160+
161+
// Or, if you want to check a partial is returned
162+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultPartialView();
163+
164+
### View results
165+
166+
_controller.WithCallTo(c => c.Index()).ShouldRenderView("ViewName");
167+
168+
// Or, if you want to check a partial is returned
169+
_controller.WithCallTo(c => c.Index()).ShouldRenderPartialView("ViewName");
170+
171+
Unfortunately, we couldn't think of a way to get rid of the magic strings here so where possible use the default ones above.
172+
173+
See below for view model testing.
174+
175+
### Files
176+
177+
_controller.WithCallTo(c => c.Index()).ShouldRenderFile();
178+
179+
_controller.WithCallTo(c => c.Index()).ShouldRenderFile("content/type");
180+
181+
### Http status codes
182+
183+
_controller.WithCallTo(c => c.Index()).ShouldGiveHttpStatus();
184+
185+
_controller.WithCallTo(c => c.Index()).ShouldGiveHttpStatus(404);
186+
187+
### JSON
188+
189+
_controller.WithCallTo(c => c.Index()).ShouldReturnJson();
190+
191+
_controller.WithCallTo(c => c.Index()).ShouldReturnJson(data =>
192+
{
193+
/* Assertions on the data being turned into json (data), e.g. if you are using NUnit, you might have something like: */
194+
Assert.That(data.SomeProperty, Is.EqualTo("SomeValue");
195+
});
196+
197+
### Model tests
198+
199+
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.:
200+
201+
// Check the type of the model
202+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
203+
.WithModel<ModelType>();
204+
205+
// Check that a particular object was passed through as the model
206+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
207+
.WithModel(expectedModel);
208+
209+
// Check that the model that was return passes a predicate
210+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
211+
.WithModel<ModelType>(m => m.Property1 == "hello");
212+
213+
// Make assertions on the model
214+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
215+
.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.
222+
223+
// Check that there are no model errors
224+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
225+
.WithModel<ModelType>().WithNoModelErrors();
226+
227+
// Check that there is a model error against a given property in the model
228+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
229+
.WithModel<ModelType>().AndModelErrorFor(m => m.Property1);
230+
231+
// Check that there isn't a model error against a given property in the model
232+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
233+
.WithModel<ModelType>().AndNoModelErrorFor(m => m.Property1);
234+
235+
// Check that there is a model error against a specific key
236+
// Avoid if possible given it includes a magic string
237+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
238+
.WithModel<ModelType>().AndModelError("Key");
239+
240+
// You can chain these model error calls and thus check for multiple errors
241+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
242+
.WithModel<ModelType>()
243+
.AndModelErrorFor(m => m.Property1)
244+
.AndModelErrorFor(m => m.Property2);
245+
246+
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:
247+
248+
// Equality
249+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
250+
.WithModel<ModelType>()
251+
.AndModelErrorFor(m => m.Property1).ThatEquals("The error message.");
252+
253+
// Start of message
254+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
255+
.WithModel<ModelType>()
256+
.AndModelErrorFor(m => m.Property1).BeginningWith("The error");
257+
258+
// End of message
259+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
260+
.WithModel<ModelType>()
261+
.AndModelErrorFor(m => m.Property1).EndingWith("message.");
262+
263+
// Containing
264+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
265+
.WithModel<ModelType>()
266+
.AndModelErrorFor(m => m.Property1).Containing("e error m");
267+
268+
You can chain the error property checks after any of these checks (you can only perform one of the checks though):
269+
270+
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
271+
.WithModel<ModelType>()
272+
.AndModelErrorFor(m => m.Property1).ThatEquals("The error message.")
273+
.AndModelErrorFor(m => m.Property2);
274+
18275

19276
Any questions, comments or additions?
20277
-------------------------------------

0 commit comments

Comments
 (0)