我有一个非常具体的问题。 我不想进行控制器测试但是要求测试。而且我不想使用Capybara因为我不想测试用户交互但只想测试响应状态。
我在spec / requests / api / garage_spec.rb
下进行了以下测试require 'spec_helper'
describe "Garages" do
describe "index" do
it "should return status 200" do
get 'http://api.localhost.dev/garages'
response.status.should be(200)
response.body.should_not be_empty
end
end
end
这很有效。但是因为我必须做更多的测试..有没有办法避免重复这个? http://api.localhost.dev
我试过setup { host! 'api.localhost.dev' }
但它没有做任何事情。
before(:each)
阻止@request.host
设置@request
某些内容,当然因为namespace :api, path: '/', constraints: { subdomain: 'api' } do
resources :garages, only: :index
end
在执行任何http请求之前为{0}而崩溃。
以这种方式正确设置路线(实际上它们有效)
{{1}}
答案 0 :(得分:5)
您可以在spec_helper.rb
中创建辅助方法,例如:
def my_get path, *args
get "http://api.localhost.dev/#{path}", *args
end
它的用法是:
require 'spec_helper'
describe "Garages" do
describe "index" do
it "should return status 200" do
my_get 'garages'
response.status.should be(200)
response.body.should_not be_empty
end
end
end
答案 1 :(得分:2)
试试这个:
RSpec.configure do |config|
config.before(:each, type: :api) do |example|
host! 'api.example.com'
end
end
require 'spec_helper'
describe "Garages", type: :api do
describe "index" do
it "should return status 200" do
get 'garages'
response.status.should be(200)
response.body.should_not be_empty
end
end
end