rspec-rails with apartment Gem and Grape cannot set host with subdomain

rpsec-rails with apartment Gem and Grape cannot set host with subdomain

我正在使用 Rails 4.2.6,Ruby 2.2.1,rspec-rails 3.4.2,葡萄 0.16.2 grape_token_auth 0.1.0。我为多租户安装了 apartment gem (1.0.2) 并尝试为葡萄请求编写 rspec 测试。

但是我每次都从 rspec-rails 响应方法中得到 跟随错误 ,无论我尝试过什么解决方案。

@buf=["<!DOCTYPE html>\n<html>\n<head>\n    <title>Apartment::TenantNotFound at /content/api/v1/questions</title>\n</head>\n<body>\n

请求一直将主机设为“www.example.com”。我通过谷歌搜索尝试了很多解决方案。但没有任何效果。你可以在规范中看到我评论这些行的地方。我希望 url 作为子域 'g_m' 的“http://g_m.lvh.me:3000”。

我试过这个: https://github.com/influitive/apartment/wiki/Testing-Your-Application 但不起作用。不知道为什么。

我试过了,但没有用: Rails: Wrong hostname for url helpers in rspec

并尝试通过以下方式设置主机:

host! "g_m.lvh.me:3000"
@request.host = 'g_m.lvh.me:3000'
request.host = 'g_m.lvh.me:3000'

没有任何效果!

我创建了一个像下面葡萄这样的测试用例 link 说: https://github.com/dblock/grape/commit/99bf4b44c511541c0e10f4506bf34ae9abcccd75

require 'rails_helper'

RSpec.describe ContentManager::QuestionAPI, :type => :request do
  #before(:each) { Apartment::Tenant.switch!("g_m")  }
  #after(:each) { Apartment::Tenant.switch!("public")  }

  #before(:each) do
   #begin
       #client = FactoryGirl.create(:client, title: 'Sample title',    subdomain: 'g_m')
   #rescue
     #client = Client.create!(title: 'Sample title',   subdomain: 'g_m')
   #end

   #default_url_options[:host] = 'http://g_m.lvh.me:3000'
   # request.host = "#{'g_m'}.lvh.me"
   #end
 #end

 #before(:each) do
 #  if respond_to?(:default_url_options)
 #    default_url_options[:host] = 'http://g_m.lvh.me:3000'
 #  end
 #end

  describe "GET /content/api/v1/questions" do
    it "returns an empty array of questions" do
    get "/content/api/v1/questions"
    #puts "response.inspect: #{response.inspect}"
    response.status.should == 200
    JSON.parse(response.body).should == []
   end
 end
end

我的配置:

in spec/rails_helper.rb

config.include RSpec::Rails::RequestExampleGroup, type: :request, file_path: /spec\/requests/

此请求一直由 rspec-rails 与

发送
url: '/content/api/v1/questions'
host: 'www.example.com'

我的测试结果显示:

$ rspec spec/requests/

F

Failures:

1) ContentManager::QuestionAPI GET /content/api/v1/questions returns an empty array of questions
  Failure/Error: response.status.should == 200

   expected: 200
        got: 500 (using ==)
 # ./spec/requests/question_spec.rb:30:in `block (3 levels) in <top (required)>'

Deprecation Warnings:

Using `should` from rspec-expectations' old `:should` syntax without      explicitly enabling the syntax is deprecated. Use the new `:expect` syntax     or explicitly enable `:should` with `config.expect_with(:rspec) { |c| c.syntax = :should }` instead. Called from /home/vagrant/gauge-slcsl/spec/requests/question_spec.rb:30:in `block (3 levels) in <top (required)>'.


If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.

1 deprecation warning total

Finished in 6.19 seconds (files took 1.93 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/requests/question_spec.rb:26 # ContentManager::QuestionAPI GET /content/api/v1/questions returns an empty array of questions

abhi@ubuntu-trusty-64:~/my-app$

如果有人知道错误以及我在这里做错了什么,请reply/answer。

错误是因为,我们不得不说公寓子域配置只是 排除了 'www' 作为子域考虑的词

添加以下内容:

# config/initializers/apartment/subdomain_exclusions.rb
Apartment::Elevators::Subdomain.excluded_subdomains = ['www']

我在我的开发和测试环境中使用 better_errors gem。这导致 rspec 输出作为更好的错误生成 html 代码并且难以理解 rspec 失败原因。

我从 'test' 环境中删除了更好的错误,我得到了这个错误的线索,我们必须排除 'www' 考虑作为子域。