ruby on rails 3 - Stubbing :find on a Model Class -


given model method self.fetch_payment_method:

def self.fetch_payment_method   name = "omnikassa"   pm = spree::paymentmethod.find(:first, :conditions => [ "lower(name) = ?", name.downcase ]) || raise(activerecord::recordnotfound) end 

and rspec test test this:

it 'should find payment_method'   spree::paymentmethod.new(:name => "omnikassa").save   @omnikassa.class.fetch_payment_method.should be_a_kind_of(spree::paymentmethod) end 

i'd improve this, not test entire stack , database. that, i'd want stub ":find" when called on class spree::paymentmethod. however:

it 'should find payment_method'   spree::paymentmethod.any_instance.stub(:find).and_return(spree::paymentmethod.new)   @omnikassa.class.fetch_payment_method.should be_a_kind_of(spree::paymentmethod) end 

does not work. rather new whole bdd/tdd thing , stubbing , mocking still magical me; misunderstand stubbing , returning doing exactly.

how should stub someactiverecordmodel.find?

you doing correctly, except stub method should called on spree::paymentmethod class itself, not on instances

and it's common practice return instance stub, not new one:

it 'should find payment_method'   payment_meth = mock_model(spree::paymentmethod)   spree::paymentmethod.stub!(:find).and_return(payment_meth)    @omnikassa.class.fetch_payment_method.should be_equal(payment_meth) end 

and, way, initialize @omnikassa object?


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -