Testing wrap_parameters in a Rails Controller

Currently, in Rails 3.1 the wrap_parameters controller method works great, but is untestable. Controller specs do not run the wrap_parameter initializer packaged with Rails. Adding a wrap_parameters call to the controller also does not work when running controller specs.

I needed to change the default Rails behavior of wrap_parameters and use its :include functionality. To solve the testability issue I defined attribute_names on the model. attribute_names returns the database fields associated with the model and is used by wrap_parameters to determine which incoming request parameters to wrap.

Overriding attribute_names provides close to the same functionality of :include. This both worked and was easily tested through the model. Overriding attribute_names also had the added benefit being able to add new attributes onto the existing model’s attributes. :include is not additive and requires you to list all attributes you want to wrap, even those already wrapped by default.