console_test.rb 2.0 KB
Newer Older
J
Joshua Peek 已提交
1 2 3 4 5 6 7 8
require 'isolation/abstract_unit'

class ConsoleTest < Test::Unit::TestCase
  include ActiveSupport::Testing::Isolation

  def setup
    build_app
    boot_rails
9
  end
J
Joshua Peek 已提交
10

11
  def load_environment
J
Joshua Peek 已提交
12 13
    # Load steps taken from rails/commands/console.rb
    require "#{rails_root}/config/environment"
14 15
    require 'rails/console/app'
    require 'rails/console/helpers'
J
Joshua Peek 已提交
16 17 18
  end

  def test_app_method_should_return_integration_session
19
    load_environment
J
Joshua Peek 已提交
20 21 22 23 24 25
    console_session = app
    assert_not_nil console_session
    assert_instance_of ActionController::Integration::Session, console_session
  end

  def test_new_session_should_return_integration_session
26
    load_environment
J
Joshua Peek 已提交
27 28 29 30 31 32
    session = new_session
    assert_not_nil session
    assert_instance_of ActionController::Integration::Session, session
  end

  def test_reload_should_fire_preparation_callbacks
33
    load_environment
J
Joshua Peek 已提交
34 35 36 37 38 39 40 41
    a = b = c = nil

    # TODO: These should be defined on the initializer
    ActionDispatch::Callbacks.to_prepare { a = b = c = 1 }
    ActionDispatch::Callbacks.to_prepare { b = c = 2 }
    ActionDispatch::Callbacks.to_prepare { c = 3 }

    # Hide Reloading... output
42
    silence_stream(STDOUT) { reload! }
J
Joshua Peek 已提交
43 44 45 46 47 48

    assert_equal 1, a
    assert_equal 2, b
    assert_equal 3, c
  end

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  def test_reload_should_reload_constants
    app_file "app/models/user.rb", <<-MODEL
      class User
        attr_accessor :name
      end
    MODEL

    load_environment
    assert User.new.respond_to?(:name)
    assert !User.new.respond_to?(:age)

    app_file "app/models/user.rb", <<-MODEL
      class User
        attr_accessor :name, :age
      end
    MODEL

    assert !User.new.respond_to?(:age)
    silence_stream(STDOUT) { reload! }
    assert User.new.respond_to?(:age)
  end

J
Joshua Peek 已提交
71
  def test_access_to_helpers
72
    load_environment
J
Joshua Peek 已提交
73 74 75 76 77 78
    assert_not_nil helper
    assert_instance_of ActionView::Base, helper
    assert_equal 'Once upon a time in a world...',
      helper.truncate('Once upon a time in a world far far away')
  end
end