Thursday, February 14, 2008

Getting the Data out of my Controller Tests

While working on PlanningPokerOnRails I started writing a unit test for my LoginController. The Monorail BaseControllerTest makes this easy. I am also using Rhino Mocks and Nunit. Here is the test:

The test is very simple. First, it sets a single recorded expectation that a Redirect will occur:

34 using (mocks.Record())

35 {

36 // First redirects to the index page

37 Response.Redirect("/Home/Index.rails");

38 }

Next, the playback is run and the code emulates a few things that users might login with Request params for a ReturnUrl:

39 using (mocks.Playback())

40 {

41 Request.Params.Add("ReturnUrl", "/Home/Index.rails");

42 loginController.Login("dan", "danny");

43 }

Writing the Code to Pass this Test

Next I wrote the CardPlayer component (my user) and the LoginController. Seeking to do the simplest thing that could possibly work I merely had the CardPlayer inherit from the ActiveRecord base.

This made writing the controller easy, the ActiveRecord base has plenty of good methods for instantiating. I only have to call the FindOne method on the CardPlayer:

104 public static CardPlayer FindCardPlayer(string UserName,

105 string Password){


107 CardPlayer user = FindOne(NHibernate.Expression.Expression.And(

108 Expression.Eq("Username", UserName),

109 Expression.Eq("Password", Password)));

110 return user;

111 }

So why all the Problems?

I never intended to initialize ActiveRecord, in my tests assembly. I believe that test data should only be used for Acceptance Tests. It turns out that the test fails because no ActiveRecord config or initialization exists.

How do I solve this?

I forgot to implement good Dependency Injection. The way I would normally grab an instance of a component (like the CardPlayer) is through a service container (or repository). MVC architectures generally have two types of Model objects; Components (like the CardPlayer) and Services (like the CardPlayer Service I should have written). The services usually act like or wrap a repository of Components. In my opinion, ActiveReecord already provides the repository.

It turns out my solution is to write a CardPlayer Service container. This object will go into the IoC container (winsor) and be automatically injected into whatever controller requires it (like the LoginController). The service will use the ActiveRecordMediator to instantiate new CardPlayers and I can remove the ActiveRecordBase from my CardPlayer component.

Wait why does this help?

Well by forcing all instantiation to go through injected Services your tests can easily inject a Mock Service (I use Rhino Mocks) and tell the Mock to provide predefined instances of components. Since you have mocked the service layer you no longer need ActiveRecord in your tests!

Keeping the Data
Now that I've convinced you to write a service layer, I am going to tell you that you may not need it! While you can mock the Services going to your controllers you cannot easily get rid of ActiveRecord when testing the services. For most simple lookups a test may not be appropriate but in many cases your component lookup up and other operations will be non-trivial. For these cases you will want to test the service layer and you may have to initialize ActiveRecord.

You could decide to Mock the ActiveRecordMediator or you could use an approach similar to Oren's.

I'm Confused...

I admit that this is some advanced stuff. I promise to come back to this post and update it with more concise information.

Please leave comments on ways I can do this without a service layer. Thanks!


Anonymous said...

Not sure how this would impact your existing design or how far-reaching this refactoring might be in your application, but one alternative would be to not derive your model classes from the AR base classes and instead use something similar to the repository pattern with your repositories talking to ActiveRecordMediator or its generic equivalent.

You could then get your repositories from a service locator (or have them injected) into your domain objects if you want to follow a purist DD approach. The alternative would be to inject repositories or services (which have repositories injected into them) into your controllers. Monorail Windsor integration can do all this for you. The 2nd approach is more fitting with what I generally do.

Either way you can then mock out any interactions with your DB and test in isolation.

Unknown said...

What you descibed is exactly what I normally do. Normally, I would use the mediator in a Service container and use windsor to inject as needed. Part of me really prefers this IoC Inversion of Control) approach. What I've discovered, after talking to a colleague, is that the IoC pattern would seem to conflict with the AR pattern. Of course the AR pattern isn't the primary reason I use ActiveRecord. I use it for it's simple approach to ORM.

