[rspec-users] A better way to stub out constants

Ashley Moran work at ashleymoran.me.uk
Sun Oct 28 18:27:41 EDT 2007


Hi

Something that's gnawing at me... to avoid using the SQLite3 gem I'm  
stubbing it out like this:

     before(:each) do
       @database = mock("SQLite3 database")

       SQLite3 = Module.new
       SQLite3::Database = Class.new
       SQLite3::Database.stub!(:new).and_return(@database)
     end

But then it keeps nagging me:
   /spec/celestial/engine/connections/sqlite_connection_spec.rb:14:
   warning: already initialized constant SQLite3

Is this the best way to handle simulating a gem?  One thought I had  
would be to have a section of code that loads the gem and passes the  
SQLite3 class in as a variable (more functional style rather than  
using global constants).  Is this a good idea?

Alternatively, am I better using unless const_defined? to avoid  
redefining them, or perhaps silencing errors somehow?  Either of  
these will still dirty the namespace though.

Better ideas welcome!

Thanks
Ashley



-- 
blog @ http://aviewfromafar.net/
linked-in @ http://www.linkedin.com/in/ashleymoran
currently @ home



More information about the rspec-users mailing list