jeudi 16 mars 2017

Rails 4 - Rspec request test post with nested attributes

I have a working code and I am trying to write a rspec request spec for the following

form

      <%= semantic_form_for @student, :url => student_documents_path(@student), method: :post do |f| %>
        <%= f.fields_for :documents, @student.documents.new do |form| %>
          <%= render 'students/document', :f => form %>
        <% end %>
      <%end%>

Controler

class Students::DocumentsController < ApplicationController
  def create
   @student.update_attributes!(documents_params)
   redirect_to student_path(@student)
  end

end

Request spec

  describe 'POST #create' do
  it 'should be successful' do
    post "/students/#{student_1.id}/documents", student: { :document_entries_attributes => { "0" => {year: 2017, category: 'bla'} } }

    expect(student_1.documents.count).to eq(1)

  end
end

nested attributes does not get saved. is it because I have not @student.documents.new in the test ? any help is highly appreciated

Aucun commentaire:

Enregistrer un commentaire