This is a simple example of strong parameters in Rails (Rails 4 or Rails 3.2 with the strong_parameters gem). The test demonstrates that the Account's name attribute can be updated via the update action, but its balance cannot. The system works.
However, how far should we take these kinds of tests? Now that we've moved this responsibility from the model to the controller, should we invest in controller specs/tests to:
- ensure that all permitted attributes can be updated?
- ensure that any forbidden attributes cannot be updated?
Is there a good, clean, expressive way to test the private account_params method in the controller, to verify the contents of the whitelist rather than the effects of the whitelist? I've used shoulda-style "should(_not) allow_mass_assignment_of" macros in the past. There doesn't appear to be anything analogous for strong_parameters yet.
Disclaimer: I haven't played with Rails 4 for or the strong_parameters gem; I'm inferring how it works from your example.
I think you're testing the functionality of a railsism, when what is more desirable is to test that your application properly implements it.
An older, analogous example:
Do you write tests to confirm that Friend objects get created with the correct foreign key when attached to a user? I don't because I consider this the job of ActiveRecord and I know that functionality is well-tested there. Instead, I use mocks and stubs to verify that I've implemented the features I want to implement. If my goal is to prevent that line from accidentally getting deleted, I might use:
In your example, I might try this: