我正在使用 Rails 4.2.6,Ruby 2.2.1,rspec-rails 3.4.2,Grape 0.16.2 , grape_token_auth 0.1.0 。我为多租户安装了公寓宝石(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 ”。我通过谷歌搜索尝试了很多解决方案。但没有任何作用。您可以在规范中看到我评论这些行。我希望网址为“http://g_m.lvh.me:3000”,其子域名为“g_m”。
我试过这个: 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'
没有任何作用!
我创建了一个测试案例,如下面的葡萄链接所示: 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$
如果有人知道错误以及我在这里做错了什么,请回复/回答。
答案 0 :(得分:0)
错误是因为,我们不得不说公寓子域配置只是排除'www'字作为子域。
添加以下内容:
# config/initializers/apartment/subdomain_exclusions.rb
Apartment::Elevators::Subdomain.excluded_subdomains = ['www']
我在开发和测试环境中使用了 better_errors gem。这导致rspec输出作为更好的错误生成html代码并且难以理解rspec失败的原因。
我从'test'环境中删除了更好的错误,我得到了这个错误的线索,我们必须排除'www'作为子域名。