如何用RSpec测试Rails项目中的Active Admin页面?
用RSpec测试Active Admin的TermsAndConditions页面
针对你基于Ruby 2.6.5、Rails 6.1搭建的Active Admin页面,以下是用RSpec完成测试的具体方案,涵盖系统功能验证和控制器权限/参数校验。
1. 环境准备
- 确保Gemfile包含
rspec-rails、capybara(用于系统测试),若想简化Active Admin测试流程,可额外添加activeadmin-rspecgem - 生成对应测试文件:
- 系统测试文件:
spec/features/active_admin/terms_and_conditions_spec.rb - 控制器测试文件:
spec/controllers/admin/terms_and_conditions_controller_spec.rb
- 系统测试文件:
2. 系统测试(Feature Spec):模拟用户交互
系统测试会模拟真实用户操作,验证页面的显示和交互逻辑。
测试索引页
require 'rails_helper' RSpec.feature "Admin managing TermsAndConditions", type: :feature do let(:admin_user) { create(:admin_user) } # 假设已定义AdminUser模型及测试工厂 let(:test_terms) { create(:bx_block_terms_and_conditions_terms_and_condition, account_id: 1, description: "初始测试条款") } before do login_as(admin_user, scope: :admin_user) test_terms end scenario "访问索引页并验证元素" do visit admin_terms_and_conditions_path expect(page).to have_content("TermsAndConditions") expect(page).to have_column("Account Id") expect(page).to have_column("Description") expect(page).to have_column("Created At") expect(page).to have_column("Updated At") expect(page).to have_content("初始测试条款") expect(page).to have_link("New TermsAndCondition") end
测试创建新条款
scenario "创建新条款成功" do visit admin_terms_and_conditions_path click_link "New TermsAndCondition" fill_in "Conta", with: 2 # 对应form里的label fill_in "Description", with: "新测试条款内容" click_button "Create Terms and condition" expect(page).to have_content("Terms and condition was successfully created") expect(BxBlockTermsAndConditions::TermsAndCondition.last.account_id).to eq(2) expect(BxBlockTermsAndConditions::TermsAndCondition.last.description).to eq("新测试条款内容") end
测试编辑现有条款
scenario "编辑条款并保存" do visit admin_terms_and_conditions_path click_link "Edit", href: edit_admin_terms_and_condition_path(test_terms) fill_in "Description", with: "更新后的条款内容" click_button "Update Terms and condition" expect(page).to have_content("Terms and condition was successfully updated") expect(test_terms.reload.description).to eq("更新后的条款内容") end
测试详情页
scenario "查看条款详情" do visit admin_terms_and_condition_path(test_terms) expect(page).to have_content(test_terms.id) expect(page).to have_content(test_terms.account_id) expect(page).to have_content(test_terms.description) expect(page).to have_content(test_terms.created_at.strftime("%B %d, %Y")) # 匹配页面时间格式 expect(page).to have_content(test_terms.updated_at.strftime("%B %d, %Y")) expect(page).to have_link("Edit") expect(page).to have_link("Back") end
3. 控制器测试:验证权限与参数
测试Active Admin控制器的参数许可、权限控制及业务逻辑。
require 'rails_helper' RSpec.describe Admin::TermsAndConditionsController, type: :controller do let(:admin_user) { create(:admin_user) } let(:valid_params) { { account_id: 3, description: "合法条款内容" } } let(:invalid_params) { { account_id: nil, description: "" } } let(:test_terms) { create(:bx_block_terms_and_conditions_terms_and_condition) } before do sign_in admin_user # Devise登录辅助 end describe "POST #create" do context "参数合法时" do it "创建新的条款记录" do expect { post :create, params: { terms_and_condition: valid_params } }.to change(BxBlockTermsAndConditions::TermsAndCondition, :count).by(1) end it "重定向到新创建的条款详情页" do post :create, params: { terms_and_condition: valid_params } expect(response).to redirect_to(admin_terms_and_condition_path(BxBlockTermsAndConditions::TermsAndCondition.last)) end end context "参数非法时" do it "不创建新记录" do expect { post :create, params: { terms_and_condition: invalid_params } }.not_to change(BxBlockTermsAndConditions::TermsAndCondition, :count) end it "渲染新建页面" do post :create, params: { terms_and_condition: invalid_params } expect(response).to render(:new) end end end describe "PUT #update" do context "参数合法时" do let(:update_params) { { description: "更新后的合法内容" } } it "更新条款记录" do put :update, params: { id: test_terms.id, terms_and_condition: update_params } test_terms.reload expect(test_terms.description).to eq("更新后的合法内容") end it "重定向到条款详情页" do put :update, params: { id: test_terms.id, terms_and_condition: update_params } expect(response).to redirect_to(admin_terms_and_condition_path(test_terms)) end end context "参数非法时" do it "不更新条款记录" do original_desc = test_terms.description put :update, params: { id: test_terms.id, terms_and_condition: invalid_params } test_terms.reload expect(test_terms.description).to eq(original_desc) end it "渲染编辑页面" do put :update, params: { id: test_terms.id, terms_and_condition: invalid_params } expect(response).to render(:edit) end end end
4. 测试注意事项
- 确保测试数据符合模型关联规则(比如
account_id对应的Account记录需提前创建) - 时间格式匹配页面实际渲染结果,避免因格式不一致导致测试失败
- 若有自定义权限控制(如部分管理员无编辑权限),需添加对应测试场景
- 系统测试中使用的
login_as依赖Devise测试辅助,需在rails_helper.rb中引入Devise::Test::IntegrationHelpers
内容的提问来源于stack exchange,提问作者Karina Piloupas
相关产品推荐
相关产品推荐

