Topic: Meta: How to test which layer

Check out my Blogpost about How to build testable Ruby Applications. Feedback very welcome!

Last edited by wintersolutions.de (2013-05-03 07:40:42)

Re: Meta: How to test which layer

The article is a bit short: it'd be great if you provided some examples and detailed explanations about why, in your opinion, a given approach is bad. Just saying that "models are tested in integration tests" is not enough: almost everything (except for services not accessible via the Web UI, e.g. background tasks) is tested in integration tests, but this is not a good reason to rely exclusively on them.

I'm quite new to RoR (and, to some extent, even to TDD), so I might be wrong, but I unit-test my models in order to catch logical errors at a lower level, making it easier to debug my application. The same goes for other pieces of app-specific logic.

I don't test controllers not only because it's done in integration tests, but also because, as you've noted, it's a lot of pain which doesn't give you many benefits. Also, while models and other services are usually indipendent from the Web UI, the only job of controllers is to provide the HTTP client with the proper response, and that's already extensively covered by integration tests.

By the way, {rails}strap is quite cool! big_smile

Last edited by alessandro1997 (2013-04-29 15:08:37)

Re: Meta: How to test which layer

Thanks for your valuable feedback Alessandro!
You highlighted one point where I didn't elaborate enough on the concept. One of the main purposes of my article is highlight that you should not unit-test your models. Because if you are unit-testing models that means that you have application logic in them, right? I'll work on the "Application Logic" paragraph a little bit more to make this absolutely clear and I'll provide a example.

What I don't fully understand is your approach to controller testing. You make it sound like you are only testing them (implicitly) trough acceptance/feature tests? This could get you in trouble, especially if you only test the happy path in acceptance/feature tests, which is a good pratice. I think about writing about that more too.

Re: Meta: How to test which layer

Hi,

actually I've just began developing with RoR so I said how I would test, but yes, I'm not sure testing my controllers is worth the effort. I've read some articles and controller unit-tests seem to be a bit... confused. For example, they ensure that a specific instance variable (e.g. the one containing the record) is set... doesn't that break if I decide to refactor my code? Isn't that testing how my class works rather than whether it works?

Perhaps I haven't really understood the purpose of controller testing here. I'd be happy if someone could enlighten me. wink

Re: Meta: How to test which layer

alessandro1997 wrote:

I've read some articles and controller unit-tests seem to be a bit... confused. For example, they ensure that a specific instance variable (e.g. the one containing the record) is set... doesn't that break if I decide to refactor my code? Isn't that testing how my class works rather than whether it works?

I tend to say that this is a trade-off the Rails architects made between ease of use and proper design. If you are coming from a static typed OO language background (Java etc.), it must be obvious to you that this breaks some established standards (encapsulation). But for the way Rails is designed those tests are necessary, because you use this instance variable in your views!

Re: Meta: How to test which layer

I've done some Java and loved it, but I was mainly a PHP (Symfony) developer before realizing what a terrible language it is. Anyway, thanks for the tips. I'll look into the topic more accurately and I'll buy some books.

I'll let you know if I have any more questions. wink