Skip to content

Commit

Permalink
added ability to provide the stub method block after calling the .yie…
Browse files Browse the repository at this point in the history
…lds() method, and execute the block as the stubbed method's code
  • Loading branch information
Derick Bailey committed Oct 25, 2009
1 parent c55cd49 commit 9f2ad80
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/not_a_mock/stubmethod.rb
Expand Up @@ -9,8 +9,9 @@ def initialize(&block)
@yield_values = []
end

def yields(*args)
def yields(*args, &block)
@yield_values = args
(@block = block) unless block.nil?
end

def execute_return_block(*args)
Expand Down
29 changes: 29 additions & 0 deletions spec/stub_method_with_block_spec.rb
Expand Up @@ -78,6 +78,10 @@ def do_something
}
end

after :each do
NotAMock::Stubber.instance.reset
end

it "should yield the first value" do
@yielded_bool.should be_true
end
Expand All @@ -101,6 +105,10 @@ def do_something
@yieldtest = YieldTest.new
@yieldtest.do_something
end

after :each do
NotAMock::Stubber.instance.reset
end

it "should track the method call on the yielded object" do
@stubyielded.should have_received(:yielding_test)
Expand All @@ -109,4 +117,25 @@ def do_something
it "should track the method call on the yielding object" do
YieldingObject.should have_received(:yield_test)
end
end

describe NotAMock::Stubber, "when stubbing a method and providing a block in the yields call" do
before :each do
@bt = BlockTest.new

@block_was_called = false
@output = @bt.stub_method(:method_that_yields_a_value).yields(true){
@block_was_called = true
}

@bt.method_that_yields_a_value()
end

after :each do
NotAMock::Stubber.instance.reset
end

it "should execute the block provided to the yield, as the stub method block" do
@block_was_called.should be_true
end
end

0 comments on commit 9f2ad80

Please sign in to comment.