Ruby on rails 如何在rspecs中逃避重定向
我向我添加了应用程序国家/地区分隔的内容。国家/地区根据应用程序中的子域从IP地址设置Ruby on rails 如何在rspecs中逃避重定向,ruby-on-rails,ruby,redirect,rspec,Ruby On Rails,Ruby,Redirect,Rspec,我向我添加了应用程序国家/地区分隔的内容。国家/地区根据应用程序中的子域从IP地址设置 before_action :check_domain def check_domain redirect_to(subdomain: current_country.slug) unless Country.find_by(slug: request.subdomain) end 我已经为我的应用程序_控制器添加了此项,以便将用户从其IP地址重定向到正确的国家(如果url已经没有子域)。在工
before_action :check_domain
def check_domain
redirect_to(subdomain: current_country.slug) unless Country.find_by(slug: request.subdomain)
end
我已经为我的应用程序_控制器添加了此项,以便将用户从其IP地址重定向到正确的国家(如果url已经没有子域)。在工作很好,但我的很多规格错误,因为它(没有重定向每个规格通过)。例如,规范:
需要“rails\u助手”
我有一个错误:
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>'
JSON::ParserError:
784:在“您正在”处出现意外标记
我应该在rspec中更改什么才能通过它?它试图解析html而不是json,因为它重定向到了国家子域。我该如何解决它?谢谢。使用
跟随重定向请求后的代码>应修复该问题
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
但是如果没有国家,我建议测试一下请求是否被重定向到正确的域。在所有其他测试中,预先创建一个国家
以逃避重定向,或者简单地存根check\u domain
方法。您可以手动修改您的请求。主机
以避免重定向。检查此项
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