rails 应用程序通过
Site
类支持多租户,该类存储主机并将其与租户关联,在 ApplicationController 中的 before_action
回调中调用:
before_action :set_clean_host_site
def set_clean_host_site(request)
rurl = request.host
if request.subdomains.empty?
rurl = 'www.' + rurl
end
clean_host = rurl.chomp("/")
@site = Site.where('host = ?', clean_host.to_s).first
end
控制器测试中应该如何进行调用? 我们可能会调用
individual_url(individual)
操作,但它会假设 url 为 www.example.com
(而不是路径,路径是相对的)。如何为每个操作(或一组操作)设置
request.host
?
您是否也尝试过在进行操作调用之前设置请求对象的主机属性
# test/models/user_test.rb
require 'test_helper'
class YourControllerTest < ActionDispatch::IntegrationTest
setup do
@site = sites(:one) # Assuming you have a sites fixtures
end
test "should get index with custom host" do
host! 'www.adesoji.com'
get your_controller_index_url
assert_response :success
assert_equal 'www.adesoji.com', @controller.request.host
assert_not_nil assigns(:site)
end
end
在这里查看有关模拟的信息:https://semaphoreci.com/community/tutorials/mocking-in-ruby-with-minitest