Testing nested attributes with RSpec

So I came across a situation today where I needed to have a particular model accept nested attributes for a related model – much like is demonstrated in the post on integrating advanced forms with Cocoon. Unfortunately, much to my dismay, RSpec doesn’t have any fancy convenience method for testing this. Intarwebz to the rescue!

I found this great little script for doing simple testing for nested attributes, including rejection and acceptance criteria. Just place the code from this gist in a file in your spec/support folder and you’ll magically gain access to a new should matcher – accept_nested_attributes_for.

it { should accept_nested_attributes_for(:association_name) }