flash_test.rb 4.3 KB
Newer Older
1
require 'abstract_unit'
D
Initial  
David Heinemeier Hansson 已提交
2 3 4 5 6

class FlashTest < Test::Unit::TestCase
  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 76
  end

  def setup
D
David Heinemeier Hansson 已提交
77 78 79
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @controller = TestController.new
D
Initial  
David Heinemeier Hansson 已提交
80 81 82
  end

  def test_flash
D
David Heinemeier Hansson 已提交
83
    get :set_flash
D
Initial  
David Heinemeier Hansson 已提交
84

D
David Heinemeier Hansson 已提交
85 86 87
    get :use_flash
    assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
    assert_equal "hello", @response.template.assigns["flashy"]
D
Initial  
David Heinemeier Hansson 已提交
88

D
David Heinemeier Hansson 已提交
89 90
    get :use_flash
    assert_nil @response.template.assigns["flash_copy"]["that"], "On second flash"
D
Initial  
David Heinemeier Hansson 已提交
91 92 93
  end

  def test_keep_flash
D
David Heinemeier Hansson 已提交
94
    get :set_flash
D
Initial  
David Heinemeier Hansson 已提交
95
    
96
    get :use_flash_and_keep_it
D
David Heinemeier Hansson 已提交
97 98
    assert_equal "hello", @response.template.assigns["flash_copy"]["that"]
    assert_equal "hello", @response.template.assigns["flashy"]
D
Initial  
David Heinemeier Hansson 已提交
99

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

D
David Heinemeier Hansson 已提交
103 104
    get :use_flash
    assert_nil @response.template.assigns["flash_copy"]["that"], "On third flash"
D
Initial  
David Heinemeier Hansson 已提交
105 106
  end
  
107
  def test_flash_now
D
David Heinemeier Hansson 已提交
108 109 110 111
    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"]
112

D
David Heinemeier Hansson 已提交
113 114 115 116
    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"]
117
  end 
118
  
119 120 121 122 123 124 125 126 127 128
  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
  
129 130 131 132 133 134
  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 
135 136 137 138 139 140 141 142 143 144 145

  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 已提交
146
end