Skip to content

Commit 76d9a03

Browse files
committed
Update master
1 parent 5db0884 commit 76d9a03

1 file changed

Lines changed: 75 additions & 1 deletion

File tree

README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,76 @@
11
ConventionTests
2-
===============
2+
===============
3+
4+
### What is ConventionTests?
5+
Convention over Configuration is a great way to cut down repetitive boilerplate code. But how do you validate that your code adheres to your conventions? Convention Tests is a code-only NuGet that provides a simple API to build validation rules for convention validation tests.
6+
7+
### Using Con­ven­tion­Tests
8+
Con­ven­tion­Tests is a sim­ple code-only Nuget that pro­vides a min­i­mal­is­tic and lim­ited API enforc­ing cer­tain struc­ture when writ­ing con­ven­tion tests and inte­grat­ing with NUnit. Installing it will add two .cs files to the project and a few depen­den­cies ([NUnit](http://www.nunit.org/), [Castle Wind­sor](http://stw.castleproject.org/Windsor.MainPage.ashx) and [ApprovalTests](http://approvaltests.sourceforge.net/)).
9+
10+
ConventionTests.NUnit file is where all the rel­e­vant code is located and __Run file is the file that runs your tests. The approach is to cre­ate a file per con­ven­tion and name them in a descrip­tive man­ner, so that you can learn what the con­ven­tions you have in the project are by just look­ing at the files in your Con­ven­tions folder, with­out hav­ing to open them.
11+
12+
Each con­ven­tion test inher­its (directly or indi­rectly) from the ICon­ven­tion­Test inter­face. There’s an abstract imple­men­ta­tion of the inter­face, Con­ven­tion­Test­Base and a few spe­cial­ized imple­men­ta­tions for com­mon sce­nar­ios pro­vided out of the box: Type-based one (Con­ven­tion­Test) and two for Wind­sor (Wind­sor­Con­ven­tion­Test, non-generic and generic for diagnostics-based tests).
13+
14+
#### Type-based con­ven­tion tests
15+
The most com­mon and most generic group of con­ven­tions are ones based around types and type infor­ma­tion. Con­ven­tions like “every controller’s name ends with ‘Con­troller’”, or “Every method on WCF ser­vice con­tracts must have Oper­a­tionCon­trac­tAt­tribute” are exam­ples of such conventions.
16+
17+
You write them by cre­at­ing a class inher­it­ing Con­ven­tion­Test, which forces you to over­ride one method. Here’s a min­i­mal example
18+
19+
public class Controllers_have_Controller_suffix_in_type_name : ConventionTest
20+
{
21+
protected override ConventionData SetUp()
22+
{
23+
return new ConventionData
24+
{
25+
Types = t => t.IsConcrete<IController>(),
26+
Must = t => t.Name.EndsWith("Controller")
27+
};
28+
}
29+
}
30+
31+
#### Windsor-based con­ven­tion tests
32+
Another com­mon set of con­ven­tion tests are tests regard­ing an IoC con­tainer. Cas­tle Wind­sor is sup­ported out of the box. The struc­ture of the tests and API is sim­i­lar, with the dif­fer­ence being that instead of types we’re deal­ing with Windsor’s com­po­nent Han­dlers.
33+
34+
public class List_classes_registered_in_Windsor : WindsorConventionTest
35+
{
36+
protected override WindsorConventionData SetUp()
37+
{
38+
return new WindsorConventionData(new WindsorContainer()
39+
.Install(FromAssembly.Containing<AuditedAction>()))
40+
{
41+
FailDescription = "All Windsor components",
42+
FailItemDescription = h => BuildDetailedHandlerDescription(h)+" | "+
43+
h.ComponentModel.GetLifestyleDescription(),
44+
}.WithApprovedExceptions("We just list all of them.");
45+
46+
}
47+
}
48+
49+
#### Cus­tom con­ven­tion tests
50+
Say we wanted to cre­ate a con­ven­tion test that lists all of our NHibernate col­lec­tions where we do cas­cade deletes, so that when we add a new col­lec­tion the test would fail remind­ing us of the issue, and force us to pay atten­tion to how we struc­ture rela­tion­ships in the appli­ca­tion. To do this we could cre­ate a base NHiber­nate­Con­ven­tion­Test and NHi­iber­nate­Con­ven­tion­Data to cre­ate sim­i­lar struc­ture, or just build a sim­ple one-class con­ven­tion like that:
51+
52+
public class List_collection_that_cascade_deletes:ConventionTestBase
53+
{
54+
public override void Execute()
55+
{
56+
// NH Bootstrapper is our custom class to set up NH
57+
var bootstrapper = new NHibernateBootstrapper();
58+
var configuration = bootstrapper.BuildConfiguration();
59+
60+
var message = new StringBuilder("Collections with cascade delete orphan");
61+
foreach (var @class in configuration.ClassMappings)
62+
{
63+
foreach (var property in @class.PropertyIterator)
64+
{
65+
if(property.CascadeStyle.HasOrphanDelete)
66+
{
67+
message.AppendLine(@class.NodeName + "." + property.Name);
68+
}
69+
}
70+
}
71+
Approve(message.ToString());
72+
}
73+
}
74+
75+
### Where to find out more
76+
[Krzysztof Koźmic](https://github.com/kkozmic) spoke about ConventionTests at NDC 2012. You can find the video of that talk [here](http://vimeo.com/43676874), slides [here](http://kozmic.pl/presentations/) and the introductory blog post [here](http://kozmic.pl/2012/06/14/using-conventiontests/).

0 commit comments

Comments
 (0)