Changeset 1102

Show
Ignore:
Timestamp:
12/13/07 23:23:33 (1 year ago)
Author:
has.s..@gmail.com
Message:

Adds spec helper methods with_route and dispatch_to

Files:

Legend:

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

    r1082 r1102  
    4545        end 
    4646      end 
     47       
     48      # Checks that a route is made to the correct controller etc 
     49      #  
     50      # === Example 
     51      # with_route("/pages/1", "PUT") do |params| 
     52      #   params[:controller].should == "pages" 
     53      #   params[:action].should == "update" 
     54      #   params[:id].should == "1" 
     55      # end 
     56      def with_route(the_path, _method = "GET") 
     57        result = Merb::Router.match(fake_request(the_path, _method), {}) 
     58        yield result[1] if block_given? 
     59        result 
     60      end  
     61 
     62      def fixtures(*files) 
     63        files.each do |name| 
     64          klass = Kernel::const_get(Inflector.classify(Inflector.singularize(name))) 
     65          entries = YAML::load_file(File.dirname(__FILE__) + "/fixtures/#{name}.yaml") 
     66          entries.each do |name, entry| 
     67            klass::create(entry) 
     68          end 
     69        end 
     70      end 
     71 
     72 
     73      # Dispatches an action to a controller.  Defaults to index.   
     74      # The opts hash, if provided will act as the params hash in the controller 
     75      # and the params method in the controller is infact the provided opts hash 
     76      # This controller is based on a fake_request and does not go through the router 
     77      #  
     78      # === Simple Example 
     79      #  {{{ 
     80      #    controller, result = dispatch_to(Pages, :show, :id => 1, :title => "blah") 
     81      #  }}} 
     82      # 
     83      # === Complex Example 
     84      # By providing a block to the dispatch_to method, the controller may be stubbed or mocked prior to the  
     85      # actual dispatch action being called. 
     86      #   {{{ 
     87      #     controller, result = dispatch_to(Pages, :show, :id => 1) do |controller| 
     88      #       controller.stub!(:render).and_return("rendered response") 
     89      #     end 
     90      #   }}} 
     91      def dispatch_to(controller, action = :index, opts = {}) 
     92        klass = controller.class == Class ? controller : controller.class 
     93        klass.stub!(:find_by_title).and_return(@page) 
     94        the_controller = klass.build(fake_request) 
     95        the_controller.stub!(:params).and_return(opts.merge!(:controller => "#{klass.name.downcase}", :action => action.to_s)) 
     96 
     97        yield the_controller if block_given? 
     98 
     99        [the_controller, the_controller.dispatch(action.to_sym)] 
     100      end 
    47101    end 
    48102  end 
  • trunk/spec/merb/controller_spec.rb

    r1098 r1102  
    1414    c._layout.should == :application 
    1515  end 
     16   
     17  it "should have a spec helper to dispatch that skips the router" do 
     18    Merb::Router.should_not_receive(:match) 
     19    dispatch_to(Bar, :foo, :id => "1") do |controller| 
     20      controller.should_receive(:foo).with("1") 
     21    end 
     22  end 
     23   
    1624end 
    1725 
     
    2937    Merb::Controller.build(request, response) 
    3038  end 
    31    
    32   # before(:all) do 
    33   #   Merb::Router.prepare do |r| 
    34   #     @resource_routes = r.resources(:blogs) 
    35   #     r.resources(:gardens) do |gardens| 
    36   #       @nested_resource = gardens.resources :flowers 
    37   #     end 
    38   #     @test_route = r.match("/the/:place/:goes/here").to(:controller => "Test", :action => "show").name(:test) 
    39   #     @default_route = r.default_routes 
    40   #   end 
    41   # end 
    42    
    43   # it "should generate a url from a route using a hash" do 
    44   #   c = new_url_controller(@test_route, :place => "1") 
    45   #   c.url_from_route(@test_route, :goes => "g").should == "/the/1/g/here" 
    46   # end 
    47  
    48   # it "should generate a url from a route using an object" do 
    49   #   c = new_url_controller(@test_route, :place => "2") 
    50   #   obj = OpenStruct.new(:goes => "elsewhere") 
    51   #   c.url_from_route(@test_route, obj).should == "/the/2/elsewhere/here" 
    52   # end 
    53  
    54   # it "should generate a url and tack extra params on as a query string" do 
    55   #   c = new_url_controller(@test_route, :place => "1") 
    56   #   c.url_from_route(@test_route, :goes => "g", :page => 2).should == "/the/1/g/here?page=2" 
    57   # end 
    58    
    59   # it "should generate a url directly from a hash using the current route as a default" do 
    60   #   c = new_url_controller(@test_route, :goes => "swimmingly") 
    61   #   c.url(:place => "provo").should == "/the/provo/swimmingly/here" 
    62   # end 
    63    
    64   # it "should generate a default route url with just :controller" do 
    65   #   c = new_url_controller(@default_route) 
    66   #   c.url(:controller => "welcome").should == "/welcome" 
    67   # end 
    6839 
    6940  it "should generate a default route url with just :action" do 
     
    7647    c.url(:id => "23").should == "/foo/bar/23" 
    7748  end 
    78  
    79   # it 'should generate urls from nested resources' do 
    80   #   c = new_url_controller(@nested_resource, :garden => 5) 
    81   #   c.url(:flower, :garden_id => 1, :id => 3).should == "/gardens/1/flowers/3" 
    82   # end 
    8349 
    8450  it "should generate a default route url with an extra param" do 
     
    10975  end   
    11076   
    111   # it "should generate a default route url with all options" do 
    112   #   c = new_url_controller(@default_route, :controller => "foo", :action => "bar") 
    113   #   c.url(:controller => "foo", :action => "bar", :id => "baz", :format => :js, :monkey => "quux").should == "/foo/bar/baz.js?monkey=quux" 
    114   # end 
    115   #  
    11677  it "should handle nested nested and more nested hashes and arrays" do 
    11778    c = new_url_controller(@default_route, :controller => "foo", :action => "bar") 
     
    12081    url.should match(%r{/foo/bar?.*user\[order\]\[\]=name}) 
    12182  end 
    122    
    123   # it "should handle an object as the second arg" do 
    124   #   c = new_url_controller(@resource_routes, :controller => "blogs", :action => "show") 
    125   #   blog = mock("blog") 
    126   #   blog.should_receive(:id).once.and_return(7) 
    127   #   url = c.url(:blog, blog) 
    128   #   url.should == "/blogs/7" 
    129   # end 
    130  
    131   # it "should point to /blogs/:blog_id if @blog is not new_record" do 
    132   #   c = new_url_controller(@resource_routes, :controller => "blogs", :action => "index") 
    133   #   blog = mock("blog") 
    134   #   blog.should_receive(:id).once.and_return(7) 
    135   #   blog.should_receive(:new_record?).once.and_return(false) 
    136   #   url = c.url(:blog, blog) 
    137   #   url.should == "/blogs/7" 
    138   # end 
    139  
    140   # it "should point to /blogs/ if @blog is new_record" do 
    141   #   c = new_url_controller(@resource_routes, :controller => "blogs", :action => "index") 
    142   #   blog = mock("blog") 
    143   #   blog.should_receive(:new_record?).once.and_return(true) 
    144   #   url = c.url(:blog, blog) 
    145   #   url.should == "/blogs/" 
    146   # end 
    14783end 
    14884 
  • trunk/spec/merb/router_spec.rb

    r1005 r1102  
    7272    Merb::Router.routes[0].should == r1 
    7373    Merb::Router.routes[1].should == r2 
     74  end 
     75   
     76  it "should have a spec helper to match routes" do 
     77    Merb::Router.prepare{ |r| r.default_routes } 
     78    with_route("/pages/show/1", "GET") do |params| 
     79      params[:controller].should == "pages" 
     80      params[:action].should == "show" 
     81      params[:id].should == "1" 
     82    end     
    7483  end 
    7584