2017-07-31 65 views
0

我向我添加了應用程序國家分隔的內容。國家在應用程序中通過子域從IP地址設置。如何逃避rspecs中的重定向

before_action :check_domain 

def check_domain 
    redirect_to(subdomain: current_country.slug) unless Country.find_by(slug: request.subdomain) 
    end 

我已加入這個我application_controller從他們的IP addresess重定向權國家的用戶(如果網址無子域的話)。在工作正常,但我的規格很多,因爲它(沒有重定向通過每個規格)。例如規格:

要求 「rails_helper」

describe "Update user's preferred subjects", type: :request do 
    let!(:video) { create(:video, :with_tags, tags: ["math"]) } 
    let!(:related_video) { create(:video, :with_tags, tags: ["math"], subject: video.subject) } 
    let!(:related_survey) { create(:survey, :with_tags, tags: ["math"], subject: video.subject) } 

    it "responds with related materials" do 
    get("/videos/#{video.id}/related_materials.json") 

    expect(response_body.keys).to match_array(%w(videos offline_materials surveys meta)) 
    expect(response_body).to eq_serialized(
     RelatedMaterialsQuery.new(video, {}), 
     serializer: RelatedMaterialsSerializer 
    ) 
    end 
end 

我得到了一個錯誤:

JSON::ParserError: 
     784: unexpected token at '<html><body>You are being <a href="http://guyana-1.example.com/videos/1/related_materials">redirected</a>.</body></html>' 

我應該在rspec的變化來傳遞呢?它試圖解析html而不是json,因爲它重定向到了國家子域名。我應該如何解決它?謝謝。

+0

請查看https://stackoverflow.com/questions/2556627/rails-rspec-set-subdomain –

回答

1

請求後應使用follow_redirect!解決問題。

describe "Update user's preferred subjects", type: :request do 
    let!(:video) { create(:video, :with_tags, tags: ["math"]) } 
    let!(:related_video) { create(:video, :with_tags, tags: ["math"], subject: video.subject) } 
    let!(:related_survey) { create(:survey, :with_tags, tags: ["math"], subject: video.subject) } 

    before do 
    get("/videos/#{video.id}/related_materials.json") 
    follow_redirect! 
    end 

    it "responds with related materials" do 
    expect(response_body.keys).to match_array(%w(videos offline_materials surveys meta)) 
    expect(response_body).to eq_serialized(
     RelatedMaterialsQuery.new(video, {}), 
     serializer: RelatedMaterialsSerializer 
    ) 
    end 
end 

但我建議測試一次,如果沒有國家的請求被重定向到正確的域。在所有其他測試中,請預先創建一個Country以轉義重定向或簡單存根check_domain方法。

0

您可以手動修改您的request.host以避免重定向。