Testing Shared Modules and ActiveSupport Concerns

Often you’ll have multiple models in your application that share some kind of common feature set. For example, you may have multiple object types that can be purchased, tagged, or commented on. You can use standard Ruby modules for this shared behavior. If the shared behavior has both class and instance methods, Rails provides ActiveSupport::Concern, which allows you to easily use a common pattern to mix multiple kinds of behavior from one module.

Testing this shared behavior can be a challenge. You don’t want to have to rewrite the shared behavior specs for each class that shares the mixed-in module. At the same time, if the shared feature depends on data being available in each class, that ...

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.