Testing Nested Attributes in Rspec/Capy

I have a feature/scenario testing a user filling in a new profile.

so for example:

fill_in 'email', with: user.email

However, now I need to test an accepts nested attributes for situation. So when a user is filling out their profile, they might be adding a certification which has a Certification model. I’m using Ryan Bates’s nested_form gem, and I need to be able to test the equivalent of fill_in ‘certification’, with: ‘Some Cert.’

Here’s some of the relevant information

Certification; belongs_to :user

has_many :certifications, dependent: :destroy, inverse_of: :user
accepts_nested_attributes_for :certifications, allow_destroy: true,
    reject_if: proc { |attributes| attributes['name'].blank? }

= f.fields_for :certifications do |cert_form|
  = cert_form.text_field :name, placeholder: "enter certification or license"
  = cert_form.text_field :authority, placeholder: "issuing authority"
  = cert_form.text_field :number, id: "cert_number", placeholder: "number"

@briandear I think you do something like:

within("form.certifications") do
  fill_in :placeholder, with: 'license'
  fill_in :authority, with: 'issued by'

I use simple form and it creates a special class name for the form; you’d have to look up what your identifying class is.

1 Like