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.
Here is my test:
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
And here is the fixture:
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?
And my model:
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
An easy way to find out why
savefails is to callsave!instead. The exception will give you enough information.Another way is to call
valid?and then putserrors.