2011-09-12 7 views
5

私は同じページにリダイレクトするこのメソッドをテストしようとしていますが、www。プレフィックスがない場合はプレフィックスを付けます。 リダイレクトを行いますが、RSpecテストでは "<:redirect>になると期待される応答が返されましたが、< 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

application_controllerの 'before_filter'としてcheck_uriがありますか? – dexter

+0

はい、before_filter経由でcheck_uriが呼び出されます。元の投稿にデバッグレスポンスを追加しました。 – 99miles

答えて

0

あなたはレスポンスのヘッダの場所をテストしてもらえますか?

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

)RSpecのは、デフォルトのテストホストで実行されるため

0

あなたのテストは、例えば、あなたがapplication_controller.rbで定義された方法check_uriに当たっていませんポート80

の「test.host」あなたはあなたのアプリケーションのコントローラに存在しているアクションを呼び出すと、次のように要求されたホストとポートをスタブする必要があります

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 
関連する問題