David Kahn
2010-Dec-12 01:11 UTC
Test/Unit integration test w Capybara - how to manually set data in the test
I am trying to do the following in a Test/Unit integration test using Capybara. Very basic Rails 3 app, Ruby 1.9.2 just with the pg and capybara gems, thats it. Everything works except that the Person object I create manually in the test does not show in the test database... or any other database I can find! But Person.all returns him, so he is getting created somewhere. Then, when Capybara/Selenium go to the browser which I have verified does hit the test database fine (through another test), I get an empty list of people on the index page. Capybara/Selenium have the Rails.env correct in terms of hitting the db correctly. The Test itself says Rails.env = "test", the data does not go into the test db. Where is the data getting saved? And how can I manually set up data in an Integration test before running Capybara? I thought maybe that Capybara was somehow triggering the database being emptied but the sequence below proves this is not the case: def test_see_yoohoo person = Person.new person.name = "Yoohoo" person.email = "yoohoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" assert person.save # passes assert Person.find_by_name("Yoohoo") # passes but in dbconsole "select * from people" in the test database (or dev database) returns no rows assert_equal Rails.env, "test" # passes visit "/people" # page loads fine.... Capybara/Selenium working.... assert page.has_content?(''Yoohoo'') # fails (page has empty list.... the list passes fine on another page so issue is not the view) assert page.has_content?(''yoohoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org'') end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Erol Fornoles
2010-Dec-12 01:38 UTC
Re: Test/Unit integration test w Capybara - how to manually set data in the test
On Sun, Dec 12, 2010 at 9:11 AM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote:> I am trying to do the following in a Test/Unit integration test using > Capybara. Very basic Rails 3 app, Ruby 1.9.2 just with the pg and capybara > gems, thats it. > > Everything works except that the Person object I create manually in the > test does not show in the test database... or any other database I can find! > But Person.all returns him, so he is getting created somewhere. Then, when > Capybara/Selenium go to the browser which I have verified does hit the test > database fine (through another test), I get an empty list of people on the > index page. > > Capybara/Selenium have the Rails.env correct in terms of hitting the db > correctly. The Test itself says Rails.env = "test", the data does not go > into the test db. Where is the data getting saved? And how can I manually > set up data in an Integration test before running Capybara? I thought maybe > that Capybara was somehow triggering the database being emptied but the > sequence below proves this is not the case: > > def test_see_yoohoo > person = Person.new > person.name = "Yoohoo" > person.email = "yoohoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" > assert person.save # passes > assert Person.find_by_name("Yoohoo") # passes but in dbconsole > "select * from people" in the test database (or dev database) returns no > rows > assert_equal Rails.env, "test" # passes > > visit "/people" # page > loads fine.... Capybara/Selenium working.... > assert page.has_content?(''Yoohoo'') # fails (page has empty > list.... the list passes fine on another page so issue is not the view) > assert page.has_content?(''yoohoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org'') > endTestUnit methods are wrapped in a transaction, with every transaction being rollbacked after the method finishes. You can set use_transactional_fixtures = false if you want to change this behavior. http://api.rubyonrails.org/classes/Fixtures.html HTH -- Erol M. Fornoles http://github.com/Erol http://twitter.com/erolfornoles http://ph.linkedin.com/in/erolfornoles -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2010-Dec-12 01:59 UTC
Re: Test/Unit integration test w Capybara - how to manually set data in the test
On Sat, Dec 11, 2010 at 7:38 PM, Erol Fornoles <erol.fornoles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> On Sun, Dec 12, 2010 at 9:11 AM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote: > >> I am trying to do the following in a Test/Unit integration test using >> Capybara. Very basic Rails 3 app, Ruby 1.9.2 just with the pg and capybara >> gems, thats it. >> >> Everything works except that the Person object I create manually in the >> test does not show in the test database... or any other database I can find! >> But Person.all returns him, so he is getting created somewhere. Then, when >> Capybara/Selenium go to the browser which I have verified does hit the test >> database fine (through another test), I get an empty list of people on the >> index page. >> >> Capybara/Selenium have the Rails.env correct in terms of hitting the db >> correctly. The Test itself says Rails.env = "test", the data does not go >> into the test db. Where is the data getting saved? And how can I manually >> set up data in an Integration test before running Capybara? I thought maybe >> that Capybara was somehow triggering the database being emptied but the >> sequence below proves this is not the case: >> >> def test_see_yoohoo >> person = Person.new >> person.name = "Yoohoo" >> person.email = "yoohoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" >> assert person.save # passes >> assert Person.find_by_name("Yoohoo") # passes but in dbconsole >> "select * from people" in the test database (or dev database) returns no >> rows >> assert_equal Rails.env, "test" # passes >> >> visit "/people" # >> page loads fine.... Capybara/Selenium working.... >> assert page.has_content?(''Yoohoo'') # fails (page has >> empty list.... the list passes fine on another page so issue is not the >> view) >> assert page.has_content?(''yoohoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org'') >> end > > > TestUnit methods are wrapped in a transaction, with every transaction being > rollbacked after the method finishes. > > You can set use_transactional_fixtures = false if you want to change this > behavior. >Thanks! At first I did not believe this was the issue but I tried and it worked. So what I learned is that even though my calls to Capybara are within a single method the transactional fixture object is its own world. So it seems the ''transaction'' never hits the db, right, so the tests are faster? And so when the browser boots this is hitting a new transaction... and thus the data I just created is gone.> http://api.rubyonrails.org/classes/Fixtures.html > > HTH > > -- > Erol M. Fornoles > http://github.com/Erol > http://twitter.com/erolfornoles > http://ph.linkedin.com/in/erolfornoles > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Dec-13 15:24 UTC
Re: Test/Unit integration test w Capybara - how to manually set data in the test
David Kahn wrote in post #967876:> I am trying to do the following in a Test/Unit integration test using > Capybara. Very basic Rails 3 app, Ruby 1.9.2 just with the pg and > capybara > gems, thats it.[...] Is there any particular reason that you are torturing yourself this way instead of using Cucumber? :) Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.