user419017
user419017

Reputation:

Rspec - dynamic tests, good or bad idea?

The idea arisen from factory_girl test all factories approach. It got me thinking, why not do something similar for models, controllers, and requests?

From what I know, any new ActiveRecord model should be invalid. And if not, then you haven't created the relevant validations. From a TDD approach this might not be a problem, because you are dealing with your assertions first. But if you're inheriting another project which doesn't have proper tests, it would invaluable if you could test each model/view/controller with some [very] general assertions.

For models, I would assume model.new.should be_invalid is a good start.

Any thoughts on this? Is this a good/bad idea?

Upvotes: 0

Views: 154

Answers (1)

Winfield
Winfield

Reputation: 19145

I discourage this kind of broad testing, because you're going to spend a lot of time running tests with inherently low value.

Your tests should be defining the behavior/requirements of your system. Asserting that all models have one or more required values isn't a useful test when it's exercised on every model.

If you have genuine cross-cutting concerns, you should extract that logic into a mixin with it's own testing that isolates and defines the behavior. Then you can mix that into all of your models without having to test each one.

Most engineers agree the cost of feature code should be weighed against business/behavior value, but tests should as well. These kinds of tests are inherently low-value, but have cost in run-time, maintenance, and false positives.

Upvotes: 1

Related Questions