The AAA Pattern: Writing Robust Tests for Any Project with Confidence

The AAA Pattern: Writing Robust Tests for Any Project with Confidence

Are you looking for a reliable way to test your applications? Look no further than the AAA pattern.

The AAA pattern stands for:

  1. Arrange
  2. Act
  3. Assert

This pattern helps you structure your tests in a clear consistent manner, and it is not tied to a particular programming language or testing tool, making it a versatile and effective approach to testing opens a new window .

Following this pattern and structuring your tests into three distinct sections, you can easily set up the necessary conditions, perform the actions you want to test, and then verify the expected results.

In simple terms, a test is a self-explanatory piece of code that checks whether your application is functioning as intended.

Automated Tests vs. Manual Tests (QA)

There are two main ways to conduct tests: manual testing or automated testing. While manual testing can be useful, it can also be susceptible to human errors, it takes time, and it can delay the deployment process.

Automated testing can be very helpful for quickly validating the behavior of your code.

Automated testing involves running code that automatically checks your application for errors or bugs.

Testing in Rails

In Rails opens a new window , there are many different types of tests you can perform, such as controller, route, view, request, or feature tests.

The key is to choose the test that best fits your requirements.

A Few Simple Examples

For example, let’s say you want to test that your app renders the /team page with specific content taken from the database.

In this scenario, a feature test may be the most appropriate.

It’s also important to choose the right testing tool for your needs.

Some popular options for Ruby applications include RSpec opens a new window , Minitest opens a new window , and Capybara opens a new window .

However, the focus of this article is not on the specific tool you can use, but rather on the AAA pattern.

Here are some examples of how to use the AAA pattern in Ruby opens a new window code:

Example 1: Testing with RSpec

# Example 1: Testing a controller action with RSpec
describe UsersController do
  describe "GET #index" do
    it "assigns @users" do
      # Arrange
      user1 = User.create(name: "Alice")
      user2 = User.create(name: "Bob")

      # Act
      get :index

      # Assert
      expect(assigns(:users)).to match_array([user1, user2])

In example #1, we’re testing the UsersController#index action with RSpec.

  • First, we arrange the necessary data by creating two User objects
  • Then, we act on the code by making a GET request to the index action
  • Finally, we assert that the @users instance variable is assigned correctly by checking that it contains both user1 and user2

Example 2: Testing with Minitest

# Example 2: Testing a model validation with Minitest
class UserTest < ActiveSupport::TestCase
  test "should not save user without name" do
    # Arrange
    user =

    # Act

    # Assert
    assert_not user.valid?
    assert_equal ["can't be blank"], user.errors[:name]

In example #2, we’re testing a model validation for the User model with Minitest.

  • First, we arrange the necessary data by creating a new User object without a name
  • Then, we act on the code by trying to save the user to the database
  • Finally, we assert that the user is not valid and that the error message for the name attribute is correct

Example 3: Testing with Capybara

# Example 3: Testing a feature with Capybara
feature "User sign in" do
    # Arrange
    User.create(email: "", password: "password")

  scenario "with valid credentials" do
    # Act
    visit "/login"
    fill_in "Email", with: ""
    fill_in "Password", with: "password"
    click_button "Sign in"

    # Assert
    expect(page).to have_content "Signed in successfully."

In example #3, we’re testing a feature test with Capybara:

  • First, we arrange the necessary data in a before block
  • Then, we act on the code by filling the form out
  • Finally, we assert that the user signed in successfully


In conclusion, the AAA pattern opens a new window is a powerful technique for ensuring that your code works as intended through automated testing. Because it’s programming language agnostic, testing tools, or testing type, it can be used in virtually any project.

Whether you’re new to automated testing or a seasoned pro, incorporating the AAA pattern into your testing strategy can help you write more robust and reliable tests.

Next time you’re writing tests for your application, give the AAA pattern a try and see how it can make your testing process more efficient and effective.

Interested in other design patterns? Check out our series of articles on design patterns opens a new window in the OmbuLabs blog opens a new window .

Need help increasing your code coverage percentage? Contact us for a quick test suite checkup opens a new window . 🚀

Get the book