warning: removing `initialize' may cause serious problems #130

Closed
felipero opened this Issue May 1, 2012 · 11 comments

4 participants

@felipero

Hi,

I'm getting some annoying warnings when using rspec-mocks on integration tests. I'm using that to mock twitter and facebook services.

Example:

/gems/rspec-mocks-2.8.0/lib/rspec/mocks/method_double.rb:105: warning: removing initialize' may cause serious problems
/gems/rspec-mocks-2.8.0/lib/rspec/mocks/any_instance/recorder.rb:135: warning: removing
initialize' may cause serious problems

The mocks works well and specs are fine. But my output gets filled with these warnings.

Any idea about how to deal with it?

Thanks,

Felipe

@dchelimsky
RSpec member

Do you get them with 2.9?

@dchelimsky
RSpec member

Also what version of Ruby?

@dchelimsky
RSpec member

Just looked at this a bit closer - the lines in question remove a method that is being stubbed somewhere outside of rspec. The warning is coming from Ruby, not rspec-mocks, so rspec-mocks is just the messenger here. I'm closing this because it is not an issue with the rspec-mocks library, but we can keep the conversation going if you'd like help resolving it.

Do you have some place in your code that you see is stubbing initialize? If not, can you run your specs with the --backtrace flag (gotta upgrade to 2.9 to do this because it's broken in 2.8) and post the backtrace here?

@dchelimsky dchelimsky closed this May 1, 2012
@felipero

Hi David, thanks for the answer. Took some time to upgrade to rspec 2.9.0 because have some issues to fix before (nothing related to rspec).
I can't find anything that undefine initialize. Maybe some other gem?

Here are the output when running with --backtrace. Seems like nothing changed

$ rspec spec/integration --backtrace
..........................................................F.......................................................*........./Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/any_instance/recorder.rb:135: warning: removing `initialize' may cause serious problems
/Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/any_instance/recorder.rb:135: warning: removing `initialize' may cause serious problems
/Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/method_double.rb:105: warning: removing `initialize' may cause serious problems
./Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/any_instance/recorder.rb:135: warning: removing `initialize' may cause serious problems
/Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/any_instance/recorder.rb:135: warning: removing `initialize' may cause serious problems
/Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/method_double.rb:105: warning: removing `initialize' may cause serious problems
../Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/any_instance/recorder.rb:135: warning: removing `initialize' may cause serious problems
/Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/any_instance/recorder.rb:135: warning: removing `initialize' may cause serious problems
/Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/method_double.rb:105: warning: removing `initialize' may cause serious problems
../Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/any_instance/recorder.rb:135: warning: removing `initialize' may cause serious problems
/Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/any_instance/recorder.rb:135: warning: removing `initialize' may cause serious problems
/Users/felipero/.rvm/gems/ruby-1.9.2-p290@minhaimobiliaria/gems/rspec-mocks-2.9.0/lib/rspec/mocks/method_double.rb:105: warning: removing `initialize' may cause serious problems
....*...........

So, any idea about how to track this problem?

@felipero

By the way, just searched in the source code of all gems and didn't find any explicit removal of initialize.

@dchelimsky
RSpec member

re: no change w/ the backtrace, that makes sense since this is a warning, not a failure (the backtrace only comes from failures or errors).

The removal of initialize is happening in rspec-mocks, but it can only happen if something is stubbing it. Look for any_instance.stub(:initialize) or any_instance.should_receive(:initialize).

@felipero

Ops... Sorry. You're right. My search regex was broken, that's why I didn't find it. Found it now.

Thanks for your help. I wish all open-source project had this kind of attention. You rock. :)

@dchelimsky
RSpec member

Glad to help. Cheers.

@charlesdeb

I have the same issue because I am stubbing out the initialize method:

IettPage.any_instance.should_receive(:initialize).and_call_original

because I want to ensure that a new IettPage object is being created in a method I am testing. Adding the "and_call_original" method is not removing the ruby warnings. I am guessing there is a more idiomatic way to test for object creation in rspec, but my Google searches have so far turned up nothing. Suggestions anyone?

@myronmarston
RSpec member

Why not use IettPage.should_receive(:new).and_call_original?

@charlesdeb

Thanks Myron - great idea which works great. I originally had

IettPage.any_instance.should_receive(:new).and_call_original

which didn't work (for what seem like obvious reasons now!). Losing the any_instance made it work perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment