Skip to content

Commit

Permalink
Rspec test helper too
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegeek committed Oct 7, 2024
1 parent 1b9904f commit 36a0136
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
12 changes: 5 additions & 7 deletions lib/quo/minitest/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
# frozen_string_literal: true

require "minitest/mock"

require_relative "../fakes/collection_backed_fake"
require_relative "../fakes/relation_backed_fake"
require_relative "../testing/collection_backed_fake"
require_relative "../testing/relation_backed_fake"

module Quo
module Minitest
module Helpers
def fake_query(query_class, results: [], total_count: nil, page_count: nil)
def fake_query(query_class, results: [], total_count: nil, page_count: nil, &block)
# make it so that results of instances of this class return a fake Result object
# of the right type which returns the results passed in
if query_class < Quo::CollectionBackedQuery
klass = Class.new(Quo::Fakes::CollectionBackedFake) do
klass = Class.new(Quo::Testing::CollectionBackedFake) do
if query_class < Quo::Preloadable
include Quo::Preloadable

Expand All @@ -28,7 +26,7 @@ def query
end
elsif query_class < Quo::RelationBackedQuery
query_class.stub(:new, ->(**kwargs) {
Quo::Fakes::RelationBackedFake.new(results: results, total_count: total_count, page_count: page_count)
Quo::Testing::RelationBackedFake.new(results: results, total_count: total_count, page_count: page_count)
}) do
yield
end
Expand Down
38 changes: 31 additions & 7 deletions lib/quo/rspec/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
# frozen_string_literal: true

require_relative "../testing/collection_backed_fake"
require_relative "../testing/relation_backed_fake"

module Quo
module Rspec
module Helpers
def stub_query(query_class, with: nil, results: [])
collection = ::Quo::CollectionBackedQuery.wrap(results)
unless with.nil?
return(
allow(query_class).to receive(:new).with(with) { collection.new }
)
def fake_query(query_class, with: nil, results: [], total_count: nil, page_count: nil, &block)
# make it so that results of instances of this class return a fake Result object
# of the right type which returns the results passed in
if query_class < Quo::CollectionBackedQuery
klass = Class.new(Quo::Testing::CollectionBackedFake) do
if query_class < Quo::Preloadable
include Quo::Preloadable

def query
collection
end
end
end
fake = ->(*kwargs) {
klass.new(results: results, total_count: total_count, page_count: page_count)
}
expectation = allow(query_class).to receive(:new)
expectation = expectation.with(with) if with
expectation.and_invoke(fake)
elsif query_class < Quo::RelationBackedQuery
fake = ->(*kwargs) {
Quo::Testing::RelationBackedFake.new(results: results, total_count: total_count, page_count: page_count)
}
expectation = allow(query_class).to receive(:new)
expectation = expectation.with(with) if with
expectation.and_invoke(fake)
else
raise ArgumentError, "Not a Query class: #{query_class}"
end
allow(query_class).to receive(:new) { collection.new }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# rbs_inline: enabled

module Quo
module Fakes
module Testing
class CollectionBackedFake < Quo.collection_backed_query_base_class
prop :results, _Any, reader: false
prop :page_count, _Nilable(Integer), reader: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# rbs_inline: enabled

module Quo
module Fakes
module Testing
class RelationBackedFake < Quo.relation_backed_query_base_class
prop :results, _Any, reader: false
prop :page_count, _Nilable(Integer), reader: false
Expand Down

0 comments on commit 36a0136

Please sign in to comment.