Changeset 1114

Show
Ignore:
Timestamp:
12/14/07 20:26:41 (1 year ago)
Author:
has.s..@gmail.com
Message:

Reorganised the RspecMatchers? and put some extra funtionality into
get, put etc so that you can stub the response

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/test/helper.rb

    r1110 r1114  
    4040      #   get "/users", :user => {:login => "dave", :email => "email@example.com"}  
    4141      # }}} 
    42       def get(path, opts = {}
    43         request("GET",path_with_options(path,opts)
     42      def get(path, opts = {}, &block
     43        request("GET",path_with_options(path,opts), &block
    4444      end 
    4545       
     
    5050      #   post "/users", :user => {:login => "dave", :email => "email@example.com"}  
    5151      # }}} 
    52       def post(path, opts = {}
    53         request("POST", path_with_options(path,opts)
     52      def post(path, opts = {}, &block
     53        request("POST", path_with_options(path,opts), &block
    5454      end 
    5555       
     
    6060      #   put "/users/1", :user => {:login => "dave", :email => "email@example.com"}  
    6161      # }}} 
    62       def put(path,opts = {}
    63         request("PUT", path_with_options(path,opts)
     62      def put(path,opts = {}, &block
     63        request("PUT", path_with_options(path,opts), &block
    6464      end 
    6565       
     
    6969      #   delete "/users/1", :user => {:login => "dave", :email => "email@example.com"}  
    7070      # }}} 
    71       def delete(path, opts= {}
    72         request("DELETE", path_with_options(path,opts)
     71      def delete(path, opts= {}, &block
     72        request("DELETE", path_with_options(path,opts), &block
    7373      end 
    7474       
     
    142142        path 
    143143      end 
    144          
    145         
    146144    end 
    147145  end 
    148146end 
     147 
     148class Object 
     149  # Checks that an object has assigned an instance variable of name 
     150  # :name 
     151  #  
     152  # ===Example in a spec 
     153  #  @my_obj.assings(:my_value).should == @my_value 
     154  def assigns(attr) 
     155    self.instance_variable_get("@#{attr}") 
     156  end 
     157end 
     158 
  • trunk/lib/merb/test/rspec.rb

    r1111 r1114  
    11require 'hpricot' 
    22require 'spec' 
     3 
     4# Get all the rspec matchers for merb and include them 
     5Dir[(File.dirname(__FILE__) + "/rspec_matchers/**/*.rb")].each do |file| 
     6  require "#{file[0...-3]}" 
     7end 
     8 
    39module Merb 
    410  module Test 
    511    module RspecMatchers 
     12       
     13      include ControllerMatchers 
     14      include MarkupMatchers 
    615 
    7       class BeRedirect 
    8         def matches?(target) 
    9           @target = target 
    10           target == 302 
    11         end 
    12         def failure_message 
    13           "expected to redirect" 
    14         end 
    15         def negative_failure_message 
    16           "expected not to redirect" 
    17         end 
    18       end 
    19  
    20       class Redirect 
    21         def matches?(target) 
    22           @target = target 
    23           BeRedirect.new.matches?(target.status) 
    24         end 
    25         def failure_message 
    26           "expected #{@target.inspect} to redirect" 
    27         end 
    28         def negative_failure_message 
    29           "expected #{@target.inspect} not to redirect" 
    30         end 
    31       end 
    32  
    33       class RedirectTo 
    34         def initialize(expected) 
    35           @expected = expected 
    36         end 
    37  
    38         def matches?(target) 
    39           @target = target.headers['Location'] 
    40           @redirected = BeRedirect.new.matches?(target.status) 
    41           @target == @expected 
    42         end 
    43  
    44         def failure_message 
    45           msg = "expected a redirect to <#{@expected}>, but " 
    46           if @redirected 
    47             msg << "found one to <#{@target}>"  
    48           else 
    49             msg << "there was no redirect" 
    50           end 
    51         end 
    52  
    53         def negative_failure_message 
    54           "expected not to redirect to <#{@expected}>, but did anyway" 
    55         end 
    56       end 
    57  
    58       def be_redirect 
    59         BeRedirect.new 
    60       end 
    61  
    62       def redirect 
    63         Redirect.new 
    64       end 
    65  
    66       def redirect_to(expected) 
    67         RedirectTo.new(expected) 
    68       end 
    69  
    70  
    71       class HaveSelector 
    72         def initialize(expected) 
    73           @expected = expected 
    74         end 
    75      
    76         def matches?(stringlike) 
    77           @document = case stringlike 
    78           when Hpricot::Elem 
    79             stringlike 
    80           when StringIO 
    81             Hpricot.parse(stringlike.string) 
    82           else 
    83             Hpricot.parse(stringlike) 
    84           end 
    85           !@document.search(@expected).empty? 
    86         end 
    87      
    88         def failure_message 
    89           "expected following text to match selector #{@expected}:\n#{@document}" 
    90         end 
    91  
    92         def negative_failure_message 
    93           "expected following text to not match selector #{@expected}:\n#{@document}" 
    94         end 
    95       end 
    96    
    97       class MatchTag 
    98         def initialize(name, attrs) 
    99           @name, @attrs = name, attrs 
    100           @content = @attrs.delete(:content) 
    101         end 
    102  
    103         def matches?(target) 
    104           @errors = [] 
    105           unless target.include?("<#{@name}") 
    106             @errors << "Expected a <#{@name}>, but was #{target}" 
    107           end 
    108           @attrs.each do |attr, val| 
    109             unless target.include?("#{attr}=\"#{val}\"") 
    110               @errors << "Expected #{attr}=\"#{val}\", but was #{target}" 
    111             end 
    112           end 
    113           if @content 
    114             unless target.include?(">#{@content}<") 
    115               @errors << "Expected #{target} to include #{@content}" 
    116             end 
    117           end 
    118           @errors.size == 0 
    119         end 
    120      
    121         def failure_message 
    122           @errors[0] 
    123         end 
    124      
    125         def negative_failure_message 
    126           "Expected not to match against <#{@name} #{@attrs.map{ |a,v| "#{a}=\"#{v}\"" }.join(" ")}> tag, but it matched" 
    127         end 
    128       end 
    129    
    130       class NotMatchTag 
    131         def initialize(attrs) 
    132           @attrs = attrs 
    133         end 
    134      
    135         def matches?(target) 
    136           @errors = [] 
    137           @attrs.each do |attr, val| 
    138             if target.include?("#{attr}=\"#{val}\"") 
    139               @errors << "Should not include #{attr}=\"#{val}\", but was #{target}" 
    140             end 
    141           end 
    142           @errors.size == 0 
    143         end 
    144      
    145         def failure_message 
    146           @errors[0] 
    147         end 
    148       end 
    149    
    150       def match_tag(name, attrs) 
    151         MatchTag.new(name, attrs) 
    152       end 
    153       def not_match_tag(attrs) 
    154         NotMatchTag.new(attrs) 
    155       end 
    156    
    157       def have_selector(expected) 
    158         HaveSelector.new(expected) 
    159       end 
    160       alias_method :match_selector, :have_selector 
    161       # alias_method :match_regex, :match 
    16216    end 
    16317  end 
  • trunk/spec/spec_helper.rb

    r1098 r1114  
    1919  config.include(Merb::Test::Helper) 
    2020  config.include(Merb::Test::RspecMatchers) 
    21   config.include(Merb::Test::MerbRspecControllerRedirect)   
     21  # config.include(Merb::Test::MerbRspecControllerRedirect)   
    2222end 
    2323