Rails 4 - 具有嵌套属性的Rspec请求测试帖子

时间:2017-03-16 08:42:46

标签: ruby-on-rails testing rspec

我有一个正常工作的代码,我正在尝试为以下

编写一个rspec请求规范

表格

      <%= 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

请求规范

  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

嵌套属性未保存。是因为我没有 @ student.documents.new在测试中?任何帮助都非常感谢

1 个答案:

答案 0 :(得分:0)

尝试使用FactoryGirl

it "redirects to the home page upon save" do
  post :create, student: Factory.attributes_for(:student)
  expect(student_1.documents.count).to eq(1)
end

关注此link