Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

reproduce an issue on activerecord with spec

  • Loading branch information...
commit 70d817458906fa2df911a70839d5fa023bf923fc 1 parent 295188f
@ono authored
Showing with 55 additions and 0 deletions.
  1. +55 −0 spec/api/mock/mock_spec.rb
View
55 spec/api/mock/mock_spec.rb
@@ -190,4 +190,59 @@ class SampleClass2 < SampleClass1; end
end
end
+
+ # This spec tries to reproduce an issue on mocking with ActiveRecord's association.
+ # It gives a strange unexpected invocation error at a particlar situation.
+ describe "with a proxy class" do
+ # The ProxySample class immitates AssociationProxy class in activerecord.
+ class ProxySample
+ def initialize
+ @model = ModelSample.new
+ end
+
+ def method_missing(sym,*args,&block)
+ if @model.respond_to?(sym)
+ @model.send(sym, *args, &block)
+ else
+ super
+ end
+ end
+ end
+
+ # The ModelSample class immitates ActiveRecord::Base class in activerecord.
+ class ModelSample
+ def name
+ "Brian Takita"
+ end
+
+ def github_id
+ "btakita"
+ end
+ end
+
+ before do
+ @proxy = ProxySample.new
+ mock(@proxy).name.times(1) { "Mocked!" }
+ end
+
+ # This works on 1.2.0
@ono Owner
ono added a note

typo. meant 1.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ it "mocks properly usually" do
+ @proxy.name.should == "Mocked!"
+ end
+
+ # This also works on 1.2.0
+ it "calls an original method if it is not mocked" do
+ @proxy.name.should == "Mocked!"
+ @proxy.github_id.should == "btakita"
+ end
+
+ # But this doesn't work on 1.2.0.
+ # It gives unexpected method invocation when non-mocking method is called
+ # second time.
+ it "calls an original method if it is not mocked for the second time too " do
+ @proxy.name.should == "Mocked!"
+ @proxy.github_id.should == "btakita"
+ @proxy.github_id.should == "btakita"
+ end
+ end
end
@ono

typo. meant 1.0.2

Please sign in to comment.
Something went wrong with that request. Please try again.