Forum: RSpec Cucumber model expectations fail to find newly created users

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6bf9328eef0afadd9c9d05334f8dd42b?d=identicon&s=25 Hubert Lepicki (hubertlepicki)
on 2009-01-04 20:13
Hi guys,

I have strange problem with getting model expectations to work with
Cucumber. My setup is that I use Cucumber + Webrat (Selenium backend).

Story I test is registering user on site. Cucumber runs story perfectly
fine, and I see it fills in form, sends it, I even see proper "You have
successfully registered" message at the end. However, as a last element
of story I want to make sure the user was really created in database -
and this fails.

I have definition of following step:

Then /^should exist exactly "(.*)" users$/ do |cnt, state|
  User.count.should == cnt.to_i
end

However, it always complains that there are 0 users, when expected X.

What is even more stranger, when I log into console, and do User.count,
I get proper number of users - they were actually created during running
a story!

Any ideas?
6bf9328eef0afadd9c9d05334f8dd42b?d=identicon&s=25 Hubert Lepicki (hubertlepicki)
on 2009-01-04 20:15
The step definition is actually:

Then /^should exist exactly "(.*)" users$/ do |cnt|
  User.count.should == cnt.to_i
end

but that doesn't change anything, please help!
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2009-01-04 20:49
(Received via mailing list)
On 1/4/09 12:13 PM, Hubert Lepicki wrote:
> Hi guys,
>
> I have strange problem with getting model expectations to work with
> Cucumber. My setup is that I use Cucumber + Webrat (Selenium backend).
>

My guess is that you are using Selenium in conjuction with rails
transactional fixtures turned on.  In your env.rb file do you have the
following?
Cucumber::Rails.use_transactional_fixtures

If so that is your problem. What is happending is that you two separate
processes and two separate DB connections going.  One is running the
server for selenium, and the other is for your features.  With
transactional_fixtures turned on all of your DB calls for each scenario
are wrapped in into a transaction that is rolled back at the end.  Be
default MySQL, and most other DBs I would imagine, don't let the data in
a transaction appear to any other queries until that transaction is
actually committed. That is why you can see the correct count in the
process running the features but not the selenium process.

How do you get around this?  You could change your settings on MySQL,
but I wouldn't suggest that.   What I do is turn off
transactional_fixtures and then manage the DB cleanup myself in After
blocks.  The quick and dirty way to do this is to truncate your tables..
There have been some threads on this list explaining how to do this.
There are some other ways to accomplish this without resorting to
truncating everytime too, but I will let you google and find those
threads.

This problem seems to come up quite a bit.. we should probably add this
to the Troubleshooting page on Cucumber's wiki....

Anyways, I hope that helps.

-Ben
6bf9328eef0afadd9c9d05334f8dd42b?d=identicon&s=25 Hubert Lepicki (hubertlepicki)
on 2009-01-04 21:16
Ben Mabey wrote:
> My guess is that you are using Selenium in conjuction with rails
> transactional fixtures turned on.  In your env.rb file do you have the
> following?
> Cucumber::Rails.use_transactional_fixtures

That was a perfect guess! Thank you, Ben, a lot!

I have updated Troubleshooting page in Github wiki like you suggested so
that other people might find solution quicker than me :).

One more time - thanks!
This topic is locked and can not be replied to.