应用程序测试全攻略
立即解锁
发布时间: 2025-08-20 01:21:02 阅读量: 1 订阅数: 3 


Rails 3入门:从零开始构建Web应用
### 应用程序测试全攻略
在开发过程中,对应用程序进行全面测试是确保其稳定性和功能性的关键步骤。本文将详细介绍如何对文章模型(Article Model)进行单元测试,以及如何对文章控制器(Articles Controller)进行功能测试。
#### 文章模型测试
首先,我们来测试文章模型的基本 CRUD(创建、读取、更新、删除)操作。在开始测试之前,需要创建一些测试数据的固定装置(fixtures)。
##### 创建固定装置
打开 `test/fixtures/articles.yml` 文件,将其内容替换为以下代码:
```yaml
welcome_to_rails:
user: eugene
title: "Welcome to Rails"
body: "Rails is such a nice web framework written in Ruby"
published_at: <%= 3.days.ago %>
```
这些固定装置的数据会在测试运行前自动插入到测试数据库中。需要注意的是,固定装置在加载前会由 ERb 解析,因此可以像在视图模板中一样使用 ERb,例如 `published_at: <%= 3.days.ago %>` 用于创建动态日期。
##### 添加创建测试
打开 `test/unit/article_test.rb` 文件,删除 `test "the truth"` 方法,并将其替换为 `test "should create article"` 测试方法。文件内容如下:
```ruby
require 'test_helper'
class ArticleTest < ActiveSupport::TestCase
test "should create article" do
article = Article.new
article.user = users(:eugene)
article.title = "Test article"
article.body = "Test body"
assert article.save
end
end
```
这个测试方法模拟了从控制台创建新文章的过程,最后使用 `assert article.save` 来确保文章成功保存。固定装置可以在测试用例中通过名称访问,例如 `users(:eugene)` 可以获取到 `eugene` 用户的固定装置数据。
##### 断言测试
在进行测试时,断言(assertions)是表达预期结果的语句。Test::Unit 自带了许多内置断言,Rails 也添加了一些自定义断言。以下是标准的 Test::Unit 断言列表:
- `assert(boolean, message=nil)`
- `assert_block(message="assert_block failed.") do ... end`
- `assert_equal(expected, actual, message=nil)`
- `assert_in_delta(expected_float, actual_float, delta, message="")`
- `assert_instance_of(klass, object, message="")`
- `assert_kind_of(klass, object, message="")`
- `assert_match(pattern, string, message="")`
- `assert_nil(object, message="")`
- `assert_no_match(regexp, string, message="")`
- `assert_not_equal(expected, actual, message="")`
- `assert_not_nil(object, message="")`
- `assert_not_same(expected, actual, message="")`
- `assert_nothing_raised(*args) do ... end`
- `assert_nothing_thrown(message="") do ... end`
- `assert_operator(object1, operator, object2, message="")`
- `assert_raise(expected_exception_klass, message="") do ... end`
- `assert_respond_to(object, method, message="")`
- `assert_same(expected, actual, message="")`
- `assert_send(send_array, message="")`
- `assert_throws(expected_symbol, message="") do ... end`
其中,`assert` 方法是最基本的断言,它断言其第一个参数的返回值为 `true`。通过 `assert article.save`,可以成功测试文章是否保存成功。
运行创建测试的命令如下:
```bash
$ ruby -Itest test/unit/article_test.rb
```
这里的 `-Itest` 参数确保 Ruby 在运行 `article_test.rb` 文件时包含 `test` 目录,因为 Rails 提供了 `test/test_helper.rb` 文件,所有测试都需要包含该文件。当使用 `rake` 命令运行测试时,`rake` 会自动添加 `test/test_helper.rb`。
##### 添加查找测试
在 `test/unit/article_test.rb` 文件中,添加 `test "should find article"` 测试方法:
```ruby
require 'test_helper'
class ArticleTest < ActiveSupport::TestCase
test "should create article" do
article = Article.new
article.user = users(:eugene)
article.title = "Test article"
article.body = "Test body"
assert article.save
end
test "should find article" do
article_id = articles(:welcome_to_rails).id
assert_nothing_raised { Article.find(article_id) }
end
end
```
这个测试方法首先从固定装置中获取文章的 `id`,然后使用 `Article.find` 方法尝试查找该文章。使用 `assert_nothing_raised` 断言来确保查找过程中没有抛出异常。
运行查找测试的命令与创建测试相同,运行后如果没有异常抛出,则说明查找功能正常。
##### 添加更新测试
在 `test/unit/article_test.rb` 文件中,添加 `test "should update article"` 测试方法:
```ruby
require 'test_helper'
class ArticleTest < ActiveSupport::TestCase
test "should create article" do
article = Article.new
article.user = users(:eugene)
article.title = "Test article"
article.body = "Test body"
assert article.save
end
test "should find article" do
article_id = articles(:welcome_to_rails).id
assert_nothing_raised { Article.find(article_id) }
end
test "should update article" do
article = articles(:welcome_to_rails)
assert article.update_attributes(:title => 'New title')
end
end
```
这个测试方法从固定装置中获取文章,然后使用 `update_attributes` 方法更新文章的标题,并使用 `assert` 断言确保更新操作成功。
运行更新测试的命令与前面相同,运行后如果更新操作成功,则说明更新功能正常。
##### 添加删除测试
在 `test/unit/article_test.rb` 文件中,添加 `test "should destroy article"` 测试方法:
```ruby
require 'test_helper'
class ArticleTest < ActiveSupport::TestCase
test "should create article" do
article = Article.new
article.user = users(:eugene)
article.title = "Test article"
article.body = "Test body"
assert article.save
end
test "should find article" do
article_id = articles(:welcome_to_rails).id
assert_nothing_raised { Article.find(article_id) }
end
test "should update article" do
article = articles(:welcome_to_rails)
assert article.update_attributes(:title => 'New title')
end
test "should destroy article" do
article = articles(:welcome_to_rails)
article.destroy
assert_raise(ActiveRecord::RecordNotFound) { Article.find(article.id) }
end
end
```
这个测试方法从固定装置中获取文章,然后使用 `destroy` 方法删除文章。接着使用 `assert_raise` 断言确保尝试查找已删除的文章时会抛出 `ActiveRecord::RecordNotFound` 异常。
运行删除测试的命令与前面相同,运行后如果抛出预期的异常,则说明删除功能正常。
##### 测
0
0
复制全文
相关推荐










