Skip to content

Commit

Permalink
Document usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Feb 29, 2024
1 parent 9342a78 commit 0f94ee5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
# rspec-sql

RSpec matcher for database queries

## Installation

```rb
# Gemfile
gem "rspec-sql"
```

## Usage

```rb
# Assert any database queries:
expect { User.last }.to query_database

# Assert no database queries:
expect { nil }.to_not query_database

# Assert exactly one query:
expect { User.last }.to query_database 1

# Assert specific queries:
expect { User.last }.to query_database ["User Load"]

expect { User.create!.update(name: "Jane") }.to query_database [
"TRANSACTION",
"User Create",
"TRANSACTION",
"TRANSACTION",
"User Update",
"TRANSACTION",
]
```

## Alternatives

If you are more interested in the number of specific queries then have a look
at these other gems:

- https://github.com/nepalez/rspec-sqlimit
- https://github.com/sds/db-query-matchers
11 changes: 11 additions & 0 deletions spec/lib/rspec/sql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@
it "expects certain queries" do
expect { User.last }.to query_database ["User Load"]
end

it "expects multiple queries" do
expect { User.create!.update(name: "Jane") }.to query_database [
"TRANSACTION",
"User Create",
"TRANSACTION",
"TRANSACTION",
"User Update",
"TRANSACTION",
]
end
end

0 comments on commit 0f94ee5

Please sign in to comment.