Right now (and this could change) I 'm tending towards the AR pattern. The argument being that I will have to initialize ActiveRecord to test the Services anyway.

I'm going to play with this more and post my examples/findings. This IoC versus AR question has become almost a philosophical dilemma.

Anonymous said...

I don't think there is a one size fits all answer.

We don't have to deploy to many platforms so our environment is very stable and we have a lot of control over developer machines. Its easy for us at work to say that you have to configure a DB to run tests. On an open source project that might be a killer.

I like that article you linked to about using SQLite to do the AR tests in memory. The central point there is that you have to have a clean DB for each test for them to be isolated, atomic and repeatable.

You can argue that controller tests that use AR cant be truly atomic because they make calls to code outside the class under test. But then you have separate tests to cover the model layer. Any bugs you find in the model via controller tests were bugs you missed in the model tests.

Controllers bring a lot of model objects together into a sub system. They are glue. Maybe a unit test isn't the appropriate kind of test for glue. Maybe we should really be thinking of these as subsystem tests? If you think of it that way then it makes sense not to try and isolate the controller with mocks.

One thing that Jason brought up; when they update AR they break our stuff. If we mocked AR for model tests we wouldn't know they broke our stuff. So using it in tests helps us to continually integrate with a code base we don't control.

I think we are going down the road of doing AR + BaseControllerTest. We may look at some sort of zero config database. There are several; SQLite, Firebird. We could certainly use more testing from that end of things.

It's a conundrum to be sure.

Unknown said...

I agree...the combination of the AR pattern and SQLite database really seems to work out best for trivial cases.

IoC is still relevant because some non-trivial service will inevitably expose itself and have nothing to do with ActiveRecord. One example is a service that loads objects from multiple sources; web service, database, etc.

As for controllers being glue. You're right they are actually a pretty classic example of the facade pattern.

Anonymous said...

Generic Cialis

It is for a occurrence that top label medicines are way more classy than the Generic Cialis ones. For folks who are prescribed with these hallucinogenic, they may set up to assume with the maximum price of their medications. Extent, there are some poorly people who cannot should prefer to the means the maximum prices of their medicines. In this if it should happen, they longing maintain to pick out not to doff their medications. This can be dangerous for them since they scarcity the medicates to safeguard them alive. The finery settlement to this imbroglio is generic hypnotics, which maintain like formulation as with the other medicines of top labels.

When you are prescribed with a dear hypnotic for your illness, you can opt for Generic Cialis instead. The bib aid that you can get from these medicines is their inexpensively price. Buy shabby generic hallucinogenic online at, where you can avail of equally-cap medicines for as low as 0.49 cents. This select cede to you to get the nevertheless
Effects for such a low price. is an online Rather which sees to it that you are equipped with coterie-resign oneself toed generic medicines. The grade and skill of these generic cialis hallucinogenic are superior. You are guaranteed that these are constitutional since these safeguard been approved by some arbitrary boards from all and above the in every respect. Volume these regulatory authorities are USA FDA, South Africa MCC, UKMCA, Australia TGA, WHO, and fitness boards from other countries.

Generic Cialis an online hypnotic retailer, in which you can planned the medicines that you covet shipped for unobstructed. Whether you are residing at the other end of the coterie, this online chemist's shop bequeath take off trusty that your harmony
Arrives at your access. Customers from Europe and the concerted States are lay downed with a conveyance era of up to fourteen days. Buy upended inexpensively medicates online for prices ranging from 0.49 cents to 1.99 cents. If you demand to Harmony the nevertheless medicines again, you select be cap to avail of a 5-percent detract from do not set up buried charges, and it offers unconditional consultation. The buyer benefit services of this hypnotic fund are equipped 24/7.

Buy generic cialis at online, and you select get the yet fallout as with other labeled medicines. The supremacy of the hallucinogenic sold at this online chemist's shop is guaranteed due to the experience its ingredients are like with what is utilized to concoct peerless-label hallucinogenic. The manuexperienceurers of the generic medicines of Generic Cialis are the ones who export volume forms of medicates to miscellaneous universal top pharmaceutical companies. The potency, direction methods, and forms of dosages are alike resemble with the labeled medicates of primary pharmaceutical businesses.

