Testing Shared Modules and ActiveSupport Concerns
Learn about shared behavior, testing shared modules, shared directory, shared examples, invoking shared behavior, and ActiveSupport concerns.
Shared behavior
Often multiple models in the application share some common feature set.
For example, we may have multiple object types that can be:
- Purchased
- Tagged
- Commented on
We can use standard Ruby classes and modules for this shared behavior. If the shared behavior has both class and instance methods, Rails provides ActiveSupport::Concern
, which allows us to easily use a common pattern to mix multiple kinds of behavior from one module.
Testing shared behavior
Testing this shared behavior can be a challenge. We don’t want to rewrite the shared behavior specs for each class that shares the mixed-in module. Simultaneously, if the shared feature depends on data being available in each class, that dependency is ...