Ruby's mixin functionality can be very useful. Modules can group together useful features that can be added at runtime to any class. However, given the dynamic nature of Ruby, testing the mixin features of a module can be tricky.
Let's go back to the AddressValidator
module we worked with in previous sections of this chapter and in Chapter 1, Exploring Testability from Unit Tests to Behavior-Driven Development and Chapter 2, Specifying Behavior with Examples and Matchers. We want to make this module into a mixin that we can add to any object with an address
method. Then we can call address_valid?
on the object to trigger all the validations.
Regardless of the complexity of the metaprogramming involved with a mixin, we can still treat it as a normal test case with a known input and expected output. The input, in this case, is not a set of arguments to a method, but a whole class into which we mix in the AddressValidator
module:
describe AddressValidator do...