Testing
Running automated tests with Dynamoid requires configuring a dedicated test environment—typically targeting a local emulator with an isolated table namespace—guarding against accidental changes to remote or non-test tables, and managing table lifecycles and item cleanup without database transactions.
Configuration
When running tests against a local emulator (such as DynamoDB Local or LocalStack), configure config.endpoint in your test setup (such as config/environments/test.rb, spec/support/dynamoid.rb, or test/test_helper.rb) to route requests to the local service instead of AWS:
Dynamoid.configure do |config|
config.endpoint = 'http://localhost:8000'
end
Because the AWS SDK requires a region and credentials even when connecting to a local emulator, configure dummy values if they are not already present in the environment (such as in CI containers):
config.region = 'us-west-2'
config.access_key = 'test'
config.secret_key = 'test'
To prevent test runs from overwriting or dropping development tables on the same emulator, configure a dedicated config.namespace for the test environment. When running tests in parallel across multiple processes, append an environment variable containing a unique worker identifier (such as ENV['TEST_ENV_NUMBER']) so each process operates on its own tables:
config.namespace = "myapp_test#{ENV['TEST_ENV_NUMBER']}"
Outside Rails (where Dynamoid uses Rails.logger), config.logger defaults to $stdout at Logger::INFO level, printing every database operation into the test output. To silence routine logs while keeping warnings and errors visible, raise the log level:
config.logger.level = Logger::WARN
Alternatively, redirect logs to a file or disable logging:
config.logger = Logger.new('log/test.log')
# or
config.logger = false
To catch accidental full-table scans during test runs before they reach production, enable config.error_on_scan so Dynamoid raises Dynamoid::Errors::ScanProhibited whenever where conditions trigger a Scan operation:
config.error_on_scan = true
Safety Guards
To prevent test runs from accidentally modifying or dropping development, staging, or production tables, add a guard in your test helper to verify that Dynamoid.config.namespace is set to a test namespace:
unless Dynamoid.config.namespace.to_s.include?('test')
raise "Refusing to run tests with non-test namespace: #{Dynamoid.config.namespace.inspect}"
end
When your test suite runs against a local emulator, also verify that Dynamoid.config.endpoint points to a local host rather than remote AWS:
host = URI(Dynamoid.config.endpoint.to_s).host
unless ['localhost', '127.0.0.1'].include?(host)
raise "Refusing to run tests against non-local endpoint: #{Dynamoid.config.endpoint.inspect}"
end
Test Suite Integration
Because DynamoDB does not support rollbackable transactions, test suites cannot isolate tests by rolling back database state; instead, use Dynamoid::TestHelper to provision tables and clear items between tests.
To provision missing tables once before the test suite starts (or only for specific models when running a subset of tests), call create_tables:
Dynamoid::TestHelper.create_tables
# or
Dynamoid::TestHelper.create_tables(User, Post)
If your models live outside ./app/models, configure config.models_dir to point to their directory—when called without arguments, create_tables only creates tables for models in that directory.
To clear stored items between individual tests while keeping the tables intact, call truncate_tables:
Dynamoid::TestHelper.truncate_tables
# or
Dynamoid::TestHelper.truncate_tables(User, Post)
Because create_tables skips tables that already exist, changing a model’s primary key or secondary indexes requires dropping existing tables first. To delete all tables in the configured config.namespace, call drop_tables:
Dynamoid::TestHelper.drop_tables
Running drop_tables before every test suite run guarantees a fresh schema, while omitting it (or gating it behind an environment variable) avoids recreating unchanged tables on every local test run.
RSpec
In projects using RSpec, configure table lifecycle hooks in spec/support/dynamoid.rb (or spec/spec_helper.rb):
RSpec.configure do |config|
config.before(:suite) do
Dynamoid::TestHelper.drop_tables
Dynamoid::TestHelper.create_tables
end
config.before(:each) do
Dynamoid::TestHelper.truncate_tables
end
end
Minitest
In projects using Minitest (or ActiveSupport::TestCase in Rails), add the equivalent lifecycle hooks to test/test_helper.rb:
Dynamoid::TestHelper.drop_tables
Dynamoid::TestHelper.create_tables
class Minitest::Test
def setup
super
Dynamoid::TestHelper.truncate_tables
end
end
# or in Rails:
class ActiveSupport::TestCase
setup do
Dynamoid::TestHelper.truncate_tables
end
end
Feedback
We encourage you to help improve the quality of this guide!
If you spot a typo, a factual error, or a section that is incomplete or could be explained more clearly, please verify the behavior against the master branch and submit a pull request editing the Markdown files in the doc/ directory. If you find something that needs fixing but cannot patch it yourself, please open an issue on GitHub.