|
| 1 | +# Shared Test Behaviors and Fixtures |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Sus provides shared test contexts which can be used to define common behaviours or tests that can be reused across one or more test files. |
| 6 | + |
| 7 | +When you have common test behaviors that you want to apply to multiple test files, add them to the `fixtures/` directory. When you have common test behaviors that you want to apply to multiple implementations of the same interface, within a single test file, you can define them as shared contexts within that file. |
| 8 | + |
| 9 | +## Shared Fixtures |
| 10 | + |
| 11 | +### Directory Structure |
| 12 | + |
| 13 | +``` |
| 14 | +my-gem/ |
| 15 | +├── lib/ |
| 16 | +│ ├── my_gem.rb |
| 17 | +│ └── my_gem/ |
| 18 | +│ └── my_thing.rb |
| 19 | +├── fixtures/ |
| 20 | +│ └── my_gem/ |
| 21 | +│ └── a_thing.rb # Provides MyGem::AThing shared context |
| 22 | +└── test/ |
| 23 | + ├── my_gem.rb |
| 24 | + └── my_gem/ |
| 25 | + └── my_thing.rb |
| 26 | +``` |
| 27 | + |
| 28 | +### Creating Shared Fixtures |
| 29 | + |
| 30 | +Create shared behaviors in the `fixtures/` directory using `Sus::Shared`: |
| 31 | + |
| 32 | +```ruby |
| 33 | +# fixtures/my_gem/a_user.rb |
| 34 | + |
| 35 | +require "sus/shared" |
| 36 | + |
| 37 | +module MyGem |
| 38 | + AUser = Sus::Shared("a user") do |role| |
| 39 | + let(:user) do |
| 40 | + { |
| 41 | + name: "Test User", |
| 42 | + |
| 43 | + role: role |
| 44 | + } |
| 45 | + end |
| 46 | + |
| 47 | + it "has a name" do |
| 48 | + expect(user[:name]).not.to be_nil |
| 49 | + end |
| 50 | + |
| 51 | + it "has a valid email" do |
| 52 | + expect(user[:email]).to be(:include?, "@") |
| 53 | + end |
| 54 | + |
| 55 | + it "has a role" do |
| 56 | + expect(user[:role]).to be_a(String) |
| 57 | + end |
| 58 | + end |
| 59 | +end |
| 60 | +``` |
| 61 | + |
| 62 | +### Using Shared Fixtures |
| 63 | + |
| 64 | +Require and use shared fixtures in your test files: |
| 65 | + |
| 66 | +```ruby |
| 67 | +# test/my_gem/user_manager.rb |
| 68 | +require 'my_gem/a_user' |
| 69 | + |
| 70 | +describe MyGem::UserManager do |
| 71 | + it_behaves_like MyGem::AUser, "manager" |
| 72 | + # or include_context MyGem::AUser, "manager" |
| 73 | +end |
| 74 | +``` |
| 75 | + |
| 76 | +### Multiple Shared Fixtures |
| 77 | + |
| 78 | +You can create multiple shared fixtures for different scenarios: |
| 79 | + |
| 80 | +```ruby |
| 81 | +# fixtures/my_gem/users.rb |
| 82 | +module MyGem |
| 83 | + module Users |
| 84 | + AStandardUser = Sus::Shared("a standard user") do |
| 85 | + let(:user) do |
| 86 | + { name: "John Doe", role: "user", active: true } |
| 87 | + end |
| 88 | + |
| 89 | + it "is active" do |
| 90 | + expect(user[:active]).to be_truthy |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + AnAdminUser = Sus::Shared("an admin user") do |
| 95 | + let(:user) do |
| 96 | + { name: "Admin User", role: "admin", active: true } |
| 97 | + end |
| 98 | + |
| 99 | + it "has admin role" do |
| 100 | + expect(user[:role]).to be == "admin" |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | +end |
| 105 | +``` |
| 106 | + |
| 107 | +Use specific shared fixtures: |
| 108 | + |
| 109 | +```ruby |
| 110 | +# test/my_gem/authorization.rb |
| 111 | +require 'my_gem/users' |
| 112 | + |
| 113 | +describe MyGem::Authorization do |
| 114 | + with "standard user" do |
| 115 | + # If there are no arguments, you can use `include` directly: |
| 116 | + include MyGem::Users::AStandardUser |
| 117 | + |
| 118 | + it "denies admin access" do |
| 119 | + auth = subject.new |
| 120 | + expect(auth.can_admin?(user)).to be_falsey |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + with "admin user" do |
| 125 | + include MyGem::Users::AnAdminUser |
| 126 | + |
| 127 | + it "allows admin access" do |
| 128 | + auth = subject.new |
| 129 | + expect(auth.can_admin?(user)).to be_truthy |
| 130 | + end |
| 131 | + end |
| 132 | +end |
| 133 | +``` |
| 134 | + |
| 135 | +### Modules |
| 136 | + |
| 137 | +You can also define shared behaviors in modules and include them in your test files: |
| 138 | + |
| 139 | +```ruby |
| 140 | +# fixtures/my_gem/shared_behaviors.rb |
| 141 | +module MyGem |
| 142 | + module SharedBehaviors |
| 143 | + def self.included(base) |
| 144 | + base.it "uses shared data" do |
| 145 | + expect(shared_data).to be == "some shared data" |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + def shared_data |
| 150 | + "some shared data" |
| 151 | + end |
| 152 | + end |
| 153 | +end |
| 154 | +``` |
| 155 | + |
| 156 | +### Enumerating Tests |
| 157 | + |
| 158 | +Some tests will be run multiple times with different arguments (for example, multiple database adapters). You can use `Sus::Shared` to define these tests and then enumerate them: |
| 159 | + |
| 160 | +```ruby |
| 161 | +# test/my_gem/database_adapter.rb |
| 162 | + |
| 163 | +require "sus/shared" |
| 164 | + |
| 165 | +ADatabaseAdapter = Sus::Shared("a database adapter") do |adapter| |
| 166 | + let(:database) {adapter.new} |
| 167 | + |
| 168 | + it "connects to the database" do |
| 169 | + expect(database.connect).to be_truthy |
| 170 | + end |
| 171 | + |
| 172 | + it "can execute queries" do |
| 173 | + expect(database.execute("SELECT 1")).to be == [[1]] |
| 174 | + end |
| 175 | +end |
| 176 | + |
| 177 | +# Enumerate the tests with different adapters |
| 178 | +MyGem::DatabaseAdapters.each do |adapter| |
| 179 | + describe "with #{adapter}", unique: adapter.name do |
| 180 | + it_behaves_like ADatabaseAdapter, adapter |
| 181 | + end |
| 182 | +end |
| 183 | +``` |
| 184 | + |
| 185 | +Note the use of `unique: adapter.name` to ensure each test is uniquely identified, which is useful for reporting and debugging - otherwise the same test line number would be used for all iterations, which can make it hard to identify which specific test failed. |
0 commit comments