Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Initially I thought this was simple: unit testing for models with other testing such as integration for controller and browser testing for views.

But more recently I've seen a lot of references to unit testing that doesn't seem to exactly follow this format.

Is it possible to have a unit test of a controller? Does that mean that just one method is called? What's the distinction? What does unit testing really means in my rails world?

share|improve this question
add comment

closed as not a real question by gnat, Jim G., StuperUser, ChrisF Oct 20 '12 at 18:55

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

Unit testing, as its name implies, is testing of individual software "units," most commonly methods. If you can fabricate a test that calls a single method and returns a predictable result (without involving loads of other code in other modules, for which the term "integration test" is more appropriate), it is a unit test.

In the case of an MVC controller, typically that method gets called with zero or more parameters, and returns some kind of object (in ASP.NET MVC, it returns a view). So if you have some correct result object that you can compare to your return object, you have a unit test.

In practice, controllers are not typically tested this way. Instead, program logic is pushed out of the controllers into a business logic layer or into the model. The controllers are then made as thin as possible, so that they are merely calling other methods that are already tested elsewhere.

Controllers should really be regarded as a "patch bay," receiving incoming POST and GET requests, and "routing" them to the appropriate program logic for processing.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.