【问题标题】:Testing Grape with Minitest - What class to inherit from?用 Minitest 测试葡萄 - 继承什么类?
【发布时间】:2015-01-04 21:41:26
【问题描述】:

我的 Rails 应用程序正在使用 minitest。我不清楚我应该为我的测试继承什么类。我在想 ActionController::TestCase 但这似乎不对,因为它没有连接到 Rails 控制器。有什么建议吗?

编辑: 不能使用MiniTest::Unit::TestCase,因为它不包含任何用于测试各个 api 端点的内容。

【问题讨论】:

  • 我花了 30 秒谷歌“minitest”,看起来答案应该是 MiniTest::Unit::TestCase?
  • 对不起,如果我听起来对你居高临下,我不是故意的。你的“api端点”是什么意思?
  • Grape 是 Ruby 用于创建 HTTP API 的库。您可以在一定程度上将其集成到 Rails 中。在编写测试时,您需要在 HTTP 端点上使用 GET、POST。 ActionController::TestCase 为您提供了一个 get 方法来调用控制器和操作,但在这种情况下不起作用。我正在寻找要继承的内容,以便获得 get/post 方法和任何其他 HTTP 帮助程序。
  • 也许尝试从Minitest::Unit::TestCaseincludeing ActiveSupport::TestCase::Behavior::ClassMethods 继承?
  • 这是个好主意。我试试看。

标签: ruby minitest grape grape-api


【解决方案1】:

我没有从特定的 minitest 类继承,而是从 Rack 中包含了测试助手。

class V0::YourAPI < ActiveSupport::TestCase
  include Rack::Test::Methods

  def app
    Rails.application
  end
end

【讨论】:

    【解决方案2】:

    我正在使用ActionDispatch::IntegrationTest

    例如:

    class ApiRequestTest < ActionDispatch::IntegrationTest
     test "sample response" do
      get "/api/v1/sample_request"
      assert_equal response.success?, true
      assert_equal response.code, 200
     end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-06
      • 2013-08-21
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多