diff options
author | Abdelkader Boudih <terminale@gmail.com> | 2014-08-26 20:08:49 +0000 |
---|---|---|
committer | Abdelkader Boudih <terminale@gmail.com> | 2014-09-02 07:50:16 +0000 |
commit | eb4245dd17ba66c0551f3d4ed841b471bcf01b91 (patch) | |
tree | b79b4091e2a92d84bdbb042d67160f0b0e51b453 /activejob/test | |
parent | 97e689a0d76daf9c208f96832b6d06db9491d569 (diff) | |
download | rails-eb4245dd17ba66c0551f3d4ed841b471bcf01b91.tar.gz rails-eb4245dd17ba66c0551f3d4ed841b471bcf01b91.tar.bz2 rails-eb4245dd17ba66c0551f3d4ed841b471bcf01b91.zip |
[ActiveJob] TestCase
Diffstat (limited to 'activejob/test')
-rw-r--r-- | activejob/test/cases/test_helper_test.rb | 217 | ||||
-rw-r--r-- | activejob/test/cases/test_test.rb | 43 |
2 files changed, 260 insertions, 0 deletions
diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb new file mode 100644 index 0000000000..b98c8e01a5 --- /dev/null +++ b/activejob/test/cases/test_helper_test.rb @@ -0,0 +1,217 @@ +# encoding: utf-8 +require 'helper' +require 'active_support/core_ext/time' +require 'active_support/core_ext/date' +require 'jobs/hello_job' +require 'jobs/logging_job' +require 'jobs/nested_job' + +class EnqueuedJobsTest < ActiveJob::TestCase + tests HelloJob + setup :perform_enqueued_at_jobs + + def test_assert_enqueued_jobs + assert_nothing_raised do + assert_enqueued_jobs 1 do + HelloJob.enqueue('david') + end + end + end + + def test_repeated_enqueued_jobs_calls + assert_nothing_raised do + assert_enqueued_jobs 1 do + HelloJob.enqueue('abdelkader') + end + end + + assert_nothing_raised do + assert_enqueued_jobs 2 do + HelloJob.enqueue('sean') + HelloJob.enqueue('yves') + end + end + end + + def test_assert_enqueued_jobs_with_no_block + assert_nothing_raised do + HelloJob.enqueue('rafael') + assert_enqueued_jobs 1 + end + + assert_nothing_raised do + HelloJob.enqueue('aaron') + HelloJob.enqueue('matthew') + assert_enqueued_jobs 3 + end + end + + def test_assert_no_enqueued_jobs + assert_nothing_raised do + assert_no_enqueued_jobs do + # Scheduled jobs are being performed in this context + HelloJob.enqueue_at(Date.tomorrow.noon, 'godfrey') + end + end + end + + def test_assert_enqueued_jobs_too_few_sent + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_enqueued_jobs 2 do + HelloJob.enqueue('xavier') + end + end + + assert_match(/2 .* but 1/, error.message) + end + + def test_assert_enqueued_jobs_too_many_sent + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_enqueued_jobs 1 do + HelloJob.enqueue('cristian') + HelloJob.enqueue('guillermo') + end + end + + assert_match(/1 .* but 2/, error.message) + end + # + def test_assert_no_enqueued_jobs_failure + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_no_enqueued_jobs do + HelloJob.enqueue('jeremy') + end + end + + assert_match(/0 .* but 1/, error.message) + end + + def test_assert_enqueued_job + assert_enqueued_job(job: LoggingJob, queue: 'default') do + NestedJob.enqueue_at(Date.tomorrow.noon) + end + end + + def test_assert_enqueued_job_failure + assert_raise ActiveSupport::TestCase::Assertion do + assert_enqueued_job(job: LoggingJob, queue: 'default') do + NestedJob.enqueue + end + end + + assert_raise ActiveSupport::TestCase::Assertion do + assert_enqueued_job(job: NestedJob, queue: 'low') do + NestedJob.enqueue + end + end + end + + def test_assert_enqueued_job_args + assert_raise ArgumentError do + assert_enqueued_job(class: LoggingJob) do + NestedJob.enqueue_at(Date.tomorrow.noon) + end + end + end +end + +class PerformedJobsTest < ActiveJob::TestCase + tests HelloJob + setup :perform_enqueued_jobs + + def test_assert_performed_jobs + assert_nothing_raised do + assert_performed_jobs 1 do + HelloJob.enqueue('david') + end + end + end + + def test_repeated_performed_jobs_calls + assert_nothing_raised do + assert_performed_jobs 1 do + HelloJob.enqueue('abdelkader') + end + end + + assert_nothing_raised do + assert_performed_jobs 2 do + HelloJob.enqueue('sean') + HelloJob.enqueue('yves') + end + end + end + + def test_assert_performed_jobs_with_no_block + assert_nothing_raised do + HelloJob.enqueue('rafael') + assert_performed_jobs 1 + end + + assert_nothing_raised do + HelloJob.enqueue('aaron') + HelloJob.enqueue('matthew') + assert_performed_jobs 3 + end + end + + def test_assert_no_performed_jobs + assert_nothing_raised do + assert_no_performed_jobs do + # Scheduled jobs are being enqueued in this context + HelloJob.enqueue_at(Date.tomorrow.noon, 'godfrey') + end + end + end + + def test_assert_performed_jobs_too_few_sent + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 2 do + HelloJob.enqueue('xavier') + end + end + + assert_match(/2 .* but 1/, error.message) + end + + def test_assert_performed_jobs_too_many_sent + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 1 do + HelloJob.enqueue('cristian') + HelloJob.enqueue('guillermo') + end + end + + assert_match(/1 .* but 2/, error.message) + end + # + def test_assert_no_performed_jobs_failure + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_no_performed_jobs do + HelloJob.enqueue('jeremy') + end + end + + assert_match(/0 .* but 1/, error.message) + end + + def test_assert_performed_job + assert_performed_job(job: NestedJob, queue: 'default') do + NestedJob.enqueue + end + end + + def test_assert_performed_job_failure + assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_job(job: LoggingJob, queue: 'default') do + NestedJob.enqueue_at(Date.tomorrow.noon) + end + end + + assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_job(job: NestedJob, queue: 'low') do + NestedJob.enqueue_at(Date.tomorrow.noon) + end + end + end +end diff --git a/activejob/test/cases/test_test.rb b/activejob/test/cases/test_test.rb new file mode 100644 index 0000000000..a4d786f3da --- /dev/null +++ b/activejob/test/cases/test_test.rb @@ -0,0 +1,43 @@ +# encoding: utf-8 +require 'helper' +require 'jobs/hello_job' +require 'jobs/logging_job' +require 'jobs/nested_job' + +class ActiveJobTestCaseTest < ActiveJob::TestCase + tests HelloJob + + def test_set_job_class_manual + assert_equal HelloJob, self.class.job_class + end +end + +class CrazySymbolNameJobTest < ActiveJob::TestCase + tests :hello_job + + def test_set_job_class_manual_using_symbol + assert_equal HelloJob, self.class.job_class + end +end + +class CrazyStringNameJobTest < ActiveJob::TestCase + tests 'hello_job' + + def test_set_job_class_manual_using_string + assert_equal HelloJob, self.class.job_class + end +end + +class HelloJobTest < ActiveJob::TestCase + def test_set_job_class_manual + assert_equal HelloJob, self.class.job_class + end +end + +class CrazyNameJobTest < ActiveJob::TestCase + def test_determine_default_job_raises_correct_error + assert_raise(ActiveJob::NonInferrableJobError) do + self.class.job_class + end + end +end |