Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear stubs after each examples #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/motion/stump_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def verify_mocks

def it_with_mock_verification(description, &block)
@after << proc { verify_mocks }
@after << proc { Stump::Stubs.clear! }
it_without_mock_verification(description, &block)
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/stump/stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Object
# my_string.retreat! # => "run away! run away!"
#
def stub!(method_name, options = {}, &stubbed)
Stump::Stubs.add(self, method_name)

behavior = (block_given? ? stubbed : lambda { return options[:return] })

safe_meta_def method_name, &behavior
Expand All @@ -31,4 +33,4 @@ def stub(method, options = {}, &block)

stub_object
end
end
end
19 changes: 19 additions & 0 deletions lib/stump/stubs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Stump
class Stubs
class << self
def add(object, method)
stubs << [object, method]
end

def stubs
@stubs ||= []
end

def clear!
stubs.each do |object, method|
object.reset(method)
end
end
end
end
end
18 changes: 18 additions & 0 deletions spec/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,23 @@ def ignored_flunk(message)
alias_method :flunk, :original_flunk
end
end

describe "clear stubs" do
context "with stub" do
before do
Dog.stub!(:kind, return: 'Foo')
end

it "runs with stubs" do
Dog.kind.should == 'Foo'
end
end

context "without stub" do
it "runs without stubs" do
Dog.kind.should == 'Mammal'
end
end
end
end
end