File: sweeper_test.rb

package info (click to toggle)
ruby-rails-observers 0.1.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 272 kB
  • sloc: ruby: 1,522; makefile: 2
file content (83 lines) | stat: -rw-r--r-- 2,118 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'minitest/autorun'
require 'action_controller'
require 'active_record'
require 'rails/observers/activerecord/active_record'
require 'rails/observers/action_controller/caching'

SharedTestRoutes = ActionDispatch::Routing::RouteSet.new

class AppSweeper < ActionController::Caching::Sweeper; end

class SweeperTestController < ActionController::Base
  include SharedTestRoutes.url_helpers

  cache_sweeper :app_sweeper

  def show
    render text: 'hello world'
  end

  def error
    raise StandardError.new
  end
end

class SweeperTest < ActionController::TestCase
  def setup
    @routes = SharedTestRoutes

    @routes.draw do
      get ':controller(/:action)'
    end

    super
  end

  def test_sweeper_should_not_ignore_no_method_error
    sweeper = ActionController::Caching::Sweeper.send(:new)
    assert_raise NoMethodError do
      sweeper.send_not_defined
    end
  end

  def test_sweeper_should_not_block_rendering
    response = test_process(SweeperTestController)
    assert_equal 'hello world', response.body
  end

  def test_sweeper_should_clean_up_if_exception_is_raised
    assert_raise StandardError do
      test_process(SweeperTestController, 'error')
    end
    assert_nil AppSweeper.instance.controller
  end

  def test_before_method_of_sweeper_should_always_return_true
    sweeper = ActionController::Caching::Sweeper.send(:new)
    assert sweeper.before(SweeperTestController.new)
  end

  def test_after_method_of_sweeper_should_always_return_nil
    sweeper = ActionController::Caching::Sweeper.send(:new)
    assert_nil sweeper.after(SweeperTestController.new)
  end

  def test_sweeper_should_not_ignore_unknown_method_calls
    sweeper = ActionController::Caching::Sweeper.send(:new)
    assert_raise NameError do
      sweeper.instance_eval do
        some_method_that_doesnt_exist
      end
    end
  end

  private

  def test_process(controller, action = "show")
    @controller = controller.is_a?(Class) ? controller.new : controller
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new

    process(action)
  end
end