An easy way to find out why save
fails is to call save!
instead. The exception will give you enough information.
Another way is to call valid?
and then puts errors
.
Question
I have written a unit test whose assertion fails - I would like to find out why, without having to inspect every attribute of the model.
test "a correct document should be saved" do
document = documents(:one)
document.pdf = File.new("test/files/document_test_file.pdf")
assert document.save
end
one:
user_id: 1
title: MyString
published_on: 2012-12-06
tags: MyText
language: MyString
Apparently, something is missing and the document cannot be saved. But how do I find out what exactly is missing? Is there any advanced assertion method to do this?
class Document < ActiveRecord::Base
attr_accessible :language, :pdf, :pdf_file_name, :published_on, :tags, :title, :user_id
# Validations
validates_presence_of :language, :published_on, :tags, :title, :user
validates_date :published_on, on_or_before: Date.current,
on_or_before_message: "must be today or earlier"
validates :user_id, numericality: { only_integer: true, greater_than: 0 }
validates :pdf, attachment_presence: true
# Relations
belongs_to :user
# Paperclip
has_attached_file :pdf
end
Solution
An easy way to find out why save
fails is to call save!
instead. The exception will give you enough information.
Another way is to call valid?
and then puts errors
.
OTHER TIPS
How about:
assert document.save, document.errors.full_messages.join(" ")