test_process.rb 1.3 KB
Newer Older
1
require 'action_dispatch/middleware/cookies'
2
require 'action_dispatch/middleware/flash'
3
require 'active_support/core_ext/hash/indifferent_access'
4

J
Joshua Peek 已提交
5 6 7
module ActionDispatch
  module TestProcess
    def assigns(key = nil)
8
      assigns = {}.with_indifferent_access
9
      @controller.view_assigns.each { |k, v| assigns.regular_writer(k, v) }
10
      key.nil? ? assigns : assigns[key]
J
Joshua Peek 已提交
11 12 13 14 15 16 17 18 19 20 21
    end

    def session
      @request.session
    end

    def flash
      @request.flash
    end

    def cookies
22
      @request.cookie_jar
J
Joshua Peek 已提交
23 24 25 26 27 28
    end

    def redirect_to_url
      @response.redirect_url
    end

29
    # Shortcut for <tt>Rack::Test::UploadedFile.new(File.join(ActionController::TestCase.fixture_path, path), type)</tt>:
J
Joshua Peek 已提交
30
    #
31
    #   post :change_avatar, avatar: fixture_file_upload('files/spongebob.png', 'image/png')
J
Joshua Peek 已提交
32 33 34 35
    #
    # To upload binary files on Windows, pass <tt>:binary</tt> as the last parameter.
    # This will not affect other platforms:
    #
36
    #   post :change_avatar, avatar: fixture_file_upload('files/spongebob.png', 'image/png', :binary)
J
Joshua Peek 已提交
37
    def fixture_file_upload(path, mime_type = nil, binary = false)
38 39 40 41
      if self.class.respond_to?(:fixture_path) && self.class.fixture_path
        path = File.join(self.class.fixture_path, path)
      end
      Rack::Test::UploadedFile.new(path, mime_type, binary)
J
Joshua Peek 已提交
42 43 44
    end
  end
end