- Read Tutorial
- Watch Guide Video
In this guide we are going to build out the audit log dashboard.
Go to features
and create a new file called audit_log_spec.rb
. In this file, we are going to require rails_helper
, and we're going to call the first describe
block as "AuditLog feature."Before we build the test cases, let's create an audit log in our let
callback method, so we have access to it throughout the file.
Let's start by creating a test case that checks if the index
page exists at all.
# spec/features/audit_log_spec.rb require 'rails_helper' describe 'AuditLog Feature' do let(:audit_log) { FactoryGirl.create(:audit_log) } describe 'index' do it 'has an index page that can be reached' do visit audit_logs_path expect(page.status_code).to eq(200) end end end
rspec
fails as expected, and to fix it, open audit_logs_controller.rb
.
Let's also open routes.rb
, and here, I'm not comfortable that all the routes for audit log is available because we are never going to need new
, edit
and destroy
. So, let's fix that first with except
, like this:
# config/routes.rb Rails.application.routes.draw do resources :audit_logs, except: [:new, :edit, :destroy]
Now, move to audit_logs_controller.rb
, and create a method called index
.
# app/controllers/audit_logs_controller.rb class AuditLogsController < ApplicationController def index end end
Next, we'll create an index file. Go to views/auditlogs/
and create a file called index.html.erb
.
If you run rspec
now, everything should pass.
Let's continue with other tests. The next one should check if there is content in the audit logs.
# spec/features/audit_log_spec.rb it 'renders audit log content' do visit audit_logs_path expect(page).to have_content(/SNOW/) end
Now if you run rspec
we should have a failure. To fix this, open audit_logs_controller.rb
and add this code:
# app/controllers/audit_logs_controller.rb class AuditLogsController < ApplicationController def index @audit_logs = AuditLog.all end end
Also, go to index.html.erb
, and add the inspect
method.
<!-- app/views/audit_logs/index.html.erb --> <%= @audit_logs.inspect %>
If you run rspec
, it fails again though I thought it would pass. Let's see what happened. The full_name
method that we are checking will not be available in audit_logs
, and this means, we have to build out the full functionality to make it more explicit. Create a partial called _audit_log.html.erb
, to put it in our table logic.
For now though, let's get our test to pass with just the full name in _audit_log.html.erb
:
<!-- app/views/audit_logs/_audit_log.html.erb --> <%= audit_log.user.full_name %><br>
In index.html.erb
, we are just calling this partial with the code:
<!-- app/views/audit_logs/index.html.erb --> <%= render @audit_logs %>
These changes still didn't pass the test, and this is because the user is not logged in. So, let's log the user in a before
block.
# spec/features/audit_log_spec.rb describe 'index' do before do admin_user = FactoryGirl.create(:admin_user) login_as(admin_user, :scope => :user) end
This doesn't pass the test either. Let's open the browser and check our functionality. If you click on audit log tab, you'll see that it is printing the values.
The issue was because I was not calling the let
method. So, you can create an audit log inside our test and remove let
or simply call the audit_log
that was defined in the let
statement.
# spec/features/audit_log_spec.rb it 'renders audit log content' do FactoryGirl.create(:audit_log) visit audit_logs_path expect(page).to have_content(/SNOW/) end
If you run the tests you'll see that the tests are now passing.