Skip to content

Instantly share code, notes, and snippets.

@sephraim
Created March 14, 2024 16:30
Show Gist options
  • Save sephraim/203fc65f14c6c746e01993dead2fa357 to your computer and use it in GitHub Desktop.
Save sephraim/203fc65f14c6c746e01993dead2fa357 to your computer and use it in GitHub Desktop.
[Rails view test with RSpec + Capybara]
/ # FILE: views/api/keys/index.html.slim.rb
h1 API Keys
table class="table"
thead
tr
th Key
th User
th Description
th Created
th Expiration
th Actions
tbody
- @api_keys.each do |api_key|
tr
td = api_key.token
td = api_key.description
td = link_to api_key.user.email, edit_admin_user_path(api_key.user)
td = api_key.created_at.strftime("%B %d, %Y %I:%M %p")
td = api_key.expires_at&.strftime("%B %d, %Y %I:%M %p") || 'Never'
td
= link_to 'Delete', api_partner_key_path(api_key), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger'
# FILE: spec/views/api/keys/index.html.slim_spec.rb
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'api/keys/index.html.slim', type: :view do
let(:user1) { create(:user) } # NOTE: API keys are automatically created for new Users
let(:user2) { create(:user) }
let(:api_keys) { ApiKey.all }
before(:example) do
assign(:api_keys, api_keys) # simulates setting @api_keys in the controller
render
end
it 'shows all API keys' do
expect(api_keys).to_not be_empty
api_keys.each do |api_key|
expect(rendered).to have_content(api_key.token)
expect(rendered).to have_content(api_key.description)
expect(rendered).to have_content(api_key.user.email)
expect(rendered).to have_content(api_key.created_at.strftime('%B %d, %Y %I:%M %p'))
expect(rendered).to have_content(api_key.expires_at&.strftime('%B %d, %Y %I:%M %p') || 'Never')
expect(rendered).to have_link('Delete', href: api_partner_key_path(api_key))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment