【问题标题】:Sinatra unit test - post with JSON bodySinatra 单元测试 - 使用 JSON 正文发布
【发布时间】:2013-08-26 17:15:38
【问题描述】:

我正在尝试为使用 Sinatra 构建的 REST API 构建单元测试。现在我只想测试我的回声功能是否正常工作。 Echo 使用 POST 并将从该帖子返回完全相同的有效负载。我还是 ruby​​ 的新手,如果我没有使用正确的术语,请原谅我。

这是我要测试的代码:

post '/echo' do
  request.body.read
end

这是我要进行的单元测试:

ENV['RACK_ENV'] = 'test'
require './rest_server'
require 'test/unit'
require 'rack/test'
require 'json'

class RestServer < Test::Unit::TestCase

  def app
    Sinatra::Application
  end

  def test_check_methods
    data = '{"dataIn": "hello"}'
    response = post '/echo', JSON.parse(data)
    assert.last_response.ok?
    assert(response.body == data)
  end
end

使用上面的代码,这里是错误:

NoMethodError: undefined method `dataIn' for Sinatra::Application:Class
    /Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1285:in `block in compile!'
    /Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1285:in `each_pair'
    /Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1285:in `compile!'
    /Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1267:in `route'
    /Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1256:in `post'
    /Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1688:in `block (2 levels) in delegate'
    /Users/barrywilliams/RubymineProjects/project/rest_server_test.rb:20:in `test_check_methods'

如果我尝试在没有 JSON.parse 的情况下这样做,我会得到

NoMethodError: undefined method `key?' for "{\"dataIn\": \"hello\"}":String
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1265:in `route'
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1256:in `post'
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1688:in `block (2 levels) in delegate'
/Users/barrywilliams/RubymineProjects/project/rest_server_test.rb:20:in `test_check_methods'

如果我尝试在 data = 'hello' 的位置执行此操作,则会得到相同的 undefined method 'key?' 错误

我试过这个建议,但没有成功: http://softwareblog.morlok.net/2010/12/18/testing-post-with-racktest/ 我收到一条错误消息,说 post 只需要 2 个参数,而不是 3 个。

因此,总而言之,我需要能够进行调用,让我正在测试的代码接收调用并返回响应,然后我需要能够读取该响应并验证它是原始数据.现在看起来它只是在拨打电话时遇到了困难。

【问题讨论】:

  • 我认为您应该为此进行集成测试而不是单元测试。单元测试用于测试类而不是测试请求。也许这个问题会有所帮助? stackoverflow.com/questions/8013770/…
  • 您是否尝试过仅发布数据结构? post 'echo', {"dataIn" =&gt; "hello"}

标签: ruby unit-testing sinatra rack-test


【解决方案1】:

我做了一个有点类似的事情,它可能对你有帮助:

申请帖子定义:

post '/' do
    data = JSON.parse request.body.read.to_s
    "Hello !\n#{data.to_s}"
end

.to_s 是必需的,否则转换将不完全相同:-/

然后在测试文件上:

class RootPostTest < Test::Unit::TestCase
    include Rack::Test::Methods
    def app
        Sinatra::Application
    end

    def test_return_the_parameters
        data = {
            'reqID' => 1,
            'signedReqID' => "plop",
            'cert' => "mycert"
        }
        post '/', data.to_json, "CONTENT_TYPE" => "application/json"
        assert last_response.ok?
        body_espected = "Hello !\n#{JSON.parse(data.to_json).to_s}"
        assert_equal last_response.body, body_espected
    end
end

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    Rack Test 将返回last_response.body 中的响应正文,无需将其保存到变量中。您也没有回显您发送的内容 - 您提供的代码中的 data 是 JSON,但您将其转换为哈希并发布,因此它与返回的内容不匹配。发送 JSON,或者如果您想在 Sinatra 路由中将其转换为 JSON(请参阅https://stackoverflow.com/a/12138793/335847 了解更多信息)。

    在 Sinatra 应用中:

    require 'json'
    
    post '/echo' do
      # Don't use request.body.read as you're not posting JSON
      params.to_json
    end
    

    在测试文件中:

    def test_check_methods
      data = '{"dataIn": "hello"}'
      post '/echo', JSON.parse(data)
      assert.last_response.ok?
      assert(last_response.body == data)
    end
    

    如果您确实最终想要发布 JSON(我认为这通常不是一个好主意,如果它很容易转换或已经将数据作为哈希)然后使用 :provides =&gt; "json" 作为condition to the route,并考虑使用 Rack::Test::Accepts 让生活更轻松地为此编写测试(注意:这是我写的 gem 的无耻插件;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      相关资源
      最近更新 更多