spec_helper.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. require File.expand_path("../spec_helper_without_rails", __FILE__)
  2. module ActiveAdminIntegrationSpecHelper
  3. extend self
  4. def load_defaults!
  5. ActiveAdmin.unload!
  6. ActiveAdmin.load!
  7. ActiveAdmin.register(Category)
  8. ActiveAdmin.register(User)
  9. ActiveAdmin.register(Post){ belongs_to :user, :optional => true }
  10. reload_menus!
  11. end
  12. def reload_menus!
  13. ActiveAdmin.application.namespaces.values.each{|n| n.load_menu! }
  14. end
  15. # Sometimes we need to reload the routes within
  16. # the application to test them out
  17. def reload_routes!
  18. Rails.application.reload_routes!
  19. end
  20. # Helper method to load resources and ensure that Active Admin is
  21. # setup with the new configurations.
  22. #
  23. # Eg:
  24. # load_resources do
  25. # ActiveAdmin.regiser(Post)
  26. # end
  27. #
  28. def load_resources
  29. ActiveAdmin.unload!
  30. yield
  31. reload_menus!
  32. reload_routes!
  33. end
  34. # Sets up a describe block where you can render controller
  35. # actions. Uses the Admin::PostsController as the subject
  36. # for the describe block
  37. def describe_with_render(*args, &block)
  38. describe *args do
  39. include RSpec::Rails::ControllerExampleGroup
  40. render_views
  41. # metadata[:behaviour][:describes] = ActiveAdmin.namespaces[:admin].resources['Post'].controller
  42. module_eval &block
  43. end
  44. end
  45. # Sets up an Arbre::Builder context
  46. def setup_arbre_context!
  47. include Arbre::Builder
  48. let(:assigns){ {} }
  49. let(:helpers){ mock_action_view }
  50. before do
  51. @_helpers = helpers
  52. end
  53. end
  54. # Setup a describe block which uses capybara and rails integration
  55. # test methods.
  56. def describe_with_capybara(*args, &block)
  57. describe *args do
  58. include RSpec::Rails::IntegrationExampleGroup
  59. module_eval &block
  60. end
  61. end
  62. # Returns a fake action view instance to use with our renderers
  63. def mock_action_view(assigns = {})
  64. controller = ActionView::TestCase::TestController.new
  65. ActionView::Base.send :include, ActionView::Helpers
  66. ActionView::Base.send :include, ActiveAdmin::ViewHelpers
  67. ActionView::Base.send :include, Rails.application.routes.url_helpers
  68. ActionView::Base.new(ActionController::Base.view_paths, assigns, controller)
  69. end
  70. alias_method :action_view, :mock_action_view
  71. # A mock resource to register
  72. class MockResource
  73. end
  74. end
  75. ENV['RAILS_ENV'] = 'test'
  76. require 'detect_rails_version'
  77. rails_version = detect_rails_version
  78. ENV['RAILS_ROOT'] = File.expand_path("../rails/rails-#{rails_version}", __FILE__)
  79. # Create the test app if it doesn't exists
  80. unless File.exists?(ENV['RAILS_ROOT'])
  81. system 'rake setup'
  82. end
  83. # Ensure the Active Admin load path is happy
  84. require 'rails'
  85. require 'active_admin'
  86. ActiveAdmin.application.load_paths = [ENV['RAILS_ROOT'] + "/app/admin"]
  87. require ENV['RAILS_ROOT'] + '/config/environment'
  88. require 'rspec/rails'
  89. # Setup Some Admin stuff for us to play with
  90. include ActiveAdminIntegrationSpecHelper
  91. load_defaults!
  92. reload_routes!
  93. # Disabling authentication in specs so that we don't have to worry about
  94. # it allover the place
  95. ActiveAdmin.application.authentication_method = false
  96. ActiveAdmin.application.current_user_method = false
  97. # Don't add asset cache timestamps. Makes it easy to integration
  98. # test for the presence of an asset file
  99. ENV["RAILS_ASSET_ID"] = ''
  100. RSpec.configure do |config|
  101. config.use_transactional_fixtures = true
  102. config.use_instantiated_fixtures = false
  103. end
  104. # All RSpec configuration needs to happen before any examples
  105. # or else it whines.
  106. require 'integration_example_group'
  107. RSpec.configure do |c|
  108. c.include RSpec::Rails::IntegrationExampleGroup, :example_group => { :file_path => /\bspec\/integration\// }
  109. end
  110. # Ensure this is defined for Ruby 1.8
  111. module MiniTest; class Assertion < Exception; end; end
  112. RSpec::Matchers.define :have_tag do |*args|
  113. match_unless_raises Test::Unit::AssertionFailedError do |response|
  114. tag = args.shift
  115. content = args.first.is_a?(Hash) ? nil : args.shift
  116. options = {
  117. :tag => tag.to_s
  118. }.merge(args[0] || {})
  119. options[:content] = content if content
  120. begin
  121. begin
  122. assert_tag(options)
  123. rescue NoMethodError
  124. # We are not in a controller, so let's do the checking ourselves
  125. doc = HTML::Document.new(response, false, false)
  126. tag = doc.find(options)
  127. assert tag, "expected tag, but no tag found matching #{options.inspect} in:\n#{response.inspect}"
  128. end
  129. # In Ruby 1.9, MiniTest::Assertion get's raised, so we'll
  130. # handle raising a Test::Unit::AssertionFailedError
  131. rescue MiniTest::Assertion => e
  132. raise Test::Unit::AssertionFailedError, e.message
  133. end
  134. end
  135. end