[rspec-users] would I stub an existing class method out or use fixtures in this scenario?

Matt Wynne matt at mattwynne.net
Sun Oct 5 16:52:05 EDT 2008

On 5 Oct 2008, at 12:26, Greg Hauptmann wrote:

> Hi,
> Would the BDD experts recommend I either (a) use fixtures for data or
> (b) stub out existing class methods for such a scenario?
> Scenario = Working on a "populate for future transactions" method &
> testing this.  It generates "transactions" table rows as part of the
> test.  To do this it (a) requires BankAccount table row entries to be
> in place to ensure foreign key constraints allow new "transaction" to
> be added, i.e. with a bank_account_id which exists, and (b) ability to
> call the BankAccount class method "balance?(date)".
> Discussion = I've set things up with dynamic fixture type data using
> Ruby code (in the before(:each) do), however I was wondering whether
> in fact I should not be having to create such data (i.e. bank_account
> rows, when the real code under test is generating new "transaction"
> rows) but rather be "stubbing" out the existing methods on the
> BankAccount model (e.g.
> BankAccount.any_instance.stubs(:balance?).returns(<as required>)).
> Do people normal use the "any_instance.stubs" approach to stub out
> existing classes already developed, as a means to minimize associated
> test data that may have to be put in place to allow the test to work?
> Any other comments/suggestions re best practice here?

I think you should be able to do this with mocks, but it's not going  
to be easy. Are you using ActiveRecord?

I might be being thick, but I'm not really clear what the desired  
behaviour is from the question. Can you maybe post a spec --format  
specdoc of the relevant specs?

More information about the rspec-users mailing list