Rails系统测试 #
一、系统测试概述 #
1.1 创建系统测试 #
bash
rails generate system_test articles
1.2 测试文件 #
ruby
# test/system/articles_test.rb
require 'application_system_test_case'
class ArticlesTest < ApplicationSystemTestCase
test 'creating an article' do
visit articles_url
click_on 'New Article'
fill_in 'Title', with: 'Test Article'
fill_in 'Body', with: 'Test body content'
click_on 'Create Article'
assert_text 'Article was successfully created'
end
end
二、Capybara方法 #
2.1 常用方法 #
ruby
visit articles_url # 访问页面
click_on 'Submit' # 点击
fill_in 'Title', with: 'Test' # 填写
select 'Option', from: 'Select' # 选择
check 'Checkbox' # 勾选
attach_file 'File', 'path' # 上传文件
2.2 断言方法 #
ruby
assert_text 'Hello'
assert_no_text 'Goodbye'
assert_selector 'h1', text: 'Title'
assert_current_path articles_path
三、总结 #
3.1 核心要点 #
| 要点 | 说明 |
|---|---|
| visit | 访问页面 |
| fill_in | 填写表单 |
| click_on | 点击按钮 |
| assert_text | 文本断言 |
3.2 下一步 #
现在你已经掌握了系统测试,接下来让我们学习 部署上线,深入了解Rails的应用部署!
最后更新:2026-03-28