From e3f6fb3c7c7062fa05de75a1d5109cdd9327549e Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 2 Jul 2021 18:43:39 -0600 Subject: [PATCH] Add no_args support for stubs Fixes https://github.com/icy-arctic-fox/spectator/issues/28 --- CHANGELOG.md | 4 ++++ spec/issues/github_issue_28_spec.cr | 19 +++++++++++++++++++ src/spectator/dsl/mocks.cr | 4 ++++ src/spectator/mocks/no_arguments.cr | 21 +++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 spec/issues/github_issue_28_spec.cr create mode 100644 src/spectator/mocks/no_arguments.cr diff --git a/CHANGELOG.md b/CHANGELOG.md index ccfc1bc..fb91198 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +### Added +- Added support for `with(no_args)` for method stubs. [#28](https://github.com/icy-arctic-fox/spectator/issues/28) + ## [0.9.38] - 2021-05-27 ### Fixed - Fix `Channel::ClosedError` when using default Crystal Logger. [#27](https://github.com/icy-arctic-fox/spectator/issues/27) diff --git a/spec/issues/github_issue_28_spec.cr b/spec/issues/github_issue_28_spec.cr new file mode 100644 index 0000000..6dd935c --- /dev/null +++ b/spec/issues/github_issue_28_spec.cr @@ -0,0 +1,19 @@ +require "../spec_helper" + +Spectator.describe "GitHub Issue #28" do + class Test + def foo + 42 + end + end + + mock Test do + stub foo + end + + it "matches method stubs with no_args" do + test = Test.new + expect(test).to receive(:foo).with(no_args).and_return(42) + test.foo + end +end diff --git a/src/spectator/dsl/mocks.cr b/src/spectator/dsl/mocks.cr index 7ffcfc8..0161fbf 100644 --- a/src/spectator/dsl/mocks.cr +++ b/src/spectator/dsl/mocks.cr @@ -170,4 +170,8 @@ module Spectator::DSL {% end %} %stubs end + + def no_args + ::Spectator::Mocks::NoArguments.new + end end diff --git a/src/spectator/mocks/no_arguments.cr b/src/spectator/mocks/no_arguments.cr new file mode 100644 index 0000000..d99f2bf --- /dev/null +++ b/src/spectator/mocks/no_arguments.cr @@ -0,0 +1,21 @@ +require "./arguments" + +module Spectator::Mocks + class NoArguments < Arguments + def args + Tuple.new + end + + def opts + NamedTuple.new + end + + def ===(other : Arguments) : Bool + other.args.empty? && other.opts.empty? + end + + def ===(other) : Bool + false + end + end +end