[rspec-users] Specifying that code is called in a block

Lugovoi Nikolai meadow.nnick at gmail.com
Fri Mar 2 12:41:47 EST 2007


What about such specification?
  specify "cow, dog and duck must be saved in the same transaction" do
    $log.clear
    @connection.stub!(:begin_db_transaction).and_return {
$log.record("BEGIN"); nil }
    @connection.stub!(:commit_db_transaction).and_return {
$log.record("COMMIT"); nil }
    @cow.stub!(:save!).and_return  {$log.record("COW SAVE"); true}
    @dog.stub!(:save!).and_return  {$log.record("DOG SAVE"); true}
    @duck.stub!(:save!).and_return {$log.record("DUCK SAVE"); true}

    @tested_object.exec_method_about_cow_dog_and_duck

    txn_intervals = %w(COW DUCK DOG).collect do |beast|
      [ $log.find("BEGIN", :before, "#{beast} SAVE"),
$log.find("COMMIT", :after, "#{beast} SAVE")]
    end

    txn_intervals.each do |range|
      range[0].should_be kind_of(Time)
      range[1].should_be kind_of(Time)
    end
    txn_intervals.uniq.size.should == 1
  end

where $log can be something like:
    $log = Struct.new(:events, :timestamps).new
    def $log.clear
      events = []
      timestamps = []
    end
    def $log.record(what)
      events << what
      timestamps << Time.now
    end
    def $log.find(what, where, start)
     # return timestamp of event `what' that is :before or :after
(where)  event `start'
    end

2007/2/28, Ashley Moran <work at ashleymoran.me.uk>:
> Not sure if this is possible currently.
>
> I have a section of code like this:
>
>    ActiveRecord::Base.transaction do
>      cow.save!
>      duck.save!
>      dog.save!
>    end
>
> (Names changed to protect the innocent.)
>
> I'd like to specify that the saves run in a transaction.  I can do
>
>    ActiveRecord::Base.should_receive(:transaction).and_yield
>
> But is there any way to specify that the code is called as above, and
> not as
>
>    cow.save!
>    ActiveRecord::Base.transaction do
>      duck.save!
>    end
>    dog.save!
>
> ?
>
> Ta
> Ashley
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list