Skip to content

Commit

Permalink
Avoid confusing Typhoeus.stub with RSpec's #stub
Browse files Browse the repository at this point in the history
This fixes #4.
  • Loading branch information
yujinakayama committed Sep 22, 2013
1 parent fe4c3c0 commit 1ce55e9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master

* Avoid confusing `Typhoeus.stub` with RSpec's `stub` ([#4](https://github.com/yujinakayama/transpec/issues/4))

## v0.0.6

* Fix a bug where `SomeClass.any_instance.should_receive(:message).any_number_of_times` was converted into `expect_any_instance_of(SomeClass).to receive(:message)` unintentionally (now it's converted into `allow_any_instance_of(SomeClass).to receive(:message)`)
Expand Down
6 changes: 5 additions & 1 deletion lib/transpec/syntax/method_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Syntax
class MethodStub < Syntax
include AnyInstanceable, AnyNumberOfTimesable, Util

RECEIVER_CLASS_WHITELIST = ['Typhoeus']

def allowize!
# There's no way of unstubbing in #allow syntax.
return unless [:stub, :stub!].include?(method_name)
Expand Down Expand Up @@ -50,7 +52,9 @@ def replace_deprecated_method!
private

def self.target_receiver_node?(node)
!node.nil?
return false if node.nil?
const_name = Util.const_name(node)
!RECEIVER_CLASS_WHITELIST.include?(const_name)
end

def self.target_method_names
Expand Down
15 changes: 15 additions & 0 deletions spec/support/ast_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# encoding: utf-8

module ASTHelper
def scan_node(node, options = {}, &block)
yield node if options[:include_origin_node]

node.children.each do |child|
next unless child.is_a?(Parser::AST::Node)
yield child
scan_node(child, &block)
end

nil
end
end
40 changes: 40 additions & 0 deletions spec/transpec/syntax/method_stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,46 @@ class Syntax

let(:in_example_group_context?) { true }

describe '.target_node?' do
include ASTHelper

let(:send_node) do
scan_node(ast, include_origin_node: true) do |node|
next unless node.type == :send
method_name = node.children[1]
next unless method_name == :stub
return node
end
fail 'No #stub node is found!'
end

context 'when #stub node is passed' do
let(:source) do
<<-END
it 'responds to #foo' do
subject.stub(:foo)
end
END
end

it 'returns true' do
MethodStub.target_node?(send_node).should be_true
end
end

context 'when #stub node with Typhoeus receiver is passed' do
let(:source) do
<<-END
::Typhoeus.stub(url, :method => method).and_return(response)
END
end

it 'returns false' do
MethodStub.target_node?(send_node).should be_false
end
end
end

describe '#method_name' do
let(:source) do
<<-END
Expand Down

0 comments on commit 1ce55e9

Please sign in to comment.