Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Allowing Unit Tests to work with default helpers #45

Merged
merged 5 commits into from about 1 month ago

2 participants

Mohamed Magdy gi-lunaweb
Mohamed Magdy

This is a fix for the unit tests that have default route helpers like people_path without specifying the locale and without setting the generate_unlocalized_routes config.

GSI GSI referenced this pull request February 09, 2014
Closed

How do I test routes? #27

gi-lunaweb gi-lunaweb merged commit 6932dcf into from February 25, 2014
gi-lunaweb gi-lunaweb closed this February 25, 2014
gi-lunaweb
Collaborator

Thanks for your work!
Changes have been merged into master and should be released to rubygems soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
7  lib/route_translator/extensions/action_controller.rb
@@ -8,4 +8,11 @@ def set_locale_from_url
8 8
       I18n.locale = params[RouteTranslator.locale_param_key]
9 9
     end
10 10
   end
  11
+
  12
+  class TestCase
  13
+  	include ActionController::UrlFor
  14
+
  15
+  	delegate :env, :request, :to => :@controller
  16
+  	def _routes; @routes; end
  17
+  end
11 18
 end
5  lib/route_translator/extensions/mapper.rb
@@ -26,11 +26,10 @@ def add_route(action, options) # :nodoc:
26 26
           end
27 27
 
28 28
           mapping = Mapping.new(@set, @scope, path, options)
29  
-          app, conditions, requirements, defaults, as, anchor = mapping.to_route
30 29
           if @localized
31  
-            @set.add_localized_route(app, conditions, requirements, defaults, as, anchor)
  30
+            @set.add_localized_route(*mapping.to_route)
32 31
           else
33  
-            @set.add_route(app, conditions, requirements, defaults, as, anchor)
  32
+            @set.add_route(*mapping.to_route)
34 33
           end
35 34
         end
36 35
       else
2  lib/route_translator/extensions/route_set.rb
@@ -10,4 +10,4 @@ def add_localized_route(app, conditions = {}, requirements = {}, defaults = {},
10 10
       end
11 11
     end
12 12
   end
13  
-end
  13
+end
5  lib/route_translator/translator.rb
@@ -17,6 +17,11 @@ def self.add_untranslated_helpers_to_controllers_and_views(old_name, helper_cont
17 17
             __send__("#{old_name}_#{I18n.default_locale.to_s.underscore}_#{suffix}", *args)
18 18
           end
19 19
         end
  20
+
  21
+        # Including the named routes helpers module
  22
+        [ActionController::TestCase, ActionView::TestCase, ActionMailer::TestCase].each do |klass|
  23
+          klass.__send__(:include, helper_container)
  24
+        end
20 25
       end
21 26
     end
22 27
 
33  test/routing_test.rb
@@ -422,4 +422,37 @@ def test_action_controller_gets_locale_suffix_helper
422 422
   def test_action_view_gets_locale_suffix_helper
423 423
     ActionView::Base.instance_methods.include?('locale_suffix')
424 424
   end
  425
+
  426
+  def test_action_controller_test_case_reads_default_urls
  427
+    test_case_reads_default_urls(ActionController::TestCase)
  428
+  end
  429
+
  430
+  def test_action_view_test_case_reads_default_urls
  431
+    test_case_reads_default_urls(ActionView::TestCase)
  432
+  end
  433
+
  434
+  def test_action_mailer_test_case_reads_default_urls
  435
+    test_case_reads_default_urls(ActionMailer::TestCase)
  436
+  end
  437
+
  438
+  private
  439
+  def test_case_reads_default_urls(klass)
  440
+    config_default_locale_settings 'en'
  441
+
  442
+    draw_routes do
  443
+      localized do
  444
+        resources :person
  445
+      end
  446
+    end
  447
+
  448
+    test_case = klass.new(nil)
  449
+
  450
+    # Not localized
  451
+    assert test_case.respond_to?(:people_path)
  452
+    assert test_case.respond_to?(:new_person_path)
  453
+    
  454
+    # Localized
  455
+    assert test_case.respond_to?(:people_en_path)
  456
+    assert test_case.respond_to?(:new_person_en_path)
  457
+  end
425 458
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.