Skip to content

Instantly share code, notes, and snippets.

@artemave
Created August 23, 2011 21:17
Show Gist options
  • Select an option

  • Save artemave/1166574 to your computer and use it in GitHub Desktop.

Select an option

Save artemave/1166574 to your computer and use it in GitHub Desktop.
class variable mystery
#code.rb
class Resource
end
module SomeModule
class Consumer
def singleton_resource
@@sr ||= Resource.new
end
end
end
#spec.rb
require 'rspec'
require_relative 'code'
describe SomeModule::Consumer do
it 'should init resource once' do
res = double('resource')
Resource.should_receive(:new).once.and_return(res)
SomeModule::Consumer.new.singleton_resource
SomeModule::Consumer.new.singleton_resource
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment