Changeset 1077

Show
Ignore:
Timestamp:
12/10/07 05:53:15 (1 year ago)
Author:
has.s..@gmail.com
Message:

Seperates spec helpers into the Merb::Test namespace to prevent spec methods leaking into
specs.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app_generators/merb/templates/spec/spec_helper.rb

    r932 r1077  
    77require 'merb/test/rspec' 
    88 
     9Spec::Runner.configure do |config| 
     10    config.include(Merb::Test::Helper) 
     11    config.include(Merb::Test::RspecMatchers) 
     12end 
     13 
     14 
    915### METHODS BELOW THIS LINE SHOULD BE EXTRACTED TO MERB ITSELF 
  • trunk/app_generators/merb/templates/test/test_helper.rb

    r826 r1077  
    1010 
    1111class Test::Unit::TestCase 
     12  include Merb::Test::Helper 
    1213  # Add more helper methods to be used by all tests here... 
    1314end 
  • trunk/lib/merb/test/helper.rb

    r1074 r1077  
    33include HpricotTestHelper 
    44 
    5 # Create a FakeRequest suitable for passing to Controller.build 
    6 def fake_request(path="/",method='GET') 
    7   method = method.to_s.upcase 
    8   Merb::Test::FakeRequest.with(path, :request_method => method) 
    9 end 
     5module Merb 
     6  module Test 
     7    module Helper 
     8      # Create a FakeRequest suitable for passing to Controller.build 
     9      def fake_request(path="/",method='GET') 
     10        method = method.to_s.upcase 
     11        Merb::Test::FakeRequest.with(path, :request_method => method) 
     12      end 
    1013 
    11 # Turn a named route into a string with the path 
    12 # This is the same method as is found in the controller 
    13 def url(name, *args) 
    14   # @_spect_url_builder ||= Merb::ControllerMixin 
    15   # @_spec_url_builder.url(*args) 
    16   Merb::Router.generate(name, *args) 
    17 end 
     14      # Turn a named route into a string with the path 
     15      # This is the same method as is found in the controller 
     16      def url(name, *args) 
     17        Merb::Router.generate(name, *args) 
     18      end 
    1819 
    1920 
    20 # For integration/functional testing 
     21      # For integration/functional testing 
    2122 
    2223 
    23 def request(verb, path) 
    24   response = StringIO.new 
    25   @request = Merb::Test::FakeRequest.with(path, :request_method => (verb.to_s.upcase rescue 'GET')) 
     24      def request(verb, path) 
     25        response = StringIO.new 
     26        @request = Merb::Test::FakeRequest.with(path, :request_method => (verb.to_s.upcase rescue 'GET')) 
    2627   
    27   yield @request if block_given? 
     28        yield @request if block_given? 
    2829   
    29   @controller, @action = Merb::Dispatcher.handle @request, response 
     30        @controller, @action = Merb::Dispatcher.handle @request, response 
     31      end 
     32 
     33      def get(path) 
     34        request("GET",path) 
     35      end 
     36    end 
     37  end 
    3038end 
    31  
    32 def get(path) 
    33   request("GET",path) 
    34 end 
  • trunk/lib/merb/test/rspec.rb

    r993 r1077  
    11require 'hpricot' 
    22require 'spec' 
    3 module MerbRspecMatchers 
    4   class HaveSelector 
    5     def initialize(expected) 
    6       @expected = expected 
    7     end 
     3module Merb 
     4  module Test 
     5    module RspecMatchers 
     6      class HaveSelector 
     7        def initialize(expected) 
     8          @expected = expected 
     9        end 
    810     
    9     def matches?(stringlike) 
    10       @document = case stringlike 
    11       when Hpricot::Elem 
    12         stringlike 
    13       when StringIO 
    14         Hpricot.parse(stringlike.string) 
    15       else 
    16         Hpricot.parse(stringlike) 
     11        def matches?(stringlike) 
     12          @document = case stringlike 
     13          when Hpricot::Elem 
     14            stringlike 
     15          when StringIO 
     16            Hpricot.parse(stringlike.string) 
     17          else 
     18            Hpricot.parse(stringlike) 
     19          end 
     20          !@document.search(@expected).empty? 
     21        end 
     22     
     23        def failure_message 
     24          "expected following text to match selector #{@expected}:\n#{@document}" 
     25        end 
     26 
     27        def negative_failure_message 
     28          "expected following text to not match selector #{@expected}:\n#{@document}" 
     29        end 
    1730      end 
    18       !@document.search(@expected).empty? 
    19     end 
     31   
     32      class MatchTag 
     33        def initialize(name, attrs) 
     34          @name, @attrs = name, attrs 
     35          @content = @attrs.delete(:content) 
     36        end 
     37 
     38        def matches?(target) 
     39          @errors = [] 
     40          unless target.include?("<#{@name}") 
     41            @errors << "Expected a <#{@name}>, but was #{target}" 
     42          end 
     43          @attrs.each do |attr, val| 
     44            unless target.include?("#{attr}=\"#{val}\"") 
     45              @errors << "Expected #{attr}=\"#{val}\", but was #{target}" 
     46            end 
     47          end 
     48          if @content 
     49            unless target.include?(">#{@content}<") 
     50              @errors << "Expected #{target} to include #{@content}" 
     51            end 
     52          end 
     53          @errors.size == 0 
     54        end 
    2055     
    21     def failure_message 
    22       "expected following text to match selector #{@expected}:\n#{@document}" 
    23     end 
    24  
    25     def negative_failure_message 
    26       "expected following text to not match selector #{@expected}:\n#{@document}" 
     56        def failure_message 
     57          @errors[0] 
     58        end 
     59     
     60        def negative_failure_message 
     61          "Expected not to match against <#{@name} #{@attrs.map{ |a,v| "#{a}=\"#{v}\"" }.join(" ")}> tag, but it matched" 
     62        end 
     63      end 
     64   
     65      class NotMatchTag 
     66        def initialize(attrs) 
     67          @attrs = attrs 
     68        end 
     69     
     70        def matches?(target) 
     71          @errors = [] 
     72          @attrs.each do |attr, val| 
     73            if target.include?("#{attr}=\"#{val}\"") 
     74              @errors << "Should not include #{attr}=\"#{val}\", but was #{target}" 
     75            end 
     76          end 
     77          @errors.size == 0 
     78        end 
     79     
     80        def failure_message 
     81          @errors[0] 
     82        end 
     83      end 
     84   
     85      def match_tag(name, attrs) 
     86        MatchTag.new(name, attrs) 
     87      end 
     88      def not_match_tag(attrs) 
     89        NotMatchTag.new(attrs) 
     90      end 
     91   
     92      def have_selector(expected) 
     93        HaveSelector.new(expected) 
     94      end 
     95      alias_method :match_selector, :have_selector 
     96      # alias_method :match_regex, :match 
    2797    end 
    2898  end 
    29    
    30   class MatchTag 
    31     def initialize(name, attrs) 
    32       @name, @attrs = name, attrs 
    33       @content = @attrs.delete(:content) 
    34     end 
    35  
    36     def matches?(target) 
    37       @errors = [] 
    38       unless target.include?("<#{@name}") 
    39         @errors << "Expected a <#{@name}>, but was #{target}" 
    40       end 
    41       @attrs.each do |attr, val| 
    42         unless target.include?("#{attr}=\"#{val}\"") 
    43           @errors << "Expected #{attr}=\"#{val}\", but was #{target}" 
    44         end 
    45       end 
    46       if @content 
    47         unless target.include?(">#{@content}<") 
    48           @errors << "Expected #{target} to include #{@content}" 
    49         end 
    50       end 
    51       @errors.size == 0 
    52     end 
    53      
    54     def failure_message 
    55       @errors[0] 
    56     end 
    57      
    58     def negative_failure_message 
    59       "Expected not to match against <#{@name} #{@attrs.map{ |a,v| "#{a}=\"#{v}\"" }.join(" ")}> tag, but it matched" 
    60     end 
    61   end 
    62    
    63   class NotMatchTag 
    64     def initialize(attrs) 
    65       @attrs = attrs 
    66     end 
    67      
    68     def matches?(target) 
    69       @errors = [] 
    70       @attrs.each do |attr, val| 
    71         if target.include?("#{attr}=\"#{val}\"") 
    72           @errors << "Should not include #{attr}=\"#{val}\", but was #{target}" 
    73         end 
    74       end 
    75       @errors.size == 0 
    76     end 
    77      
    78     def failure_message 
    79       @errors[0] 
    80     end 
    81   end 
    82    
    83   def match_tag(name, attrs) 
    84     MatchTag.new(name, attrs) 
    85   end 
    86   def not_match_tag(attrs) 
    87     NotMatchTag.new(attrs) 
    88   end 
    89    
    90   def have_selector(expected) 
    91     HaveSelector.new(expected) 
    92   end 
    93   alias_method :match_selector, :have_selector 
    94   # alias_method :match_regex, :match 
    9599end 
    96  
    97 Spec::Runner.configure do |config| 
    98   config.include(MerbRspecMatchers) 
    99 end 
  • trunk/spec/merb/dispatch_spec.rb

    r1042 r1077  
    55 
    66describe Merb::Dispatcher do 
    7    
     7 
    88  before(:all) do 
    99    Merb::Server.config[:allow_reloading] = false 
  • trunk/spec/spec_helper.rb

    r1056 r1077  
    1414FIXTURES = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures')) 
    1515 
     16Spec::Runner.configure do |config| 
     17  config.include(Merb::Test::Helper) 
     18  config.include(Merb::Test::RspecMatchers) 
     19end 
    1620 
    1721# Creates a new controller, e.g.