flash_test.rb 4.1 KB
Newer Older
1
require 'abstract_unit'
D
Initial  
David Heinemeier Hansson 已提交
2

3
class FlashTest < ActionController::TestCase
D
Initial  
David Heinemeier Hansson 已提交
4 5 6
  class TestController < ActionController::Base
    def set_flash
      flash["that"] = "hello"
7
      render :inline => "hello"
D
Initial  
David Heinemeier Hansson 已提交
8 9
    end

10 11
    def set_flash_now
      flash.now["that"] = "hello"
12 13 14
      flash.now["foo"] ||= "bar"
      flash.now["foo"] ||= "err"
      @flashy = flash.now["that"]
15
      @flash_copy = {}.update flash
16
      render :inline => "hello"
17 18 19 20 21
    end

    def attempt_to_use_flash_now
      @flash_copy = {}.update flash
      @flashy = flash["that"]
22
      render :inline => "hello"
23 24
    end

D
Initial  
David Heinemeier Hansson 已提交
25
    def use_flash
26
      @flash_copy = {}.update flash
D
Initial  
David Heinemeier Hansson 已提交
27
      @flashy = flash["that"]
28
      render :inline => "hello"
D
Initial  
David Heinemeier Hansson 已提交
29 30 31
    end

    def use_flash_and_keep_it
32
      @flash_copy = {}.update flash
D
Initial  
David Heinemeier Hansson 已提交
33
      @flashy = flash["that"]
34
      flash.keep
35
      render :inline => "hello"
D
Initial  
David Heinemeier Hansson 已提交
36
    end
37 38 39 40 41 42
    
    def use_flash_and_update_it
      flash.update("this" => "hello again")
      @flash_copy = {}.update flash
      render :inline => "hello"
    end
D
Initial  
David Heinemeier Hansson 已提交
43

44 45 46 47 48 49 50 51 52 53
    def use_flash_after_reset_session
      flash["that"] = "hello"
      @flashy_that = flash["that"]
      reset_session
      @flashy_that_reset = flash["that"]
      flash["this"] = "good-bye"
      @flashy_this = flash["this"]
      render :inline => "hello"
    end

D
Initial  
David Heinemeier Hansson 已提交
54
    def rescue_action(e)
55
      raise unless ActionView::MissingTemplate === e
D
Initial  
David Heinemeier Hansson 已提交
56
    end
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    # methods for test_sweep_after_halted_filter_chain
    before_filter :halt_and_redir, :only => "filter_halting_action"

    def std_action
      @flash_copy = {}.update(flash)
    end

    def filter_halting_action
      @flash_copy = {}.update(flash)
    end

    def halt_and_redir
      flash["foo"] = "bar"
      redirect_to :action => "std_action"
      @flash_copy = {}.update(flash)
    end
D
Initial  
David Heinemeier Hansson 已提交
74 75
  end

76
  tests TestController
D
Initial  
David Heinemeier Hansson 已提交
77 78

  def test_flash
D
David Heinemeier Hansson 已提交
79
    get :set_flash
D
Initial  
David Heinemeier Hansson 已提交
80

D
David Heinemeier Hansson 已提交
81 82 83
    get :use_flash
    assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
    assert_equal "hello", @response.template.assigns["flashy"]
D
Initial  
David Heinemeier Hansson 已提交
84

D
David Heinemeier Hansson 已提交
85 86
    get :use_flash
    assert_nil @response.template.assigns["flash_copy"]["that"], "On second flash"
D
Initial  
David Heinemeier Hansson 已提交
87 88 89
  end

  def test_keep_flash
D
David Heinemeier Hansson 已提交
90
    get :set_flash
D
Initial  
David Heinemeier Hansson 已提交
91
    
92
    get :use_flash_and_keep_it
D
David Heinemeier Hansson 已提交
93 94
    assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
    assert_equal "hello", @response.template.assigns["flashy"]
D
Initial  
David Heinemeier Hansson 已提交
95

D
David Heinemeier Hansson 已提交
96 97
    get :use_flash
    assert_equal "hello", @response.template.assigns["flash_copy"]["that"], "On second flash"
D
Initial  
David Heinemeier Hansson 已提交
98

D
David Heinemeier Hansson 已提交
99 100
    get :use_flash
    assert_nil @response.template.assigns["flash_copy"]["that"], "On third flash"
D
Initial  
David Heinemeier Hansson 已提交
101 102
  end
  
103
  def test_flash_now
D
David Heinemeier Hansson 已提交
104 105 106 107
    get :set_flash_now
    assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
    assert_equal "bar"  , @response.template.assigns["flash_copy"]["foo"]
    assert_equal "hello", @response.template.assigns["flashy"]
108

D
David Heinemeier Hansson 已提交
109 110 111 112
    get :attempt_to_use_flash_now
    assert_nil @response.template.assigns["flash_copy"]["that"]
    assert_nil @response.template.assigns["flash_copy"]["foo"]
    assert_nil @response.template.assigns["flashy"]
113
  end 
114
  
115 116 117 118 119 120 121 122 123 124
  def test_update_flash
    get :set_flash
    get :use_flash_and_update_it
    assert_equal "hello",       @response.template.assigns["flash_copy"]["that"]
    assert_equal "hello again", @response.template.assigns["flash_copy"]["this"]
    get :use_flash
    assert_nil                  @response.template.assigns["flash_copy"]["that"], "On second flash"
    assert_equal "hello again", @response.template.assigns["flash_copy"]["this"], "On second flash"
  end
  
125 126 127 128 129 130
  def test_flash_after_reset_session
    get :use_flash_after_reset_session
    assert_equal "hello",    @response.template.assigns["flashy_that"]
    assert_equal "good-bye", @response.template.assigns["flashy_this"]
    assert_nil   @response.template.assigns["flashy_that_reset"]
  end 
131 132 133 134 135 136 137 138 139 140 141

  def test_sweep_after_halted_filter_chain
    get :std_action
    assert_nil @response.template.assigns["flash_copy"]["foo"]
    get :filter_halting_action
    assert_equal "bar", @response.template.assigns["flash_copy"]["foo"]
    get :std_action # follow redirection
    assert_equal "bar", @response.template.assigns["flash_copy"]["foo"]
    get :std_action
    assert_nil @response.template.assigns["flash_copy"]["foo"]
  end
J
Jeremy Kemper 已提交
142
end