ruby - Sinatra 单元测试 - 使用 JSON 正文发布

标签 ruby unit-testing sinatra rack-test

我正在尝试为我使用 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 个。

因此,总而言之,我需要能够进行调用,让我正在测试的代码接收调用并返回响应,然后我需要能够读取该响应并验证它是原始数据.现在它似乎卡在了打电话的过程中。

最佳答案

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

应用帖子定义:

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

希望对你有帮助。

关于ruby - Sinatra 单元测试 - 使用 JSON 正文发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18449428/

相关文章:

c++ - Visual Studio 2015 未运行 C++ 单元测试

ruby - 使用 Ruby 在 Elasticsearch 中保存图像

ruby - Sinatra session 成员 "disappearing"

ruby - 通过 ssh 调用远程命令,使用 Ruby,改进语法?

ruby 单元测试 : run some code after each failed test

ruby-on-rails - Rails/在 View 中使用模型

unit-testing - Gradle : Multiple configurations for Test tasks

ruby - block 前的 Sinatra/Rack params[]

ruby - 以特定顺序运行 RSpec 任务

ruby-on-rails - 更新 TopicsController 以允许版主更新主题,但不能创建或删除