I want to ensure the publish date is set for model when the user decides to publish an article.
I have this:
class Article < ApplicationRecord
before_validation :check_published
validates :publish_date, presence: true, if: :article_published?
def check_published
self.publish_date = Time.now if self.published
end
def article_published?
self.published
end
end
In my Article model test file:
require 'test_helper'
class ArticleTest < ActiveSupport::TestCase
def setup
@new_article = {
title: "Car Parks",
description: "Build new car parks",
published: true
}
end
test "Article Model: newly created article with published true should have publish date" do
article = Article.new(@new_article)
puts "article title: #{article.title}"
puts "article published: #{article.published}"
puts "article publish date: #{article.publish_date}"
assert article.publish_date != nil
end
end
The test fails.
Is what I am doing possible or do I need to do it in the controller instead?