2011-09-12 25 views
5

Tôi đang thử nghiệm phương pháp này chuyển hướng đến cùng một trang nhưng với www. tiền tố nếu nó không có ở đó. Nó chuyển hướng, nhưng kiểm tra RSpec trả về "Phản hồi dự kiến ​​là <: chuyển hướng>, nhưng là < 200>." Tại sao vậy?Kiểm tra RSpec cho trả về chuyển hướng 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 

Khi tôi gỡ lỗi tôi nhận được: (

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

Cậu gỡ lỗi phản ứng? Nội dung cho response.body là gì? – e3matheus

+0

bạn có check_uri là 'before_filter' trong application_controller không? – dexter

+0

Có, check_uri được gọi qua before_filter. Tôi đã thêm phản hồi gỡ lỗi vào bài đăng gốc. – 99miles

Trả lời

0

bạn có thể kiểm tra các vị trí tiêu đề của phản ứng?

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

)

0

thử nghiệm của bạn không được đánh phương pháp check_uri mà bạn xác định trong application_controller.rb, vì RSpec chạy với một loạt thử nghiệm mặc định, ví dụ "Test.host" trên cổng 80.

Bạn nên gọi một hành động mà hiện diện trong bộ điều khiển ứng dụng của bạn và còn sơ khai các máy chủ yêu cầu và cổng như thế này:

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 
Các vấn đề liên quan