Permalink
Browse files

Take 2 on stubbing attribute accessor after respond_to? call on mocke…

…d model. Fixes #437.
  • Loading branch information...
1 parent 4c7fd73 commit e242279331959c678761520c3e3b35cc9a1f2336 @iafonov iafonov committed with dchelimsky Dec 7, 2011
Showing with 31 additions and 5 deletions.
  1. +4 −1 lib/rspec/rails/mocks.rb
  2. +27 −4 spec/rspec/rails/mocks/mock_model_spec.rb
@@ -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
@@ -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)

0 comments on commit e242279

Please sign in to comment.