proper way to stub Fedora connection?

29 views
Skip to first unread message

Bess Sadler

unread,
Sep 6, 2011, 12:45:52 AM9/6/11
to active...@googlegroups.com
When you're writing unit tests and you want to fake your fedora connection, what's the best way to do it? Right now I have this line in some of my tests:

Fedora::Repository.stubs(:instance).returns(stub('stub').as_null_object)

It stubs the Fedora connection just fine, but it means that later, in other tests where I want a real connection to Fedora, I'm getting a stub back instead. Can anyone tell me how to clean up after this stub statement (ActiveFedora.init doesn't work, I tried that) or else give me a better way to mock the connection and then get it back after?

Thanks in advance,
Bess

Matt Zumwalt

unread,
Sep 6, 2011, 1:00:20 AM9/6/11
to active...@googlegroups.com
This is a perennial issue with RSpec.  It's bad about unstubbing Class methods like, in this case Fedora::Repository.instance

Technically, RSpec should be unstubbing the class methods after the particular tests run.  Failing that, you should be able to call Fedora::Repository.unstub(:instance) but that usually doesn't seem to actually work.

In the ActiveFedora tests, I do have before blocks that stub Fedora::Repository.instance and it is releasing that stub after running without the test explicitly calling unstub.  Possibly it's sensitive to where you set up the stub?

Example in: spec/unit/base_file_management_spec.rb

describe ActiveFedora::Base do
  
  before(:each) do
    Fedora::Repository.stubs(:instance).returns(stub_everything())
    ...
   end

end



Matt Zumwalt
MediaShelf, LLC




--
You received this message because you are subscribed to the Google Groups "ActiveFedora / Ruby + Fedora Commons" group.
To post to this group, send email to active...@googlegroups.com.
To unsubscribe from this group, send email to active-fedor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/active-fedora?hl=en.


Alexander

unread,
Sep 7, 2011, 5:24:05 AM9/7/11
to ActiveFedora / Ruby + Fedora Commons
I am also facing this issue. I desided to inject into Repository like
this
Fedora::NulledRepository.inject
Fedora::NulledRepository.revert

where NulledRepository works as collection to store incoming objects,
but i didnt finish that yet.
so if you need only to stub an instance, alias that method and ignore
all requests, after you done, return old method back.

its first implementation:
def self.instance
@instance ||= self.new
end

def self.inject
raise "injected called twice!" if @injected
@injected = true
@instance = nil
class << Fedora::Repository
# save real instance
alias_method :real_instance, :instance

def instance
Fedora::NulledRepository.instance
end
end
end

def self.revert
if Fedora::Repository.respond_to? :real_instance then
class << Fedora::Repository
# revert real instance
alias_method :instance, :real_instance
end

@injected = false
end
end

On 6 сен, 08:00, Matt Zumwalt <matt.zumw...@yourmediashelf.com> wrote:
> This is a perennial issue with RSpec.  It's bad about unstubbing Class methods like, in this case Fedora::Repository.instance
>
> Technically, RSpec should be unstubbing the class methods after the particular tests run.  Failing that, you should be able to call Fedora::Repository.unstub(:instance) but that usually doesn't seem to actually work.
>
> In the ActiveFedora tests, I do have before blocks that stub Fedora::Repository.instance and it is releasing that stub after running without the test explicitly calling unstub.  Possibly it's sensitive to where you set up the stub?
>
> Example in: spec/unit/base_file_management_spec.rb
>
> describe ActiveFedora::Base do
>
>   before(:each) do
>     Fedora::Repository.stubs(:instance).returns(stub_everything())
>     ...
>    end
>
> end
>
> Matt Zumwalt
> MediaShelf, LLChttp://www.yourmediashelf.com
Reply all
Reply to author
Forward
0 new messages