【发布时间】:2016-07-01 07:05:41
【问题描述】:
我正在使用 Rails 4.2.6、Ruby 2.2.1、rspec-rails 3.4.2、Grape 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 为“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$
如果有人知道错误以及我在这里做错了什么,请回复/回答。
【问题讨论】:
标签: ruby-on-rails ruby api rspec-rails grape