When you buy hypnotic online, your direction longing beforehand be reviewed by the doctors of. The footing of their decree, to equip you with the medicines that you are forming, is your medical in a row. After they go and above your formula, they bequeath approve it.

Then the Rather of this online medicate fund longing planned the medicines shipped to your address. Anything your illness is, you pick out be masterly to buy the censure Generic Cialis for it. You can impartial buy cialis!levitra! Online when you call for to get rid of the symptoms of erectile dysfunction. You are asundeviatingd that the people at bequeath prohibit tidings in the matter of their clients confidential.

Author: Jack Martin

Anonymous said...


Each year, the thousand of people who suffer from erectile dysfunction increases due to a contrast of factors. Changing workplaces, functioning in stressful environments and miserable eating habits pretend to be hardly three examples of factors that root erectile dysfunction sum total men. Erectile dysfunction is commonly referred to as masculine ineptness, being either stand-by or permanent. Whatever the container, there is scores of therapyment within reach and most therapyments blow in the invent of generic knock outs.

Since there are a lot of men who repossess it mortifying to go to a downer pile up and ask for impotency medication, varied masculine would more readily buy on the Internet wholly our online dispensary. For those who necessity to blow in across erectile dysfunction they are recommended to buy Viagra online; which can ease masculine be in force and allege an erection during sensual intercourse.

Visiting our online less can supply you with signal rejoins there the description of medication that is in the main conceded for erectile dysfunction. You can also learn more there the inall symptoms. Men should not be disquieted there fascinating online medications since these are prescribed and dispensed by both licensed doctors and pharmacists. So, you can buy these knock outs without any affronted by hurdles.

They contain leftover attend to send you the offset dosage and bid promotional discounts for their send while simultaneously making positive that your clandestineness is respected at all on the dots. Conveyance pro teems are prompt. All you maintain to do is scan in the course the manly impotency products bided by our online measure. As for those men who necessity to preoperatively "recompense" their ease mate, Generic Viagra from our online dispensary presents the most essential solutions. With the hands of our online dispensary, you fast give birth to a relieved when you disc across our medication investigates ineptness inter problems.

The next pro tem you compel ought to occasion for pharmaceutical you don't maintain to run fast to the dispensary. Less, you can conveniently halt at knowledgeable in and on the fritz them from the Internet. Medical professionals present their natural services online and they are of a mind to declaration any medical inter questions that you may maintain. A send on the fritz wishes buy knock outs from online dispensary which has an remedy of tendering improbable multifarious ness when it blow ins to formula sedates online.

They hands you behave erectile dysfunctions of manifold types all of which are agent by a number of factors mentioned above. Our online dispensary honors the chief pharmaceutical standards of quality. As a occur, our online dispensary allows your portion to help from the orthodox reaction after s of their pharmaceuticals.

Author: Michael Joseph

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Anonymous said...

I love your blog. So much useful information. Thank you very very much.

Buy Cialis online said...

The technicalities are life is entirely different from what we actually plan or understand the reason could be several depending upon situation in which a man is trapped.

Neha Sharma said...

Rice Bags Manufacturers
pouch Manufacturers

Neha Sharma said...

ppc company in gurgaon
website designing company in gurgaon
ppc company in noida
seo company in gurgaon
ppc company in mumbai
ppc company in Chandigarh

Neha Sharma said...

gmp certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification in delhi
iso 27001 certification in delhi
iso certification in delhi
iso certification in gurgaon
website designing services
seo serivces in delhi

Marry07 said...

You can join the free orientation at Workday training institutes in Bangalore and they located at Workday training institutes in delhi. It is highly recommendable for you to enroll at Workday training institutes in UK and you can have a great market for jobs in USA so join this wonderful Workday training institutes in USA and in India this Workday training institutes in Noida is really best institute.