require File.dirname(__FILE__) + '/../test_helper' class EpisodeTest < Test::Unit::TestCase def test_permalink assert_equal 'buffalo-soldier', create_with_callback(:before_create).permalink assert_equal 'alices-restaurant', create_with_callback(:before_create, :title => "Alice's restaurant").permalink end def test_to_param episode = create_with_callback(:before_create) assert_equal "buffalo-soldier", episode.to_param end def test_should_set_transcript_published_at_on_save e = create_with_callback(:before_save) assert_nil e.transcript_published_at e.has_transcript = true e.send(:before_save) assert_not_nil e.transcript_published_at, "Transcript publish date was not filled in" assert_kind_of Time, e.transcript_published_at end def test_should_set_transcript_published_at_on_create e = create_episode(:has_transcript => true) assert_not_nil e.transcript_published_at, "Transcript publish date was not filled in" assert_kind_of Time, e.transcript_published_at end def test_should_not_reset_transcript_published_if_already_present e = create_episode(:has_transcript => true) assert_not_nil e.transcript_published_at, "Transcript publish date was not filled in" assert_kind_of Time, e.transcript_published_at original_time = e.transcript_published_at e.update_attribute(:title, "Crunchy Bacon") e.reload assert_equal original_time.to_i, e.transcript_published_at.to_i end def test_should_not_recreate_permalink e = create_episode assert_equal "buffalo-soldier", e.permalink e.update_attribute :title, "Buffalo Bill" e.reload assert_equal 'buffalo-soldier', e.permalink end def test_should_not_allow_same_title_twice e = create_episode assert e.valid?, e.errors.full_messages.join e2 = create_episode deny e2.valid?, "E2 was valid but should not be" assert_not_nil e2.errors.on(:title), "E2 had no errors on :title" end private def create_episode(options={}) Episode.create({:title => "Buffalo Soldier"}.merge(options)) end ## # A faster fake create where a specific callback is executed. # # No DB access needed. def create_with_callback(callback_method_name=nil, options={}) episode = Episode.new({:title => "Buffalo Soldier"}.merge(options)) episode.send(callback_method_name) episode end end