Allow creation of doubles without a definition

Utilizes anonymous doubles to implement this.
Fixes https://github.com/icy-arctic-fox/spectator/issues/30
This commit is contained in:
Michael Miller 2021-07-02 19:18:14 -06:00
parent d4e2954725
commit a0537484f4
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 32 additions and 14 deletions

View file

@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added ### Added
- Added support for `with(no_args)` for method stubs. [#28](https://github.com/icy-arctic-fox/spectator/issues/28) - 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 ## [0.9.38] - 2021-05-27
### Fixed ### Fixed

View file

@ -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

View file

@ -19,13 +19,17 @@ module Spectator::DSL
end end
macro create_double(type_name, name, **stubs) macro create_double(type_name, name, **stubs)
{% type_name.resolve? || raise("Could not find a double labeled #{name}") %} {% if type_name.resolve? %}
{{type_name}}.new.tap do |%double|
{{type_name}}.new.tap do |%double| {% for name, value in stubs %}
{% for name, value in stubs %} allow(%double).to receive({{name.id}}).and_return({{value}})
allow(%double).to receive({{name.id}}).and_return({{value}}) {% end %}
{% end %} end
end {% elsif @def %}
anonymous_double({{name ? name.stringify : "Anonymous"}}, {{stubs.double_splat}})
{% else %}
{% raise "Block required for double definition" %}
{% end %}
end end
macro define_double(type_name, name, **stubs, &block) macro define_double(type_name, name, **stubs, &block)
@ -73,13 +77,17 @@ module Spectator::DSL
end end
macro create_null_double(type_name, name, **stubs) macro create_null_double(type_name, name, **stubs)
{% type_name.resolve? || raise("Could not find a double labeled #{name}") %} {% if type_name.resolve? %}
{{type_name}}.new(true).tap do |%double|
{{type_name}}.new(true).tap do |%double| {% for name, value in stubs %}
{% for name, value in stubs %} allow(%double).to receive({{name.id}}).and_return({{value}})
allow(%double).to receive({{name.id}}).and_return({{value}}) {% end %}
{% end %} end
end {% elsif @def %}
anonymous_null_double({{name ? name.stringify : "Anonymous"}}, {{stubs.double_splat}})
{% else %}
{% raise "Block required for double definition" %}
{% end %}
end end
macro define_null_double(type_name, name, **stubs, &block) macro define_null_double(type_name, name, **stubs, &block)