RSpec: common examples for validators?

I have several models with start_time

and end_time

, and I have a special validator that has special rules for these fields. For testing purposes, I feel like I have 3 options:

  • You have validator and validator_spec . Then retest the entire validator implementation in each model to ensure that the validator works with that model.
  • You have validator and validator_spec . In each model, somehow check that an already checked validator is included in the model. (Perhaps this means testing a single condition that will only arise from an included validator.)
  • Create a generic example as a validation test and include it in every test of the model ( it_behaves_like SomeValidator

    looks weird though )

Any thoughts? The validator has multiple conditions, so I would find taxes, not DRY, to implement # 1.

+3


source to share


2 answers


I suggest another option:

4.) implement validator_spec and build a custom connector that can be reused in every model using this validator



maybe you can find inspiration at https://github.com/thoughtbot/shoulda-matchers

0


source


The "mockist" approach would be to write a specification for the validator class so that it can be tested in isolation. Then in your model specifications, you can stub the validator to return a predefined value.



There is a compromise . You avoid multiplying the number of tests in a combinational manner, so your test suite will be smaller, faster, and easier to maintain. But you pose a small risk that in some situations the model might not work when combined with a real validator after being integrated into production code.

0


source







All Articles