I have a Premise model with an after_create method, along the lines of: class Premise < ActiveRecord::Base ... after_create :etl_attributes ... end In production code, etl_attributes accesses the web. For testing I''m using WebMock and VCR in cases where I want to test the etl_attributes function -- works like a champ. For all other tests, I want to create a real Premise object (I need it for HABTM associations), but I want to stub out the call to etl_attributes. My problem is that I haven''t figured out the right way to do so. The following DOESN''T work (it should be clear that I''m using FactoryGirl as well): mock_model(Premise) Premise.stub!(:etl_attributes) @premise = Factory.create(:premise) ... this still calls etl_attributes and WebMock gets appropriately mad. What am I missing? - ff -- Posted via http://www.ruby-forum.com/.
On Feb 14, 11:31?am, Fearless Fool <li... at ruby-forum.com> wrote:> I have a Premise model with an after_create method, along the lines of: > > class Premise < ActiveRecord::Base > ? ... > ? after_create :etl_attributes > ? ... > end > > In production code, etl_attributes accesses the web. ?For testing I''m > using WebMock and VCR in cases where I want to test the etl_attributes > function -- works like a champ. > > For all other tests, I want to create a real Premise object (I need it > for HABTM associations), but I want to stub out the call to > etl_attributes. ?My problem is that I haven''t figured out the right way > to do so. ?The following DOESN''T work (it should be clear that I''m using > FactoryGirl as well): > > ? ? ? mock_model(Premise) > ? ? ? Premise.stub!(:etl_attributes) > ? ? ? @premise = Factory.create(:premise) > > ... this still calls etl_attributes and WebMock gets appropriately mad. > > What am I missing? > > - ff > > -- > Posted viahttp://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-usersetl_attributes is an instance method, not a class method. So, you want to stub the method on the Premise instance. Since you''re using rspec- mocks, I would recommend something like this: @premise = Factory.build(:premise) @premise.stub(:etl_attributes) @premise.save! For more info using pure mock objects, checkout these pages: http://relishapp.com/rspec/rspec-rails/v/2-5/dir/mocks/mock-model http://relishapp.com/rspec/rspec-rails/v/2-5/dir/mocks/stub-model
Hi, On Tue, Feb 15, 2011 at 02:31, Fearless Fool <lists at ruby-forum.com> wrote:> I have a Premise model with an after_create method, along the lines of: > > class Premise < ActiveRecord::Base > ?... > ?after_create :etl_attributes > ?... > end > > In production code, etl_attributes accesses the web. ?For testing I''m > using WebMock and VCR in cases where I want to test the etl_attributes > function -- works like a champ. > > For all other tests, I want to create a real Premise object (I need it > for HABTM associations), but I want to stub out the call to > etl_attributes. ?My problem is that I haven''t figured out the right way > to do so. ?The following DOESN''T work (it should be clear that I''m using > FactoryGirl as well): > > ? ? ?mock_model(Premise) > ? ? ?Premise.stub!(:etl_attributes) > ? ? ?@premise = Factory.create(:premise) > > ... this still calls etl_attributes and WebMock gets appropriately mad. > > What am I missing?Perhaps one way to approach this is to have WebMock return an appropriate success message in response to the network call that :etl_attributes makes. Mike
Mike Mazur wrote in post #981665:> Perhaps one way to approach this is to have WebMock return an > appropriate success message in response to the network call that > :etl_attributes makes. > > MikeAh - good point. With all the options for stubbing and mocking, I''d forgotten that one. Factory Girl is creating premise objects that will generate different URLs, but I can use the regex option for WebMock to catch them. Thanks for getting me unstuck! - ff -- Posted via http://www.ruby-forum.com/.