Patrick J. Collins
2012-Feb-13 20:16 UTC
[rspec-users] stale records with integration testing?
Hi, I was writing an integration test for my user signup form (with capybara), and found that my test was failing due to a validation error: "email is already taken". I''m a bit confused because I thought when I run "rspec spec/some_spec.rb", the test database would be wiped clear? Is that not the case? Patrick J. Collins http://collinatorstudios.com
On Feb 13, 2012, at 1:16 PM, Patrick J. Collins wrote:> Hi, > > I was writing an integration test for my user signup form (with > capybara), and found that my test was failing due to a validation error: > "email is already taken". I''m a bit confused because I thought when I > run "rspec spec/some_spec.rb", the test database would be wiped clear? > > Is that not the case? > > Patrick J. Collins > http://collinatorstudios.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersYou basically have two options to ensure a clean database: 1.) Transactional examples: RSpec.configuration.use_transactional_examples = true 2.) DatabaseCleaner: RSpec.configure do |config| config.before { DatabaseCleaner.start } config.after { DatabaseCleaner.clean } end Look into what those do. Let us know if you get stuck.
David Chelimsky
2012-Feb-14 05:35 UTC
[rspec-users] stale records with integration testing?
On Mon, Feb 13, 2012 at 9:04 PM, Justin Ko <jko170 at gmail.com> wrote:> > On Feb 13, 2012, at 1:16 PM, Patrick J. Collins wrote: > >> Hi, >> >> I was writing an integration test for my user signup form (with >> capybara), and found that my test was failing due to a validation error: >> "email is already taken". ?I''m a bit confused because I thought when I >> run "rspec spec/some_spec.rb", the test database would be wiped clear? >> >> Is that not the case? >> >> Patrick J. Collins >> http://collinatorstudios.com >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > You basically have two options to ensure a clean database: > > 1.) Transactional examples: > > RSpec.configuration.use_transactional_examples = true > > 2.) DatabaseCleaner: > > RSpec.configure do |config| > ?config.before { DatabaseCleaner.start } > ?config.after { DatabaseCleaner.clean } > end > > Look into what those do. Let us know if you get stuck.What Justin says is true if you''re running in the same process. If you''re using Capybara to run examples in-browser, then option 2 will work for you, but option 1 will not.
Patrick J. Collins
2012-Feb-14 07:36 UTC
[rspec-users] stale records with integration testing?
> > 2.) DatabaseCleaner: > > > > RSpec.configure do |config| > > ?config.before { DatabaseCleaner.start } > > ?config.after { DatabaseCleaner.clean } > > end > > > > Look into what those do. Let us know if you get stuck. > > What Justin says is true if you''re running in the same process. If > you''re using Capybara to run examples in-browser, then option 2 will > work for you, but option 1 will not.Hmmm.. Well, I had actually tried the DatabaseCleaner gem prior to asking my question, and I saw no difference in the results... I just put that code back in spec_helper, and ran my specs and they fail again with "email already taken". It seems to break unless I explicitly put: User.all.map(&:destroy) in a before block........ Is there any reason why DatabaseCleaner might not be working? I''m on Rails 3.2 with rspec 2.8.0, and database_cleaner 0.7.1 Patrick J. Collins http://collinatorstudios.com
David Chelimsky
2012-Feb-14 11:31 UTC
[rspec-users] stale records with integration testing?
On Tue, Feb 14, 2012 at 1:36 AM, Patrick J. Collins <patrick at collinatorstudios.com> wrote:>> > 2.) DatabaseCleaner: >> > >> > RSpec.configure do |config| >> > ?config.before { DatabaseCleaner.start } >> > ?config.after { DatabaseCleaner.clean } >> > end >> > >> > Look into what those do. Let us know if you get stuck. >> >> What Justin says is true if you''re running in the same process. If >> you''re using Capybara to run examples in-browser, then option 2 will >> work for you, but option 1 will not. > > Hmmm.. ?Well, I had actually tried the DatabaseCleaner gem prior to > asking my question, and I saw no difference in the results... ?I just > put that code back in spec_helper, and ran my specs and they fail again > with "email already taken".This could happen for one of two reasons: 1. The email is already in the database before you run your specs. To avoid this, use DatabaseCleaner to truncate the tables once before each run (not before each example). 2. Models are generated in before(:all) blocks, which are not implicitly wrapped in transactions. If you generate models in before(:all), then it is your responsibility to remove them in an after(:all). Also - DatabaseCleaner has two modes: transaction and truncation. Make sure you''re using the appropriate one. HTH, David
On Feb 13, 2012, at 10:35 PM, David Chelimsky wrote:> On Mon, Feb 13, 2012 at 9:04 PM, Justin Ko <jko170 at gmail.com> wrote: >> >> On Feb 13, 2012, at 1:16 PM, Patrick J. Collins wrote: >> >>> Hi, >>> >>> I was writing an integration test for my user signup form (with >>> capybara), and found that my test was failing due to a validation error: >>> "email is already taken". I''m a bit confused because I thought when I >>> run "rspec spec/some_spec.rb", the test database would be wiped clear? >>> >>> Is that not the case? >>> >>> Patrick J. Collins >>> http://collinatorstudios.com >>> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-users at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> You basically have two options to ensure a clean database: >> >> 1.) Transactional examples: >> >> RSpec.configuration.use_transactional_examples = true >> >> 2.) DatabaseCleaner: >> >> RSpec.configure do |config| >> config.before { DatabaseCleaner.start } >> config.after { DatabaseCleaner.clean } >> end >> >> Look into what those do. Let us know if you get stuck. > > What Justin says is true if you''re running in the same process. If > you''re using Capybara to run examples in-bro.wser, then option 2 will > work for you, but option 1 will not.You can still do this with an AR patch. Look at the "Transactions and database setup" in the Capybara README. Using DatabaseCleaner with truncation is SLOW.> _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2012-Feb-14 16:23 UTC
[rspec-users] stale records with integration testing?
On Tue, Feb 14, 2012 at 9:28 AM, Justin Ko <jko170 at gmail.com> wrote:> > On Feb 13, 2012, at 10:35 PM, David Chelimsky wrote: > >> On Mon, Feb 13, 2012 at 9:04 PM, Justin Ko <jko170 at gmail.com> wrote: >>> >>> On Feb 13, 2012, at 1:16 PM, Patrick J. Collins wrote: >>> >>>> Hi, >>>> >>>> I was writing an integration test for my user signup form (with >>>> capybara), and found that my test was failing due to a validation error: >>>> "email is already taken". ?I''m a bit confused because I thought when I >>>> run "rspec spec/some_spec.rb", the test database would be wiped clear? >>>> >>>> Is that not the case? >>>> >>>> Patrick J. Collins >>>> http://collinatorstudios.com >>>> >>>> _______________________________________________ >>>> rspec-users mailing list >>>> rspec-users at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/rspec-users >>> >>> You basically have two options to ensure a clean database: >>> >>> 1.) Transactional examples: >>> >>> RSpec.configuration.use_transactional_examples = true >>> >>> 2.) DatabaseCleaner: >>> >>> RSpec.configure do |config| >>> ?config.before { DatabaseCleaner.start } >>> ?config.after { DatabaseCleaner.clean } >>> end >>> >>> Look into what those do. Let us know if you get stuck. >> >> What Justin says is true if you''re running in the same process. If >> you''re using Capybara to run examples in-bro.wser, then option 2 will >> work for you, but option 1 will not. > > You can still do this with an AR patch. Look at the "Transactions and database setup" in the Capybara README."This may have thread safety implications and could cause strange failures, so use caution with this approach."> Using DatabaseCleaner with truncation is SLOW.True, but you can minimize that by using transaction by default, and specifying truncation for in-browser scenarios (which are already far slower than will be impacted by truncation).> >> _______________________________________________ >> 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
Patrick J. Collins
2012-Feb-14 18:02 UTC
[rspec-users] stale records with integration testing?
> This could happen for one of two reasons: > > 1. The email is already in the database before you run your specs. > > To avoid this, use DatabaseCleaner to truncate the tables once before > each run (not before each example). > > 2. Models are generated in before(:all) blocks, which are not > implicitly wrapped in transactions. > > If you generate models in before(:all), then it is your responsibility > to remove them in an after(:all). > > Also - DatabaseCleaner has two modes: transaction and truncation. Make > sure you''re using the appropriate one.Ah... Ok cool, all of the above info solved my problem. Thanks! Patrick J. Collins http://collinatorstudios.com
On Feb 14, 2012, at 9:23 AM, David Chelimsky wrote:> On Tue, Feb 14, 2012 at 9:28 AM, Justin Ko <jko170 at gmail.com> wrote: >> >> On Feb 13, 2012, at 10:35 PM, David Chelimsky wrote: >> >>> On Mon, Feb 13, 2012 at 9:04 PM, Justin Ko <jko170 at gmail.com> wrote: >>>> >>>> On Feb 13, 2012, at 1:16 PM, Patrick J. Collins wrote: >>>> >>>>> Hi, >>>>> >>>>> I was writing an integration test for my user signup form (with >>>>> capybara), and found that my test was failing due to a validation error: >>>>> "email is already taken". I''m a bit confused because I thought when I >>>>> run "rspec spec/some_spec.rb", the test database would be wiped clear? >>>>> >>>>> Is that not the case? >>>>> >>>>> Patrick J. Collins >>>>> http://collinatorstudios.com >>>>> >>>>> _______________________________________________ >>>>> rspec-users mailing list >>>>> rspec-users at rubyforge.org >>>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>> >>>> You basically have two options to ensure a clean database: >>>> >>>> 1.) Transactional examples: >>>> >>>> RSpec.configuration.use_transactional_examples = true >>>> >>>> 2.) DatabaseCleaner: >>>> >>>> RSpec.configure do |config| >>>> config.before { DatabaseCleaner.start } >>>> config.after { DatabaseCleaner.clean } >>>> end >>>> >>>> Look into what those do. Let us know if you get stuck. >>> >>> What Justin says is true if you''re running in the same process. If >>> you''re using Capybara to run examples in-bro.wser, then option 2 will >>> work for you, but option 1 will not. >> >> You can still do this with an AR patch. Look at the "Transactions and database setup" in the Capybara README. > > "This may have thread safety implications and could cause strange > failures, so use caution with this approach." > >> Using DatabaseCleaner with truncation is SLOW. > > True, but you can minimize that by using transaction by default, and > specifying truncation for in-browser scenarios (which are already far > slower than will be impacted by truncation).cucumber-rails has good examples on how to set this up in RSpec: https://github.com/cucumber/cucumber-rails/blob/master/lib/cucumber/rails/database.rb It includes AR shared connection and truncation strategies. David, how come rspec-rails doesn''t include support for these things as well? I don''t really mind setting it up manually, but it does seem to be a necessary pattern for request specs.> >> >>> _______________________________________________ >>> 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 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On 14 Feb 2012, at 20:44, Justin Ko wrote:> > On Feb 14, 2012, at 9:23 AM, David Chelimsky wrote: > >> On Tue, Feb 14, 2012 at 9:28 AM, Justin Ko <jko170 at gmail.com> wrote: >>> >>> On Feb 13, 2012, at 10:35 PM, David Chelimsky wrote: >>> >>>> On Mon, Feb 13, 2012 at 9:04 PM, Justin Ko <jko170 at gmail.com> wrote: >>>>> >>>>> On Feb 13, 2012, at 1:16 PM, Patrick J. Collins wrote: >>>>> >>>>>> Hi, >>>>>> >>>>>> I was writing an integration test for my user signup form (with >>>>>> capybara), and found that my test was failing due to a validation error: >>>>>> "email is already taken". I''m a bit confused because I thought when I >>>>>> run "rspec spec/some_spec.rb", the test database would be wiped clear? >>>>>> >>>>>> Is that not the case? >>>>>> >>>>>> Patrick J. Collins >>>>>> http://collinatorstudios.com >>>>>> >>>>>> _______________________________________________ >>>>>> rspec-users mailing list >>>>>> rspec-users at rubyforge.org >>>>>> http://rubyforge.org/mailman/listinfo/rspec-users >>>>> >>>>> You basically have two options to ensure a clean database: >>>>> >>>>> 1.) Transactional examples: >>>>> >>>>> RSpec.configuration.use_transactional_examples = true >>>>> >>>>> 2.) DatabaseCleaner: >>>>> >>>>> RSpec.configure do |config| >>>>> config.before { DatabaseCleaner.start } >>>>> config.after { DatabaseCleaner.clean } >>>>> end >>>>> >>>>> Look into what those do. Let us know if you get stuck. >>>> >>>> What Justin says is true if you''re running in the same process. If >>>> you''re using Capybara to run examples in-bro.wser, then option 2 will >>>> work for you, but option 1 will not. >>> >>> You can still do this with an AR patch. Look at the "Transactions and database setup" in the Capybara README. >> >> "This may have thread safety implications and could cause strange >> failures, so use caution with this approach." >> >>> Using DatabaseCleaner with truncation is SLOW. >> >> True, but you can minimize that by using transaction by default, and >> specifying truncation for in-browser scenarios (which are already far >> slower than will be impacted by truncation). > > cucumber-rails has good examples on how to set this up in RSpec: > https://github.com/cucumber/cucumber-rails/blob/master/lib/cucumber/rails/database.rb > > It includes AR shared connection and truncation strategies. > > David, how come rspec-rails doesn''t include support for these things as well? I don''t really mind setting it up manually, but it does seem to be a necessary pattern for request specs.Probably because it''s crazy complicated. If you do want to support it too, let''s work out a way to factor it out into a single shared library. FYI the feature for it is here: https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120216/57ab0d6d/attachment.html>
David Chelimsky
2012-Feb-16 12:21 UTC
[rspec-users] stale records with integration testing?
On Thu, Feb 16, 2012 at 4:17 AM, Matt Wynne <matt at mattwynne.net> wrote:> > On 14 Feb 2012, at 20:44, Justin Ko wrote: > > > On Feb 14, 2012, at 9:23 AM, David Chelimsky wrote: > > On Tue, Feb 14, 2012 at 9:28 AM, Justin Ko <jko170 at gmail.com> wrote: > > > On Feb 13, 2012, at 10:35 PM, David Chelimsky wrote: > > > On Mon, Feb 13, 2012 at 9:04 PM, Justin Ko <jko170 at gmail.com> wrote: > > > On Feb 13, 2012, at 1:16 PM, Patrick J. Collins wrote: > > > Hi, > > > I was writing an integration test for my user signup form (with > > capybara), and found that my test was failing due to a validation error: > > "email is already taken". ?I''m a bit confused because I thought when I > > run "rspec spec/some_spec.rb", the test database would be wiped clear? > > > Is that not the case? > > > Patrick J. Collins > > http://collinatorstudios.com > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > You basically have two options to ensure a clean database: > > > 1.) Transactional examples: > > > RSpec.configuration.use_transactional_examples = true > > > 2.) DatabaseCleaner: > > > RSpec.configure do |config| > > config.before { DatabaseCleaner.start } > > config.after { DatabaseCleaner.clean } > > end > > > Look into what those do. Let us know if you get stuck. > > > What Justin says is true if you''re running in the same process. If > > you''re using Capybara to run examples in-bro.wser, then option 2 will > > work for you, but option 1 will not. > > > You can still do this with an AR patch. Look at the "Transactions and > database setup" in the Capybara README. > > > "This may have thread safety implications and could cause strange > > failures, so use caution with this approach." > > > Using DatabaseCleaner with truncation is SLOW. > > > True, but you can minimize that by using transaction by default, and > > specifying truncation for in-browser scenarios (which are already far > > slower than will be impacted by truncation). > > > cucumber-rails has good examples on how to set this up in RSpec: > https://github.com/cucumber/cucumber-rails/blob/master/lib/cucumber/rails/database.rb > > It includes AR shared connection and truncation strategies. > > David, how come rspec-rails doesn''t include support for these things as > well? I don''t really mind setting it up manually, but it does seem to be a > necessary pattern for request specs. > > > Probably because it''s crazy complicated.Seems so, but worthwhile. Also, I wasn''t aware of the shared connection strategy before this thread.> If you do want to support it too, > let''s work out a way to factor it out into a single shared library.I like this idea but am seriously overcommitted right now. Can you two coordinate on this?> FYI the > feature for it is here: > https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature > > cheers, > Matt
On 16 Feb 2012, at 12:21, David Chelimsky wrote:> On Thu, Feb 16, 2012 at 4:17 AM, Matt Wynne <matt at mattwynne.net> wrote: >> >> On 14 Feb 2012, at 20:44, Justin Ko wrote: >> >> >> On Feb 14, 2012, at 9:23 AM, David Chelimsky wrote: >> >> On Tue, Feb 14, 2012 at 9:28 AM, Justin Ko <jko170 at gmail.com> wrote: >> >> >> On Feb 13, 2012, at 10:35 PM, David Chelimsky wrote: >> >> >> On Mon, Feb 13, 2012 at 9:04 PM, Justin Ko <jko170 at gmail.com> wrote: >> >> >> On Feb 13, 2012, at 1:16 PM, Patrick J. Collins wrote: >> >> >> Hi, >> >> >> I was writing an integration test for my user signup form (with >> >> capybara), and found that my test was failing due to a validation error: >> >> "email is already taken". I''m a bit confused because I thought when I >> >> run "rspec spec/some_spec.rb", the test database would be wiped clear? >> >> >> Is that not the case? >> >> >> Patrick J. Collins >> >> http://collinatorstudios.com >> >> >> _______________________________________________ >> >> rspec-users mailing list >> >> rspec-users at rubyforge.org >> >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> >> You basically have two options to ensure a clean database: >> >> >> 1.) Transactional examples: >> >> >> RSpec.configuration.use_transactional_examples = true >> >> >> 2.) DatabaseCleaner: >> >> >> RSpec.configure do |config| >> >> config.before { DatabaseCleaner.start } >> >> config.after { DatabaseCleaner.clean } >> >> end >> >> >> Look into what those do. Let us know if you get stuck. >> >> >> What Justin says is true if you''re running in the same process. If >> >> you''re using Capybara to run examples in-bro.wser, then option 2 will >> >> work for you, but option 1 will not. >> >> >> You can still do this with an AR patch. Look at the "Transactions and >> database setup" in the Capybara README. >> >> >> "This may have thread safety implications and could cause strange >> >> failures, so use caution with this approach." >> >> >> Using DatabaseCleaner with truncation is SLOW. >> >> >> True, but you can minimize that by using transaction by default, and >> >> specifying truncation for in-browser scenarios (which are already far >> >> slower than will be impacted by truncation). >> >> >> cucumber-rails has good examples on how to set this up in RSpec: >> https://github.com/cucumber/cucumber-rails/blob/master/lib/cucumber/rails/database.rb >> >> It includes AR shared connection and truncation strategies. >> >> David, how come rspec-rails doesn''t include support for these things as >> well? I don''t really mind setting it up manually, but it does seem to be a >> necessary pattern for request specs. >> >> >> Probably because it''s crazy complicated. > > Seems so, but worthwhile. Also, I wasn''t aware of the shared > connection strategy before this thread. > >> If you do want to support it too, >> let''s work out a way to factor it out into a single shared library. > > I like this idea but am seriously overcommitted right now. Can you two > coordinate on this?I know how you feel, but I will try to help, yes. I think reading the feature is a good place to start: that''s where I dumped my understanding of the problem when I last worked on it:>> https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.featurecheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120217/e83b597b/attachment-0001.html>