【问题标题】:Using Sinatra with Rspec for test JSON使用带有 Rspec 的 Sinatra 测试 JSON
【发布时间】:2017-06-19 02:37:16
【问题描述】:

我是 Rspec 和 Sinatra 的新手,想问一下如何测试以下内容。我想我已经正确配置了 Sinatra 以与 Rspec 一起工作。我有一个 JSON 正在使用,并且项目正在运行 100%。但是,测试不起作用。一个测试例子:

it "Has response HTTP 200" do
      get "/"
      puts last_response.inspect
      expect(last_response).to have_http_status(:success)
    end

从 .inspect 返回时,结果如下:

<Rack::MockResponse:0x007fbc948c2f50 @original_headers={"Content-Type"=>"text/html;charset=utf-8", "X-Cascade"=>"pass", "Content-Length"=>"459", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "X-Frame-Options"=>"SAMEORIGIN"}, @errors="", @body_string=nil, @status=404, @header={"Content-Type"=>"text/html;charset=utf-8", "X-Cascade"=>"pass", "Content-Length"=>"459", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "X-Frame-Options"=>"SAMEORIGIN"}, @chunked=false, @writer=#<Proc:0x007fbc948c28c0@/Users/eltonsantos/.rvm/gems/ruby-2.3.3@sinatra/gems/rack-1.6.5/lib/rack/response.rb:30 (lambda)>, @block=nil, @length=459, @body=["<!DOCTYPE html>\n<html>\n<head>\n  <style type=\"text/css\">\n  body { text-align:center;font-family:helvetica,arial;font-size:22px;\n    color:#888;margin:20px}\n  #c {margin:0 auto;width:500px;text-align:left}\n  </style>\n</head>\n<body>\n  <h2>Sinatra doesn&rsquo;t know this ditty.</h2>\n  <img src='http://example.org/__sinatra__/404.png'>\n  <div id=\"c\">\n    Try this:\n    <pre>get &#x27;&#x2F;&#x27; do\n  &quot;Hello World&quot;\nend\n</pre>\n  </div>\n</body>\n</html>\n"]>

注意“Sinatra 不知道这个小曲”。用于 Rspec 和 json 的配置 Sinatra 是否正确???说真的,我正在努力,但我不知道该怎么办了:( 没有一项 Rspec 测试有效:( 这是我的存储库,克隆、分叉、提交,任何东西,但请帮助我。

https://github.com/eltonsantos/locaweb-twitter

当我运行 ruby​​ app.rb 时,它可以工作,但 rspec 不起作用。

谢谢!

【问题讨论】:

  • 失败说明了什么?
  • Failure/Error: expect(last_response).to have_http_status(:success) 预期 #<:mockresponse:0x007f9af295f6e8> 响应 has_http_status? # ./spec/locaweb_twitter_spec.rb:20:在 ' 中的 `block (3 个级别)
  • 但我试过了,大多数都没有:(

标签: json ruby testing rspec sinatra


【解决方案1】:

Taryn 的This blog post 可能会有所帮助。她描述了一个带有 RSpec 的 Sinatra 控制器测试序列,大致如下:

describe "GET '/'" do
it "loads homepage" do
  get '/'
  expect(last_response).to be_ok
end

这篇文章有很多细节,包括对模拟返回值的解释。

【讨论】:

    猜你喜欢
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    相关资源
    最近更新 更多