From a0537484f4333d6d8c261886433a12926eb68824 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 2 Jul 2021 19:18:14 -0600 Subject: [PATCH] Allow creation of doubles without a definition Utilizes anonymous doubles to implement this. Fixes https://github.com/icy-arctic-fox/spectator/issues/30 --- CHANGELOG.md | 1 + spec/issues/github_issue_30_spec.cr | 9 ++++++++ src/spectator/dsl/mocks.cr | 36 ++++++++++++++++++----------- 3 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 spec/issues/github_issue_30_spec.cr diff --git a/CHANGELOG.md b/CHANGELOG.md index fb91198..50d9198 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added - Added support for `with(no_args)` for method stubs. [#28](https://github.com/icy-arctic-fox/spectator/issues/28) +- Allow creation of doubles without definition block. [#30](https://github.com/icy-arctic-fox/spectator/issues/30) ## [0.9.38] - 2021-05-27 ### Fixed diff --git a/spec/issues/github_issue_30_spec.cr b/spec/issues/github_issue_30_spec.cr new file mode 100644 index 0000000..2720803 --- /dev/null +++ b/spec/issues/github_issue_30_spec.cr @@ -0,0 +1,9 @@ +require "../spec_helper" + +Spectator.describe "GitHub Issue #30" do + let(dbl) { double(:foo) } + + it "supports block-less symbol doubles" do + expect(dbl).to_not be_nil + end +end diff --git a/src/spectator/dsl/mocks.cr b/src/spectator/dsl/mocks.cr index c910619..640c054 100644 --- a/src/spectator/dsl/mocks.cr +++ b/src/spectator/dsl/mocks.cr @@ -19,13 +19,17 @@ module Spectator::DSL end macro create_double(type_name, name, **stubs) - {% type_name.resolve? || raise("Could not find a double labeled #{name}") %} - - {{type_name}}.new.tap do |%double| - {% for name, value in stubs %} - allow(%double).to receive({{name.id}}).and_return({{value}}) - {% end %} - end + {% if type_name.resolve? %} + {{type_name}}.new.tap do |%double| + {% for name, value in stubs %} + allow(%double).to receive({{name.id}}).and_return({{value}}) + {% end %} + end + {% elsif @def %} + anonymous_double({{name ? name.stringify : "Anonymous"}}, {{stubs.double_splat}}) + {% else %} + {% raise "Block required for double definition" %} + {% end %} end macro define_double(type_name, name, **stubs, &block) @@ -73,13 +77,17 @@ module Spectator::DSL end macro create_null_double(type_name, name, **stubs) - {% type_name.resolve? || raise("Could not find a double labeled #{name}") %} - - {{type_name}}.new(true).tap do |%double| - {% for name, value in stubs %} - allow(%double).to receive({{name.id}}).and_return({{value}}) - {% end %} - end + {% if type_name.resolve? %} + {{type_name}}.new(true).tap do |%double| + {% for name, value in stubs %} + allow(%double).to receive({{name.id}}).and_return({{value}}) + {% end %} + end + {% elsif @def %} + anonymous_null_double({{name ? name.stringify : "Anonymous"}}, {{stubs.double_splat}}) + {% else %} + {% raise "Block required for double definition" %} + {% end %} end macro define_null_double(type_name, name, **stubs, &block)