Changeset 1102
- Timestamp:
- 12/13/07 23:23:33 (1 year ago)
- Files:
-
- trunk/lib/merb/test/helper.rb (modified) (1 diff)
- trunk/spec/merb/controller_spec.rb (modified) (5 diffs)
- trunk/spec/merb/router_spec.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/merb/test/helper.rb
r1082 r1102 45 45 end 46 46 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 47 101 end 48 102 end trunk/spec/merb/controller_spec.rb
r1098 r1102 14 14 c._layout.should == :application 15 15 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 16 24 end 17 25 … … 29 37 Merb::Controller.build(request, response) 30 38 end 31 32 # before(:all) do33 # Merb::Router.prepare do |r|34 # @resource_routes = r.resources(:blogs)35 # r.resources(:gardens) do |gardens|36 # @nested_resource = gardens.resources :flowers37 # end38 # @test_route = r.match("/the/:place/:goes/here").to(:controller => "Test", :action => "show").name(:test)39 # @default_route = r.default_routes40 # end41 # end42 43 # it "should generate a url from a route using a hash" do44 # c = new_url_controller(@test_route, :place => "1")45 # c.url_from_route(@test_route, :goes => "g").should == "/the/1/g/here"46 # end47 48 # it "should generate a url from a route using an object" do49 # 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 # end53 54 # it "should generate a url and tack extra params on as a query string" do55 # 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 # end58 59 # it "should generate a url directly from a hash using the current route as a default" do60 # c = new_url_controller(@test_route, :goes => "swimmingly")61 # c.url(:place => "provo").should == "/the/provo/swimmingly/here"62 # end63 64 # it "should generate a default route url with just :controller" do65 # c = new_url_controller(@default_route)66 # c.url(:controller => "welcome").should == "/welcome"67 # end68 39 69 40 it "should generate a default route url with just :action" do … … 76 47 c.url(:id => "23").should == "/foo/bar/23" 77 48 end 78 79 # it 'should generate urls from nested resources' do80 # c = new_url_controller(@nested_resource, :garden => 5)81 # c.url(:flower, :garden_id => 1, :id => 3).should == "/gardens/1/flowers/3"82 # end83 49 84 50 it "should generate a default route url with an extra param" do … … 109 75 end 110 76 111 # it "should generate a default route url with all options" do112 # 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 # end115 #116 77 it "should handle nested nested and more nested hashes and arrays" do 117 78 c = new_url_controller(@default_route, :controller => "foo", :action => "bar") … … 120 81 url.should match(%r{/foo/bar?.*user\[order\]\[\]=name}) 121 82 end 122 123 # it "should handle an object as the second arg" do124 # 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 # end130 131 # it "should point to /blogs/:blog_id if @blog is not new_record" do132 # 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 # end139 140 # it "should point to /blogs/ if @blog is new_record" do141 # 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 # end147 83 end 148 84 trunk/spec/merb/router_spec.rb
r1005 r1102 72 72 Merb::Router.routes[0].should == r1 73 73 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 74 83 end 75 84
