I just posted an article on RubySource.com titled Flexible Searching with Solr and Sunspot. It covers working Solr into a Rails app and massaging Solr to match searches on your content.
Posted by Mike Pack on 03/07/2012 at 08:54AM
There's not any great info out there about stubbing constants. In fact, you can't stub constants. What you can do is reinitialize them.
The following code is trouble because Ruby complains about reinitializing the constant:
it 'changes the constant' do MyClass::SOME_CONSTANT = [1,2,3] MyClass::SOME_CONSTANT.should == [1,2,3] end #=> warning: already initialized constant SOME_CONSTANT
The spec passes but I hate the warning message Ruby spits out. So here's how we can prevent this warning and achieve the same result:
it 'changes the constant' do MyClass.send(:remove_const, 'SOME_CONSTANT') MyClass::SOME_CONSTANT = [1,2,3] MyClass::SOME_CONSTANT.should == [1,2,3] end
We explicitly remove the constant before reinitializing it. We use #send to access #remove_const because it's a private method. Happy stubbing!
Posted by Mike Pack on 02/21/2012 at 01:15PM
My article titled "SOA for the Little Guys" was just published on RubySource. It covers breaking apart a monolithic app into services with testing and Sinatra as the driving forces.
Give it a read!
Posted by Mike Pack on 02/13/2012 at 10:04AM