百度首页 | 百度空间
 
查看文章
 
把Rails的controllers映射到二级域名(subdomains)
2008-07-14 18:59

Rails提供的测试routes的helper有3个:

assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
只返回根据options生成的路由列表里第一条命中的,再与expected_path比较,不适合有多种形态URL的情况

assert_recognizes(expected_options, path, extras={}, message=nil)
用来判断URL是否能达到期待的controller 和action,path可以是Hash,可传入POST或GET等方法。

assert_routing(path, options, defaults={}, extras={}, message=nil)
同时测试assert_generates 和 assert_recognizes,适合唯一映射的情况

还有个更重要的问题是,测试文件要放在哪里?

应该放在 test/integration 目录下。先有个概念:unit 是用来测试 model,functional 用来测试 controller 及其 actions 的,integration 是用来做跨多个 controller 测试。functional 比 integration 要快些,因为它掉过了一些初始化操作,比如roules的初始化。所以只有 integration 才适合做 routes 测试。

# test/integration/routes_test.rb
require "#{File.dirname(__FILE__)}/../test_helper"

class RoutesTest < ActionController::IntegrationTest
    # Rails提供的helper不能直接用来测试subdomains,所以需要自己扩展一个helper
    include RoutesTestHelper

    # test subdomains
    def test_should_have_subdomains_routing
      # test show action
      opts = { :controller => "post", :action => "show", :id => "1" }
      assert_recognizes_uri opts,"http://myapp.com/post/show/1"

      assert_recognizes_uri opts,"http://post.myapp.com/show/1"

      assert_recognizes_uri opts,"http://post.myapp.com/1"

      # test index action
      opts = { :controller => "post", :action => "index" }

      assert_recognizes_uri opts,"http://www.myapp.com/post"
      assert_recognizes_uri opts,"http://myapp.com/post"

      assert_recognizes_uri opts,"http://post.myapp.com/"

      assert_recognizes_uri opts,"http://post.myapp.com/index"
  
    end

end


类别:服务器技术 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu