【问题标题】:Sintara rspec test got 404 when testing subclassing subclassesSintara rspec 测试在测试子类子类时得到 404
【发布时间】:2016-04-15 09:48:12
【问题描述】:

我正在测试非常基础的 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(like rackup)输出:

127.0.0.1 - - [11/Jan/2016:14:48:05 +0800] "GET /books/api/v1/ HTTP/1.1" 200 6 0.0089

编辑:test 环境中的浏览器也可以正常工作。如下:

EIDT:当我输出last_request.url时,我得到了:

 "http://example.org/books/api/v1/books"

我以为是域名,但是当我测试Api 的GET 时,我得到了与http://example.org/api/v1.2/events 相同的主机URL,但测试可以通过。

任何帮助,非常感谢!

【问题讨论】:

  • 它是否出现在您的开发服务器输出中,如果出现,请在此处发布。
  • 已编辑,我从浏览器访问 url 工作正常。
  • 在你的测试中访问它的时候呢?
  • 你也可以发布这个请求吗?
  • 查看上传的图片。

标签: ruby-on-rails rspec sinatra


【解决方案1】:

您必须像这样在规范中定义一个新控制器:

def app
  BooksController.new
end

【讨论】:

    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2012-12-13
    相关资源
    最近更新 更多