2010-10-27 41 views
3

我已經成功添加了在我的應用程序中使用動態子域的功能。問題是,當我運行我的黃瓜測試,我收到以下錯誤,當我的應用程序執行其中包含一個子域redirect_to的:黃瓜/水豚使用子域的問題

features/step_definitions/web_steps.rb:27 
the scheme http does not accept registry part: test_url.example.com (or bad hostname?) 

我有一個創建用戶和所選擇的帳戶和重定向一個註冊控制器動作用戶根據用戶在註冊表單中選擇的子域指定的子域指定註銷方法。下面是一旦用戶和賬號模型創建並保存這種情況發生重定向操作的代碼:

redirect_to :controller => "sessions", :action => "destroy", :subdomain => @account.site_address 

這裏是我的軌道3條路線:

constraints(Subdomain) do 
    resources :sessions 
    match 'login', :to => 'sessions#new', :as => :login 
    match 'logout', :to => 'sessions#destroy', :as => :logout 
    match '/' => 'accounts#show' 
end 

下面是我到目前爲止的代碼針對其在上面的約束指定的子域類:

class Subdomain 
    def self.matches?(request) 
    request.subdomain.present? && request.subdomain != "www" 
    end 
end 

我添加UrlHelper到ApplicationController中:

class ApplicationController < ActionController::Base 
    include UrlHelper 
    protect_from_forgery 
end 

這是上述UrlHelper類的代碼:

module UrlHelper 
    def with_subdomain(subdomain) 
    subdomain = (subdomain || "") 
    subdomain += "." unless subdomain.empty? 
    [subdomain, request.domain, request.port_string].join 
    end 

    def url_for(options = nil) 
    if options.kind_of?(Hash) && options.has_key?(:subdomain) 
     options[:host] = with_subdomain(options.delete(:subdomain)) 
    end 
    super 
    end 
end 

所有這些代碼的上面讓我在本地瀏覽器中運行良好的子域。上面的問題發生在我運行我的黃瓜測試時。測試點擊註冊按鈕,該按鈕又調用redirect_to並拋出上面列出的例外。

這裏是我的寶石文件看起來像:

require 'subdomain' 

SomeApp::Application.routes.draw do 

    resources :accounts, :only => [:new, :create] 
    match 'signup', :to => 'accounts#new' 

    constraints(Subdomain) do 
    resources :sessions 
    match 'login', :to => 'sessions#new', :as => :login 
    match 'logout', :to => 'sessions#destroy', :as => :logout 

    match '/' => 'accounts#show' 
    end 
end 

能否請你讓被知道有沒有我的測試工作,現在一個額外的方法?我會對修補程序感興趣或者我可以在不使用子域的情況下測試我的方法(例如,檢索帳戶名稱的模擬方法)。

+0

爲了解決這個錯誤,我改變了url_for方法來做檢查,以確保我不在測試環境中。這讓我誤解了錯誤,但它並不模仿「生產」的作用。這將迫使我使用不同的方法而不是子域來檢索當前帳戶名稱。 – 2010-10-27 13:35:43

回答

1

我在我的代碼中有相同的模式。我使用水豚(但不是黃瓜),我能夠繞過它像這樣:

# user creates an account that will have a new subdomain 
    click_button "Get Started" 
    host! "testyco.myapp.com" 

    # user is now visiting app on new subdomain 
    visit "/register/get_started/" + Resetkey.first.resetkey 
    assert_contain("Get Started Guide") 

主人!命令有效地改變了主機在測試請求中對應用程序的顯示。

編輯:剛纔意識到這是與webrat,但不是水豚(我使用兩個,現在逐步淘汰網頁。)我在水豚做它的方式是要麼點擊鏈接到一個新的域名水豚遵循它)或:

visit "http://testyco.myapp.com/register" 

編輯:另一個更新。找到一種方法,無需在每個事件中使用完整的URL。

 host! "test.hiringthing.com" 
     Capybara.app_host = "http://test.hiringthing.com" 

在測試設置中。