Rails 测试期间发生了什么?

发布于 2024-12-01 23:11:47 字数 254 浏览 0 评论 0原文

我是 Ruby On Rails 的新手。我喜欢它,它内置了测试功能。但是,我无法全神贯注于测试。这是我关于它的第一个基本问题。

测试过程中到底发生了什么?

我了解开发,我们想要一些结果,我们使用我们拥有的数据或从用户那里获得的数据来实现我们想要的最终结果。但是,测试的概念有时似乎让我感到困惑。我已经在浏览器中测试应用程序一段时间了,我们是否用代码复制相同的内容?这是测试的目的吗?使用自动化代码复制浏览器测试?在此启发我。

I'm new to Ruby On Rails. I love, it has Testing capabilities built in. But, I can't wrap around my head with testing. Here is my first basic Question about it.

What happens during testing really?

I understand development, we want some result, we use the data we have or get it from users to achieve the end result we want. But, the notion of testing seems sometimes confusing for me. I have been testing applications in browser for some time, are we replicating the same with code? Is it what testing is about? Replicating browser testing with automated code? Enlighten Me here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

思慕 2024-12-08 23:11:47

阅读测试 Rails 应用程序指南将是一个很好的起点。

基本上,您有三种类型的测试:单元功能集成

单元测试正在测试您的模型。在这些测试中,您检查模型的单个方法是否按预期工作,例如您设置分配带有空格的登录名,然后测试空格是否被删除:

class UserTest < ActiveSupport::TestCase
  def test_login_cleaning
    u = User.new
    u.login = "  login_with_spaces  "
    assert_equal "login_with_spaces", u.login
  end
  # ... and other tests
end

功能测试正在测试您的控制器(和意见)。在每个测试中,您使用一组给定的参数模拟发送到一个控制器的一个请求,然后确保控制器返回正确的响应。

但请注意,在此测试中您无法测试页面的渲染,因此它并不是严格模拟浏览器。要测试您的页面是否美观,您需要手动执行此操作(我几乎确定存在一些技术,但我不知道它们)

功能测试的一个示例:

class UserControllerTest < ActionController::TestCase
  def test_show_renders_admin
    get :show, :id => 1
    assert_response :success
    assert_select "div.user" do
      assert_select "span.name", "Joe Admin"
    end
  end
  def test_show_handles_unknown_id
    get :show, :id => 9999
    assert_response 404
    assert_select "p.warning", "No such user"
  end
end

集成测试正在测试一系列请求 - 类似于用户登录、获取“创建用户”页面、创建用户等场景。这些测试检查单个请求(在功能测试中测试)是否能够协同工作。


我看到 Simone 已经指出了测试中自动化的重要性,因此指南的链接是我的答案中唯一的价值;-)

您可能会发现应用 测试驱动开发,尤其是当您的项目稍微成熟时。

我知道通过编写测试启动项目并不容易,因为通常你还不知道一切将如何工作,但是稍后,当你发现错误时,我强烈建议启动修复编写失败的测试用例中的每个错误。它确实非常有助于修复错误阶段以及之后的工作——确保错误不会再次出现。


好吧,我注意到我没有直接回答你的问题;-)

