可以单独使用 minitest 来测试 rails 应用程序吗?

Can minitest alone be used to test a rails application?

我是 Ruby 和 Rails 的新手,基本上来自 Java 背景,因此我提前为我对 RoR 的有限知识表示歉意。

Ruby版本=2.1

Rails版本=4.2

在处理现有的 Rails 4 应用程序时,我遇到了在其中一个测试文件中编写的以下测试。此测试试图测试应用程序的登录页面,但我对以下问题感到困惑:

  1. MiniTest 仅用于单元测试或集成测试还是两者兼而有之?
  2. 这个测试实际上是一个 MiniTest 测试,还是它只是在帮助文件中导入现有的默认 Ruby 测试框架,而不是实际使用任何 MiniTest 功能?
  3. MiniTest 能否实际测试 DOM 元素,例如用户输入框中的输入文本、按钮点击等?
  4. 它实际上是通过在内部启动服务器来访问应用程序吗?
  5. 可以单独使用 MiniTest 在无头模式下由 运行 端到端地测试 Rails 应用程序吗?
  6. 是否可以单独使用 MiniTest 通过 selenium 启动浏览器来端到端地测试 Rails 应用程序?我知道水豚可以做到这一点。
  7. 我猜这个测试是 运行 对 localhost。我怎样才能让这个测试指向不同的主机,比如www.dev-server.com

login_test.rb

require 'integration_test_helper'
class LoginTest < ActionDispatch::IntegrationTest

  setup do
    @user = users(:admin)
  end

  test "login page is displayed on root path" do
    delete '/signout'
    get root_path
    assert redirect?
    assert_redirected_to '/signin'
    follow_redirect!
    assert_template :new
    assert_template %r{\Adevise/sessions/new\Z}
    assert_select "a[href=?]", users_path, count: 0
    assert_select "input[id=?]", 'login-email', count: 1
    assert_select "input[id=?]", 'login-password', count: 1
    assert_select "button[id=?]", 'login', count: 1
  end

end

integration_test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use! [Minitest::Reporters::JUnitReporter.new, Minitest::Reporters::SpecReporter.new(:color => true)]

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

 ....


end
  1. Is 'MiniTest` used for just unit testing or integration testing or both?

MiniTest 是一个通用的测试框架,就像 JUnit 或 PHPUnit 或其他任何东西一样。它开箱即用,非常稀疏,但可以针对不同的任务进行扩展。它是一个扳手,你可以用扳手建造火箭飞船或玩具汽车。

  1. Is this test actually a MiniTest test or is it just importing existing default Ruby test framework in the helper file and not actually using any of the MiniTest features?

Rails 测试都建立在 ActiveSupport::TestCase 之上。 ActiveSupport::TestCaseMiniTest 或更旧的 Test::Unit 之上构建到 运行,后者提供 运行 神经元和断言。

  1. Can MiniTest actually test DOM elements such as input text in user input boxes, do button clicks etc?

是也不是。 MiniTest 本身不包含文档解析器或 DOM 模拟器。如果您添加这些 - 是的。

  1. Is it actually hitting the application by starting the server internally?

是的。集成测试实际上使用 HTTP 在您的应用程序上执行请求。

  1. Can MiniTest alone be used to test a Rails application end-to end by running in headless mode?

不,见上文。

  1. Can MiniTest alone be used to test a Rails application end-to end by launching a browser through selenium? I know Capybara can do this.

不,见上文。

  1. I guess this test is running against localhost. How can I make this test point to a different host such www.dev-server.com?

是否可能;是的。好主意?没那么多。

您不想这样做的原因是:

  • 太慢了。
  • 如果不对您的开发服务器进行一些非常不雅的暴露,您将无法正确重置测试之间的状态。

这基本上是您在对您无法控制的服务器进行 javascript 测试时被迫做的事情。那场景太可怕了。

您可能正在寻找的是设置一个 CI 环境,例如 Travis,您可以在其中 运行 进行测试。