Skip to content

Commit

Permalink
Take 2 on stubbing attribute accessor after respond_to? call on mocke…
Browse files Browse the repository at this point in the history
…d model. Fixes rspec#437.
  • Loading branch information
iafonov committed Dec 7, 2011
1 parent 4c7fd73 commit 9f86140
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/rspec/rails/mocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def @object.instance_of?(other)
def @object.respond_to?(method_name, include_private=false)
if #{model_class}.respond_to?(:column_names) && #{model_class}.column_names.include?(method_name.to_s)
stub!(method_name) unless [#{stubs.keys.map{|stub| ":#{stub}"}.join(',')}].include?(method_name)
unless __send__(:__mock_proxy).send(:method_doubles).map{ |md| md.method_name }.include?(method_name.to_sym)
stub!(method_name)
end
true
else
super
Expand Down
31 changes: 27 additions & 4 deletions spec/rspec/rails/mocks/mock_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,33 @@
it "says it will respond_to?(key) if RealModel has the attribute 'key'" do
@model.respond_to?("column_a").should be(true)
end
it "stubs column accessor method after respond_to call" do
lambda { @model.column_a }.should raise_error
@model.respond_to?("column_a").should be(true)
lambda { @model.column_a }.should_not raise_error
it "stubs column accessor (with string)" do
@model.respond_to?("column_a")
@model.column_a.should be_nil
end
it "stubs column accessor (with symbol)" do
@model.respond_to?(:column_a)
@model.column_a.should be_nil
end
it "does not stub column accessor if already stubbed in declaration (with string)" do
model = mock_model(MockableModel, "column_a" => "a")
model.respond_to?("column_a")
model.column_a.should eq("a")
end
it "does not stub column accessor if already stubbed in declaration (with symbol)" do
model = mock_model(MockableModel, :column_a => "a")
model.respond_to?("column_a")
model.column_a.should eq("a")
end
it "does not stub column accessor if already stubbed after declaration (with string)" do
@model.stub("column_a" => "a")
@model.respond_to?("column_a")
@model.column_a.should eq("a")
end
it "does not stub column accessor if already stubbed after declaration (with symbol)" do
@model.stub(:column_a => "a")
@model.respond_to?("column_a")
@model.column_a.should eq("a")
end
it "says it will not respond_to?(key) if RealModel does not have the attribute 'key'" do
@model.respond_to?("column_c").should be(false)
Expand Down

0 comments on commit 9f86140

Please sign in to comment.