我正在测试sinatra非常基础GET
,但我在rspec中遇到了404错误,而且浏览器工作正常。
config.ru
map('/books'){run BooksController} #another application just for books
map('/'){run Api}
Api.rb
class Api < Sinatra::Base end;
BooksController.rb
class BooksController < Api
namespace '/api/v1' do
get '/books' do
"books"
end
end
end
books_controller_spec.rb
RSpec.describe BooksController do
describe "Get Index" do
it "/books" do
get "/books/api/v1/books", {access_token:user1.access_token}
expect(last_response.status).to eq 200
expect(last_response.body).to eq "books"
end
end
end
错误:
1) BooksController Get Index /books
Failure/Error: expect(last_response.status).to eq 200
expected: 200
got: 404
(compared using ==)
但是http://127.0.0.1:9393/books/api/v1/books在我的浏览器中运行良好。
浏览器访问时输出shotgun
(如机架):
127.0.0.1 - - [11/Jan/2016:14:48:05 +0800] "GET /books/api/v1/ HTTP/1.1" 200 6 0.0089
EIDT :当我出去last_request.url
时,我得到了:
"http://example.org/books/api/v1/books"
我认为它是域名,但是当我测试Api
的GET时,我得到了与http://example.org/api/v1.2/events
相同的主机网址,但测试可以通过。
任何帮助,非常感谢!
答案 0 :(得分:0)
您必须在规范中定义一个新的控制器,如下所示:
def app
BooksController.new
end