[rspec-users] Interdependency between RSpec files

Glenn Ford glenn at aldenta.com
Thu Nov 15 10:55:27 EST 2007


On Nov 14, 2007, at 2:14 PM, David Chelimsky wrote:

> On Nov 14, 2007 1:07 PM, Glenn Ford <glenn at aldenta.com> wrote:
>> Your suggestion put me on the right track.  I looked back to where I
>> had I ran into a case where I was trying to stub an instance I
>> couldn't get ahold of in the scope of my spec and since I was having
>> trouble with some mocha bugs, I resorted to a
>> Model.class_eval do
>>       alias_method :original_method, :old_method
>> end
>
> Try doing that on the instance instead.
>
> model = Model.new
>
> (class << model).class_eval do
>  alias_method :original_method, :old_method
> end
>
> That way it can't leak to other instances of the same model.

That's what I had hoped to do, but as I stated in my description, the  
difficult situation involved a model I couldn't get ahold of in the  
scope of my spec.  If I was able to instantiate it somehow myself I  
would have just stubbed the method.  This probably means I've got some  
code in the controller that shouldn't live in the controller,  
however.  If I refactor it properly I doubt I'll need any more fancy  
tricks like this.  Thanks!

>
>
>>
>> Unfortunately I didn't set the alias back at the end of the spec.
>> That was my problem.  Thanks a ton!
>>
>> Glenn
>>
>>
>> On Nov 14, 2007, at 12:28 PM, Scott Taylor wrote:
>>
>>>
>>> On Nov 14, 2007, at 11:37 AM, Glenn Ford wrote:
>>>
>>>> I run my suite of tests, one test fails.
>>>> I run that one test file, no tests fail.
>>>>
>>>> Something is carrying over between files and I can't figure out  
>>>> what.
>>>> I tracked down the problem to the very line it's occurring on, with
>>>> printouts before and after every call to make sure I know exactly
>>>> what
>>>> is being reached.
>>>
>>>
>>> First of all - make sure you don't have any after(:create) or before
>>> (:create) specs.  If an AR object is created in one of these, it *IS
>>> NOT* going to be rolled back.
>>>
>>> You might also try the following:
>>>
>>> rake db:migrate; rake db:test:prepare
>>>
>>> Log into mysql (or whatever database) - inspect it to see that no
>>> records are present in your test database (if you really aren't  
>>> using
>>> fixtures at all).
>>>
>>>
>>>>
>>>> In this spec I have 0 fixtures/mocks/stubs.  The objects in this  
>>>> file
>>>> do not live anywhere outside of the file.  This is a model spec and
>>>> I'm using all real objects to test it.  In the "before" block I set
>>>> up
>>>> some models, and in my spec itself I, in each test, make a couple
>>>> changes then call a model's save method.  That model has an
>>>> "after_create" method which calls a method in a child model that it
>>>> has.  It is THIS method which is not being called.  I have  
>>>> printouts
>>>> before and after everything, like I said, and the lines before and
>>>> after the method call work, which leads me to believe that method  
>>>> IS
>>>> getting called.  However I have a "puts 'foo'" at the top of that
>>>> method and it doesn't get printed.
>>>
>>> You might want to try running that one spec with the following
>>> snippet stuck into the top of your after_create method:
>>>
>>> require 'rubygems'; require 'ruby-debug'; debugger;
>>>
>>> When the spec runs, you will be dropped down into the ruby debugger,
>>> so you can inspect what is *actually* going on.  (You will need to
>>> know how to use a debugger, and have the ruby-debug gem installed).
>>>
>>> If that doesn't yield any helpful information, then remove that
>>> snippet and put it at the top of the failing test (the first line of
>>> the example ("it") block).  Then run the full test suite, and again,
>>> you will be dropped down into the debugger.
>>>
>>> Hope that helps,
>>>
>>> Scott
>>>
>>>
>>>
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-users at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list