Skip to content

Commit

Permalink
Use factory to generate attribute attributes.
Browse files Browse the repository at this point in the history
I'm planning on adding a mandatory title attribute to attachments. This
commit should make the latter change much less painful.
  • Loading branch information
floehopper committed Jan 25, 2012
1 parent 143e29f commit 33d95d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
6 changes: 3 additions & 3 deletions features/step_definitions/publication_steps.rb
Expand Up @@ -4,17 +4,17 @@
end

Given /^a draft publication "([^"]*)" with a PDF attachment$/ do |title|
attachment = Attachment.new(file: pdf_attachment)
attachment = build(:attachment, file: pdf_attachment)
create(:draft_publication, title: title, attachments: [attachment])
end

Given /^a submitted publication "([^"]*)" with a PDF attachment$/ do |title|
attachment = Attachment.new(file: pdf_attachment)
attachment = build(:attachment, file: pdf_attachment)
create(:submitted_publication, title: title, attachments: [attachment])
end

Given /^a published publication "([^"]*)" with a PDF attachment$/ do |title|
attachment = Attachment.new(file: pdf_attachment)
attachment = build(:attachment, file: pdf_attachment)
create(:published_publication, title: title, attachments: [attachment])
end

Expand Down
41 changes: 30 additions & 11 deletions test/support/admin_document_controller_test_helpers.rb
Expand Up @@ -191,7 +191,9 @@ def should_allow_attachments_for(document_type)
test 'creating a document should attach file' do
greenpaper_pdf = fixture_file_upload('greenpaper.pdf', 'application/pdf')
attributes = controller_attributes_for(document_type)
attributes[:attachments_attributes] = { "0" => { file: greenpaper_pdf } }
attributes[:attachments_attributes] = {
"0" => attributes_for(:attachment, file: greenpaper_pdf)
}

post :create, document: attributes

Expand All @@ -216,7 +218,9 @@ def should_allow_attachments_for(document_type)

post :create, document: controller_attributes_for(document_type,
title: "",
attachments_attributes: { "0" => { file: greenpaper_pdf } }
attachments_attributes: {
"0" => attributes_for(:attachment, file: greenpaper_pdf)
}
)

assert_select "form#document_new" do
Expand All @@ -229,7 +233,9 @@ def should_allow_attachments_for(document_type)

post :create, document: controller_attributes_for(document_type,
title: "",
attachments_attributes: { "0" => { file: greenpaper_pdf } }
attachments_attributes: {
"0" => attributes_for(:attachment, file: greenpaper_pdf)
}
)

assert_select "form#document_new" do
Expand All @@ -241,7 +247,9 @@ def should_allow_attachments_for(document_type)
test 'creating a document with invalid data should not show any attachment info' do
attributes = controller_attributes_for(document_type)
greenpaper_pdf = fixture_file_upload('greenpaper.pdf')
attributes[:attachments_attributes] = { "0" => { file: greenpaper_pdf } }
attributes[:attachments_attributes] = {
"0" => attributes_for(:attachment, file: greenpaper_pdf)
}

post :create, document: attributes.merge(title: '')

Expand All @@ -253,8 +261,8 @@ def should_allow_attachments_for(document_type)
csv_file = fixture_file_upload('sample-from-excel.csv', 'text/csv')
attributes = controller_attributes_for(document_type)
attributes[:attachments_attributes] = {
"0" => { file: greenpaper_pdf },
"1" => { file: csv_file }
"0" => attributes_for(:attachment, file: greenpaper_pdf),
"1" => attributes_for(:attachment, file: csv_file)
}

post :create, document: attributes
Expand Down Expand Up @@ -286,7 +294,9 @@ def should_allow_attachments_for(document_type)
document = create(document_type)

put :update, id: document, document: document.attributes.merge(
attachments_attributes: { "0" => { file: greenpaper_pdf } }
attachments_attributes: {
"0" => attributes_for(:attachment, file: greenpaper_pdf)
}
)

document.reload
Expand All @@ -303,7 +313,10 @@ def should_allow_attachments_for(document_type)
document = create(document_type)

put :update, id: document, document: document.attributes.merge(
attachments_attributes: { "0" => { file: greenpaper_pdf }, "1" => { file: csv_file } }
attachments_attributes: {
"0" => attributes_for(:attachment, file: greenpaper_pdf),
"1" => attributes_for(:attachment, file: csv_file)
}
)

document.reload
Expand Down Expand Up @@ -333,7 +346,9 @@ def should_allow_attachments_for(document_type)

put :update, id: document, document: controller_attributes_for(document_type,
title: "",
attachments_attributes: { "0" => { file: greenpaper_pdf } }
attachments_attributes: {
"0" => attributes_for(:attachment, file: greenpaper_pdf)
}
)

assert_select "form#document_edit" do
Expand All @@ -347,7 +362,9 @@ def should_allow_attachments_for(document_type)

put :update, id: document, document: controller_attributes_for(document_type,
title: "",
attachments_attributes: { "0" => { file: greenpaper_pdf } }
attachments_attributes: {
"0" => attributes_for(:attachment, file: greenpaper_pdf)
}
)

assert_select "form#document_edit" do
Expand Down Expand Up @@ -376,7 +393,9 @@ def should_allow_attachments_for(document_type)

put :update, id: document, document: document.attributes.merge(
lock_version: lock_version,
attachments_attributes: { "0" => { file: greenpaper_pdf } }
attachments_attributes: {
"0" => attributes_for(:attachment, file: greenpaper_pdf)
}
)

assert_select "form#document_edit" do
Expand Down

0 comments on commit 33d95d4

Please sign in to comment.