Reputation: 14275
I can't for the life of me figure out what's going wrong here. This is the situation:
Here is my failing test (the count is not increased by one):
test "should create document" do
assert_difference('Document.count') do
post :create, document: { pdf: fixture_file_upload("../files/document_test_file.pdf"), language: @document.language, published_on: @document.published_on, tags: @document.tags, title: @document.title, user_id: @user }
end
assert_redirected_to document_path(assigns(:document))
end
This is my validation in the document model:
def document_is_a_pdf
if !self.pdf.content_type.match(/pdf/)
errors.add(:pdf, "must be a pdf file")
false
end
end
If I do not call that validation in the model, the test runs fine. What am I doing wrong here?
Upvotes: 0
Views: 361
Reputation: 31
I know this is an old question but if anyone still needs help,
From http://apidock.com/rails/ActionController/TestProcess/fixture_file_upload
fixture_file_upload(path, mime_type = nil, binary = false)
Fixture file upload method by default will set the mime type as nil so simply changing the mime type as below will correct this
fixture_file_upload("../files/document_test_file.pdf", 'application/pdf')
Upvotes: 3
Reputation: 14275
I found the problem. Somehow, while testing, the content type could not be determined. That is why the validation of the content type failed and the test did not pass.
I added the content type to the accessible attributes inside my document model and inserted the content type in the test (second attribute inside the document hash):
test "should create document" do
assert_difference('Document.count') do
post :create, document: { pdf: fixture_file_upload("../files/document_test_file.pdf"), pdf_content_type: "application/pdf", language: @document.language, published_on: @document.published_on, tags: @document.tags, title: @document.title, user_id: @user }
end
assert_redirected_to document_path(assigns(:document))
end
Upvotes: 0