Skip to content

Commit 441ad02

Browse files
committed
Updated readme based on extended documentation appearing on TestStack docs site
1 parent 4ab50fb commit 441ad02

File tree

1 file changed

+51
-203
lines changed

1 file changed

+51
-203
lines changed

README.md

Lines changed: 51 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
TestStack.FluentMVCTesting
1+
TestStack.FluentMVCTesting
22
====================================
33

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.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/).
55

66
This library is testing framework agnostic, so you can combine it with the testing library of your choice (e.g. NUnit, xUnit, etc.).
77

@@ -14,7 +14,7 @@ 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 full installation and usage instructions.
17+
Please see [the documentation](http://docs.teststack.net/fluentmvctesting/) for full installation and usage instructions.
1818

1919

2020
Installation
@@ -39,13 +39,14 @@ If you get the following exception:
3939

4040
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.
4141

42-
Examples
43-
--------
42+
Show me the code!
43+
-----------------
4444

45-
### Recommended class structure
45+
Make sure to check out [the documentation](http://docs.teststack.net/fluentmvctesting/) for full usage instructions.
4646

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).
4848

49+
```c#
4950
using MyApp.Controllers;
5051
using NUnit.Framework;
5152
using TestStack.FluentMVCTesting;
@@ -62,216 +63,63 @@ The following code snippet is an example for how to set up a test class to use F
6263
{
6364
_controller = new HomeController();
6465
}
66+
}
67+
}
68+
```
69+
70+
Then you can create a test like this:
6571

72+
```c#
6673
[Test]
6774
public void Render_default_view_for_get_to_index()
6875
{
6976
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView();
7077
}
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):
78+
```
15079

151-
_controller.WithCallTo(c => c.Index())
152-
.ShouldRedirectTo<SomeOtherController>(c2 => c2.SomeAction());
80+
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?
15381

154-
_controller.WithCallTo(c => c.Index())
155-
.ShouldRedirectTo<SomeOtherController>(typeof(SomeOtherController).GetMethod("SomeAction"));
82+
Here are some other random examples of assertions that you can make (see the documentation for the full list):
15683

157-
### View results (where the view name is the same as the action name - explicitly or via an empty string)
84+
```c#
85+
var vm = new SomeViewModel();
86+
_controller.WithModelErrors().WithCallTo(c => c.Index(vm))
87+
.ShouldRenderDefaultView()
88+
.WithModel(vm);
15889

159-
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView();
90+
_controller.WithCallTo(c => c.Index())
91+
.ShouldRenderDefaultView()
92+
.WithModel<ModelType>()
93+
.AndModelErrorFor(m => m.Property1).ThatEquals("The error message.")
94+
.AndModelErrorFor(m => m.Property2);
16095

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.");
96+
_controller.WithCallTo(c => c.Index())
97+
.ShouldRenderView("ViewName");
25298

253-
// Start of message
254-
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
255-
.WithModel<ModelType>()
256-
.AndModelErrorFor(m => m.Property1).BeginningWith("The error");
99+
_controller.WithCallTo(c => c.Index())
100+
.ShouldReturnEmptyResult();
257101

258-
// End of message
259-
_controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView()
260-
.WithModel<ModelType>()
261-
.AndModelErrorFor(m => m.Property1).EndingWith("message.");
102+
_controller.WithCallTo(c => c.Index())
103+
.ShouldRedirectTo("http://www.google.com.au/");
262104

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-
105+
_controller.WithCallTo(c => c.ActionWithRedirectToAction())
106+
.ShouldRedirectTo(c => c.ActionInSameController);
107+
108+
_controller.WithCallTo(c => c.Index())
109+
.ShouldRedirectTo<SomeOtherController>(c => c.SomeAction());
110+
111+
_controller.WithCallTo(c => c.Index())
112+
.ShouldRenderFile("text/plain");
113+
114+
_controller.WithCallTo(c => c.Index())
115+
.ShouldGiveHttpStatus(404);
116+
117+
_controller.WithCallTo(c => c.Index()).ShouldReturnJson(data =>
118+
{
119+
Assert.That(data.SomeProperty, Is.EqualTo("SomeValue");
120+
}
121+
);
122+
```
275123

276124
Any questions, comments or additions?
277125
-------------------------------------

0 commit comments

Comments
 (0)