当你开始测试过程时,Rails:

  • 删除测试数据库(所以确保你这里没有任何有价值的数据),
  • 使用开发数据库的结构重新创建它(因此,请确保您已运行所有迁移),
  • 加载所有装置(来自 test/fixtures/*),
  • 加载来自 test/units/* 的所有测试类和其他目录,
  • 调用其名称的每个方法以 'test_' 开头,或者由宏 test "should Something.." 创建(按字母顺序排列,但您可能认为顺序是随机的),
  • 在每次调用之前它都会执行特殊的 setup< /code> 过程,每次调用后它都会执行 teardown 过程,
  • 在每次调用之前它可能(取决于配置)重新创建数据库数据,再次加载灯具。

您将在指南中找到更多信息。

Reading A Guide to Testing Rails Applications will be a good starting point.

Basically, you have three kinds of tests: unit, functional and integration.

Unit tests are testing your Models. In these tests you check whether a single method of your model works as expected, for example you set assign a login with spaces, and then you test whether the spaces were removed:

class UserTest < ActiveSupport::TestCase
  def test_login_cleaning
    u = User.new
    u.login = "  login_with_spaces  "
    assert_equal "login_with_spaces", u.login
  end
  # ... and other tests
end

Functional tests are testing your controllers (and views). In each test you simulate one request sent to one controller with given set of parameters, and then you ensure that the controller returned the proper response.

Note however, that in this test you cannot test the rendering of the page, so it's not strictly simulating a browser. To test whether your page looks nicely, you need to do it manually (I am almost sure some techniques exist, but I do not know of them).

An example of functional test:

class UserControllerTest < ActionController::TestCase
  def test_show_renders_admin
    get :show, :id => 1
    assert_response :success
    assert_select "div.user" do
      assert_select "span.name", "Joe Admin"
    end
  end
  def test_show_handles_unknown_id
    get :show, :id => 9999
    assert_response 404
    assert_select "p.warning", "No such user"
  end
end

Integration tests are testing a sequence of requests - something like a scenario, where an user logins, gets the 'create user' page, creates an user, and so on. These tests check whether the single requests (tested in functional tests) are able to work together.


I see that Simone already pointed the importance of automation in tests, so the link to the Guide is the only value in my answer ;-)

You may find it very helpful to apply some rules of Test Driven Development, especially when your project matures a little.

I know that it's not easy to start the project by writing test, because often you do not yet know how everything will work, but later, when you find a bug, I strongly suggest to start fixing every bug from writing a failing test case. It really, really helps both in the bug-fixing phase, and later - ensuring that the bug does not reappear.


Well, I noticed that I did not directly answer your question ;-)

When you start test procedure, Rails:

  • deletes the test database (so make sure you do not have any valuable data here),
  • recreates it using the structure of the development database (so, make sure you have run all your migrations),
  • loads all the fixtures (from test/fixtures/*)
  • loads all the test classes from test/units/* and other directories,
  • calls every method whose name starts with 'test_' or was created by the macro test "should something.." (alphabetically, but you may consider the order as being random)
  • before every call it executes a special setup procedure, and after every call it executes teardown procedure,
  • before every call it may (depending on the configuration) recreate your database data, loading the fixtures again.

You will find more information in the Guide.

清欢 2024-12-08 23:11:47

测试期间发生的情况是,您实际上运行一组专门的程序或例程(测试代码),它们调用应用程序中的例程(被测代码)并验证它们是否产生预期结果。测试框架通常具有某种机制来确保每个测试例程独立于其他测试。换句话说,一项测试的结果不会影响其他测试的结果。

具体来说,在 Rails 中,您可以使用 rake test 命令行工具运行测试。这将以随机顺序加载并执行每个测试例程,并告诉您每个测试是否成功。

What happens during testing is that you really run a set of specialized programs or routines (test code) that calls routines in your application (code under test) and verifies that they produce the expected results. The testing framework usually has some mechanism to make sure that each test routine is independent of the other tests. In other words the result from one test does not affect the result of the others.

In Rails specifically you run the tests using the rake test command line tool. This will load and execute each test routine in a random order, and tell you if each test was successful or not.

我不咬妳我踢妳 2024-12-08 23:11:47

这个答案不一定适用于 Rails 本身。当您谈论 Rails 中的测试时,通常指的是自动测试。

自动这个词的本质就是这个意思。这实际上是单元测试和“浏览器”测试之间最大的区别。

通过单元测试,您本质上是编写一段代码,一个例程,强调代码的特定部分,以确保其按预期工作。与“浏览器”测试相比,单元测试的主要优点是:

  1. 它是自动的并且可以以编程方式运行。
  2. 您的测试套件在开发生命周期中不断增加。
  3. 您可以降低回归错误的风险,因为当您修改一段代码并运行测试套件时,您实际上正在运行所有测试,而不仅仅是随机检查。

这是一个非常简单的基本示例。采取一个模型,假设是用户模型。您具有以下属性:first_namelast_name。您需要一个名为 name 的方法来返回名字和姓氏(如果存在)。

这是方法

class User
  def name
    [first_name, last_name].reject(&:blank?).join(" ")
  end
end

,这是相应的单元测试。

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  def test_name
    assert_equal "John Doe", User.new(:first_name => "John", :last_name => "Doe").name
    assert_equal "John", User.new(:first_name => "John").name
    assert_equal "Doe", User.new(:last_name => "Doe").name
    assert_equal "", User.new().name
  end
end

This answer doesn't necessary apply to Rails itself. When you talk about testing in Rails, you usually mean automatic testing.

The word automatic is the essence of the meaning. This is in fact the biggest difference between unit testing and "browser" testing.

With unit testing you essentially write a code, a routine, that stresses a specific portion of your code to make sure it works as expected. The main advantages of unit testing compared to "browser" testing are:

  1. It's automatic and can be run programmatically.
  2. Your test suite increases during the development lifecycle.
  3. You reduce the risk of regression bugs, because when you modify a piece of code and you run the test suite, you are actually running all the tests, not just a random check.

Here's a basic, very simple example. Take a model, let's say the User model. You have the following attributes: first_name, last_name. You want a method called name to return the first and last name, if they exist.

Here's the method

class User
  def name
    [first_name, last_name].reject(&:blank?).join(" ")
  end
end

and here's the corresponding unit test.

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  def test_name
    assert_equal "John Doe", User.new(:first_name => "John", :last_name => "Doe").name
    assert_equal "John", User.new(:first_name => "John").name
    assert_equal "Doe", User.new(:last_name => "Doe").name
    assert_equal "", User.new().name
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文