From 0f94ee54236d052bcee67a75cf77a50b714f08a7 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 29 Feb 2024 13:06:44 +1100 Subject: [PATCH] Document usage --- README.md | 41 ++++++++++++++++++++++++++++++++++++++ spec/lib/rspec/sql_spec.rb | 11 ++++++++++ 2 files changed, 52 insertions(+) diff --git a/README.md b/README.md index 31a622d..c231ed1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/spec/lib/rspec/sql_spec.rb b/spec/lib/rspec/sql_spec.rb index 6f29abd..d63f36c 100644 --- a/spec/lib/rspec/sql_spec.rb +++ b/spec/lib/rspec/sql_spec.rb @@ -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