You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Ruby on Rails的ActiveAdmin后台配置编写RSpec测试?

编写ActiveAdmin Settings后台的RSpec测试

首先确保你的Gemfile的test组包含rspec-activeadmin:

group :test do
  gem 'rspec-rails'
  gem 'rspec-activeadmin'
  # 其他测试依赖(如capybara、factory_bot_rails等)
end

执行bundle install完成依赖安装,然后创建测试文件spec/admin/settings_spec.rb,以下是针对你的后台配置的完整测试用例:


测试代码完整示例

require 'rails_helper'

RSpec.describe 'Admin Settings', type: :feature do
  # 假设你已用FactoryBot定义了对应的工厂
  let(:admin_user) { create(:admin_user) }
  let(:coin_setting) { create(:bx_block_cfappcoinsmanagement_coin_setting, 
                              single_coin_worth: 0.000000123456789, 
                              transaction_type: 'purchase') }

  # 所有测试前先登录管理员
  before do
    login_as(admin_user, scope: :admin_user)
  end

  # 列表页测试
  describe 'index page' do
    before do
      coin_setting
      visit admin_settings_path
    end

    it '显示正确的列' do
      expect(page).to have_content('ID')
      expect(page).to have_content('Transaction Type')
      expect(page).to have_content('Single Coin Worth($)')
    end

    it '正确格式化single_coin_worth数值' do
      expect(page).to have_content('0.000000123456789')
    end

    it '显示指定的操作按钮' do
      expect(page).to have_link('Show', href: admin_setting_path(coin_setting))
      expect(page).to have_link('Edit', href: edit_admin_setting_path(coin_setting))
      expect(page).not_to have_link('Delete') # 匹配你禁用默认删除按钮的配置
    end
  end

  # 详情页测试
  describe 'show page' do
    before do
      visit admin_setting_path(coin_setting)
    end

    it '显示所有必填字段及正确内容' do
      expect(page).to have_content('single_coin_worth($)')
      expect(page).to have_content('0.000000123456789')
      expect(page).to have_content('created_at')
      expect(page).to have_content('updated_at')
      expect(page).to have_content('purchase')
    end
  end

  # 表单(创建/编辑)测试
  describe 'form' do
    context '创建新配置' do
      before do
        visit new_admin_setting_path
      end

      it '包含指定字段及验证规则' do
        expect(page).to have_field('Single coin worth', required: true)
        expect(page).to have_select('Transaction type', options: ['purchase', 'withdrawl'])
      end

      it '校验single_coin_worth的格式合法性' do
        fill_in 'Single coin worth', with: 'abc'
        click_button 'Create Setting'
        expect(page).to have_content('Only digits and decimal point are allowed')
      end

      it '使用合法数据创建配置成功' do
        fill_in 'Single coin worth', with: '100.12345'
        select 'withdrawl', from: 'Transaction type'
        click_button 'Create Setting'

        expect(page).to have_content('Setting was successfully created')
        latest_setting = BxBlockCfappcoinsmanagement::CoinSetting.last
        expect(latest_setting.single_coin_worth).to eq(100.12345)
        expect(latest_setting.transaction_type).to eq('withdrawl')
      end
    end

    context '编辑已有配置' do
      before do
        visit edit_admin_setting_path(coin_setting)
      end

      it '更新配置数据成功' do
        fill_in 'Single coin worth', with: '0.987654321'
        select 'withdrawl', from: 'Transaction type'
        click_button 'Update Setting'

        expect(page).to have_content('Setting was successfully updated')
        coin_setting.reload
        expect(coin_setting.single_coin_worth).to eq(0.987654321)
        expect(coin_setting.transaction_type).to eq('withdrawl')
      end
    end
  end

  # 参数许可测试
  describe 'permitted parameters' do
    it '仅允许指定参数被提交' do
      params = {
        bx_block_cfappcoinsmanagement_coin_setting: {
          single_coin_worth: '50.0',
          transaction_type: 'purchase',
          unwanted_param: 'should_not_be_permitted' # 测试不允许的参数
        }
      }

      expect_any_instance_of(ActiveAdmin::ResourceController).to receive(:permitted_params).and_return(
        params[:bx_block_cfappcoinsmanagement_coin_setting].slice(:single_coin_worth, :transaction_type)
      )

      post admin_settings_path, params: params
      setting = BxBlockCfappcoinsmanagement::CoinSetting.last
      expect(setting.unwanted_param).to be_nil
    end
  end
end

关键注意事项

  1. 确保你已用FactoryBot定义了AdminUser和BxBlockCfappcoinsmanagement::CoinSetting的工厂类,否则需要手动在测试中创建测试数据。
  2. 若要测试前端的格式校验(如input的pattern属性),需要启用Capybara的JavaScript驱动(如selenium)。
  3. 数值格式化的断言需与number_with_precision的输出严格匹配,避免因格式化规则导致断言失败。

内容的提问来源于stack exchange,提问作者Amit K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 04:12:28