[rspec-users] DB transactions w/ RSpec

Francis Hwang sera at fhwang.net
Wed May 6 15:57:02 EDT 2009


Yeah, that worked for me, thanks. And for whatever reason nesting was
part of the problem, but if I put it all in one top-level describe
with the examples below it worked fine.

FH


On Wed, May 6, 2009 at 2:42 PM, Nicholas Van Weerdenburg
<vanweerd at gmail.com> wrote:
> The rails code is pretty straightforward:
>   def uses_transaction(*methods)
>         @uses_transaction = [] unless defined?(@uses_transaction)
>         @uses_transaction.concat methods.map(&:to_s)
>       end
>       def uses_transaction?(method)
>         @uses_transaction = [] unless defined?(@uses_transaction)
>         @uses_transaction.include?(method.to_s)
>       end
>     end
>     def run_in_transaction?
>       use_transactional_fixtures &&
>         !self.class.uses_transaction?(method_name)
>     end
> Works for me in my tests.
> uses_transaction "it should do this"
> it "should do this" do
>   puts "runs in transaction=#{self.run_in_transaction?}"
> end
> runs as expected, providing false when the uses_transaction is set, and true
> otherwise (assuming   config.use_transactional_fixtures = true).
> Regards,
> Nick
> On Wed, May 6, 2009 at 12:43 PM, Francis Hwang <sera at fhwang.net> wrote:
>>
>> On Wed, May 6, 2009 at 9:47 AM, Nicholas Van Weerdenburg
>> <vanweerd at gmail.com> wrote:
>> > As far as I can tell, RSpec simply uses inherited TestCase capabilities
>> > for
>> > transactions, including use_transactional_fixtures (it's config setting
>> > is
>> > simply passed on) and likely uses_transaction.
>> > I'm guessing you can do something like:
>> >
>> >     uses_transaction :create
>> >     it "should save message" do
>> >         ...
>> >     end
>> >
>> > At least it gives no exception for me.
>> > The TestCase logic is pretty simple- it skips transactional fixtures if
>> > the
>> > method is in it's array, so I imagine it works.
>> > If not, this shoulda ticket talks about the same need, with a patch that
>> > integrates it into shoulda contexts. That might give some clues.
>> > https://thoughtbot.lighthouseapp.com/projects/5807-shoulda/tickets/97
>>
>> Any thoughts on what uses_transaction wants as an argument? I can't
>> help but feel like I've tried 10 combinations of ways to do this, and
>> there's some 11th way that will be easy but I haven't thought to try.
>>
>> For context, my code doesn't even use transactions; it's using a 2nd
>> DB connection to a slave DB, which will be different in some envs and
>> not others. This seems to get tripped up by transactions since the
>> master connection makes a change to the DB and the slave connection
>> needs to see it right away. (Yes I know in production there are
>> latency issues, that's acceptable in this case.)
>>
>> > BTW- I happened to be watching your RubyConf2008 Testing Heresies talk
>> > yesterday. Good talk.
>>
>> Thanks!
>>
>> Francis
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list