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
> On Sep 5, 2011, at 11:45 PM, Bess Sadler wrote:
> > 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
> > --
> > 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-fedora@googlegroups.com.
> > To unsubscribe from this group, send email to active-fedora+unsubscribe@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/active-fedora?hl=en.