2011-09-12 42 views
5

我想測試這個方法重定向到同一頁面,但與www。如果它不在那裏,則爲前綴。 它執行重定向,但RSpec測試返回「預期響應爲<:重定向>,但是是< 200>」。這是爲什麼?RSpec測試重定向返回200

application_controller.rb

def check_uri 
    if request.subdomain.present? and request.subdomain.first != "www" 
     redirect_to request.protocol + "www." + request.host_with_port + request.fullpath if !/^www/.match(request.host) 
    end 
    end 

application_controller_spec.rb#

describe :check_uri do 
    it "should redirect to www" do 
     { :get => "http://sub.lvh.me:3000/accounts/sign_up" }. 
     should redirect_to "http://www.sub.lvh.me:3000/accounts/sign_up" 
    end 
    end 

當我調試,我得到: (

rdb:1) p response 
#<ActionController::TestResponse:0x0000010277d988 @writer=#<Proc:[email protected]/Users/mm/.rvm/gems/[email protected]/gems/actionpack-3.0.7/lib/action_dispatch/http/response.rb:43 (lambda)>, @block=nil, @length=0, @header={}, @status=200, @body=[], @cookie=[], @sending_file=false, @blank=false, @cache_control={}, @etag=nil> 
+0

你調試的反應如何?什麼是response.body的內容? – e3matheus

+0

你有check_uri作爲application_controller中的'before_filter'嗎? – dexter

+0

是的,check_uri通過before_filter調用。我將調試響應添加到原始文章中。 – 99miles

回答

0

你能測試響應的頭的位置?

response.header["Location"].should eq("http://www.sub.lvh.me:3000/accounts/sign_up" 

0

您的測試是不打,你在application_controller.rb定義的方法check_uri,因爲RSpec的一個默認的測試主機,例如運行端口「test.host」 80

你應該叫一個動作存在於你的應用程序控制器和存根這樣的請求,主機和端口:

describe :check_uri do 
    it "should redirect to www" do 
    @request.host = 'sub.lvh.me' 
    @request.port = 3000 

    get :index 

    expect(response).to redirect_to "http://www.sub.lvh.me:3000/accounts/sign_up" 
    end 
end