Sha256: 1752b9dd729ffce9326dc501cbfa65a911a9c996c93eaecfcd65f93b9e7a02b2

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

require 'test_helper'

class MailEngine::MailScheduleTest < ActiveSupport::TestCase

  context "schedule" do
    setup do
      @user = FactoryGirl.create(:user)
      @schedule = FactoryGirl.create(:mail_schedule, :period => "once")
    end

    should "have logs" do
      @schedule.send_test_mail_to!("hlxwell@gmail.com", @user.id)
      assert_equal 1, @schedule.logs.count
    end

    should "be able to load all payload" do
      assert_equal @schedule.load_payload(@user), {"lastname" => "He", "firstname" => "Michael"}
    end

    should "check email when send_test_mail_to" do
      assert_raise RuntimeError do
        @schedule.send_test_mail_to!("hlxwell", @user.id)
      end
    end

    should "be able to send_test_mail_to" do
      @schedule.send_test_mail_to!("hlxwell@gmail.com", @user.id)
      assert_equal 1, MailEngine::MailLog.count
    end

    should "check sample user when send_test_mail_to" do
      assert_raise ActiveRecord::RecordNotFound do
        @schedule.send_test_mail_to!("hlxwell@gmail.com", 0)
      end
    end
  end

  context "once sending only schedule" do
    setup do
      clear_database
      @schedule = FactoryGirl.create(:mail_schedule, :period => "once")
    end

    should "send mail only once" do
      assert @schedule.ready_to_send?
      @schedule.sendmail
      assert_equal 1, @schedule.sent_count
      # should not be able to send anyway
      assert !@schedule.ready_to_send?
      Timecop.travel(1.year.since)
      assert !@schedule.ready_to_send?
    end
  end

  context "daily schedule" do
    setup do
      clear_database
      @schedule = FactoryGirl.create(:mail_schedule, :period => "daily")
    end

    should "send mail every day at the same time to first_send_at" do
      assert @schedule.ready_to_send?
      @schedule.sendmail
      assert !@schedule.ready_to_send?
      Timecop.travel(1.day.since)

      assert @schedule.ready_to_send?
      @schedule.sendmail
      assert !@schedule.ready_to_send?
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mail_engine-0.1.2 test/unit/mail_engine/mail_schedule_test.rb
mail_engine-0.1.1 test/unit/mail_engine/mail_schedule_test.rb
mail_engine-0.1.0 test/unit/mail_engine/mail_schedule_test.rb