|
1 | | -# TODO |
2 | | -write guide |
| 1 | +# Introduction |
| 2 | + |
| 3 | +<Bit/> |
| 4 | + |
| 5 | +`Capybara::ActiveAdmin` created to write tests as fast as `ActiveAdmin` resource which they are test. |
| 6 | + |
| 7 | +## Getting Started |
| 8 | + |
| 9 | +### Installation |
| 10 | + |
| 11 | +```ruby |
| 12 | +# Gemfile |
| 13 | + |
| 14 | +gem 'capybara_active_admin', group: :test, require: false |
| 15 | +``` |
| 16 | + |
| 17 | +```ruby |
| 18 | +# spec/rails_helper.rb |
| 19 | + |
| 20 | +# after require 'rspec' or 'rspec/rails' |
| 21 | +require 'capybara/active_admin/rspec' |
| 22 | +``` |
| 23 | + |
| 24 | +### Usage |
| 25 | + |
| 26 | +For example we have such database table |
| 27 | +```ruby |
| 28 | +create_table :users do |t| |
| 29 | + t.string :full_name |
| 30 | + t.timestamps |
| 31 | +end |
| 32 | +``` |
| 33 | +And such `ActiveRecord` model |
| 34 | +```ruby |
| 35 | +class User < ActiveRecord::Base |
| 36 | + validates :full_name, presence: true |
| 37 | +end |
| 38 | +``` |
| 39 | +And there is our `ActiveAdmin` resource |
| 40 | +```ruby |
| 41 | +ActiveAdmin.register User do |
| 42 | + permit_params :full_name |
| 43 | +end |
| 44 | +``` |
| 45 | + |
| 46 | +Our test can look like below |
| 47 | +```ruby |
| 48 | +# spec/system/users_spec.rb |
| 49 | + |
| 50 | +RSpec.describe 'Users' do |
| 51 | + |
| 52 | + describe 'index page' do |
| 53 | + subject do |
| 54 | + visit admin_users_path |
| 55 | + end |
| 56 | + |
| 57 | + it 'have no table' do |
| 58 | + subject |
| 59 | + |
| 60 | + expect(page).to have_text('There are no Users yet.') |
| 61 | + expect(page).to have_action_item('New User') |
| 62 | + expect(page).to_not have_action_item('Edit User') |
| 63 | + expect(page).to_not have_table |
| 64 | + end |
| 65 | + |
| 66 | + context 'with users' do |
| 67 | + let!(:users) do |
| 68 | + [ |
| 69 | + User.create!(full_name: 'John Doe'), |
| 70 | + User.create!(full_name: 'Jane Air') |
| 71 | + ] |
| 72 | + end |
| 73 | + |
| 74 | + it 'have correct table rows' do |
| 75 | + subject |
| 76 | + |
| 77 | + # you can check that table is visible on page |
| 78 | + expect(page).to have_table |
| 79 | + |
| 80 | + # checkout data within table, |
| 81 | + # same as `within(table_selector) do` |
| 82 | + within_table_for do |
| 83 | + # check how many rows in table (tr) |
| 84 | + expect(page).to have_table_row(count: 2) |
| 85 | + # or cells (td) |
| 86 | + expect(page).to have_table_cell(count: 10) |
| 87 | + # or check cell of specific column contain specific value, |
| 88 | + # accepts same options as `have_selector`, such as :text, :exact_text, :count, etc. |
| 89 | + expect(page).to have_table_cell(text: 'John Doe', column: 'Full Name') |
| 90 | + |
| 91 | + # we can check data inside specific row by record id, |
| 92 | + # or we can find it by index in table `within_table_row(index: 0) do`. |
| 93 | + within_table_row(id: users.first.id) do |
| 94 | + # default columns for users table are id, full_name, created_at, updated_at, actions. |
| 95 | + expect(page).to have_table_cell(count: 5) |
| 96 | + expect(page).to have_table_cell(text: 'John Doe') |
| 97 | + # or you can write |
| 98 | + expect(page).to have_table_cell(text: 'John Doe', column: 'Full Name') |
| 99 | + # negate matcher |
| 100 | + expect(page).to_not have_table_cell(text: 'John Doe', column: 'Id') |
| 101 | + end |
| 102 | + |
| 103 | + within_table_row(id: users.second.id) do |
| 104 | + expect(page).to have_table_cell(text: users.second.id, column: 'Id') |
| 105 | + expect(page).to have_table_cell(text: 'Jane Air', column: 'Full Name') |
| 106 | + expect(page).to_not have_table_cell(text: 'John Doe') |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | +end |
| 113 | +``` |
0 commit comments