On Tuesday, July 15, 2014 11:16:53 PM UTC-7, Javix wrote:
>
>
> On 16 Jul 2014, at 05:06, Aaron Kromer wrote:
>
> This is caused by your use of before(:all) blocks which create data in 
> the database. If you are using the :transaction strategy, which it seems 
> you are, then any data added in before(:all) is created prior to the 
> transaction. This also means it’s left over after the example. I’m guessing 
> you want to use the :all block for “performance reasons”. However, that 
> may not be where your time is being spent. For example, your “Clients Page 
> Has pagination and lists all clients” spec (
> spec/features/clients_page_spec.rb:24) took 13.25 seconds on my machine. 
> That time was *not* spent creating the objects, but instead verifying 
> everything on the page.
>
> If you really must use before(:all) then you need to manually clean it up 
> or change your configs. See the following references for more info:
>
>    - http://toctan.com/articles/be-careful-with-before(:all)-in-rspec/ 
>    - https://groups.google.com/forum/#!topic/database_cleaner/qUp5Hp6N0eY 
>    - 
>    
> http://www.ultrasaurus.com/2014/01/rspec-mixing-transcations-truncation-database-clearner-strategies/
>  
>
>
>
> On Tue, Jul 15, 2014 at 4:58 PM, Myron Marston  wrote:
>
>> On Tuesday, July 15, 2014 12:48:56 AM UTC-7, Javix wrote:
>>>
>>>
>>>
>>> On Wednesday, July 9, 2014 10:24:33 PM UTC+2, Javix wrote:
>>>>
>>>> I can't figure out why my example fails sometimes and sometimes not:
>>>>
>>>> describe ClientsController do
>>>>   let(:admin) { create(:admin) }
>>>>
>>>>   before(:each) { sign_in admin }
>>>>
>>>>   describe 'GET #index' do
>>>>     Client.delete_all
>>>>     let!(:clients) { Array.new(3) { create(:client) } }
>>>>     it "populates an array of all clients" do
>>>>
>>>>       get :index
>>>>       expect(assigns(:clients)).to match_array(clients)
>>>>     end
>>>>
>>>>     it "renders the :index template" do
>>>>       get :index
>>>>       expect(response).to render_template :index
>>>>     end
>>>>    end
>>>> end
>>>>
>>>> I set up DatabaseCleaner as follows in spec_helper:
>>>>
>>>> config.before(:suite) do
>>>>     DatabaseCleaner.clean_with(:truncation)
>>>>   end
>>>>
>>>>   config.before(:each) do
>>>>     DatabaseCleaner.strategy = :transaction
>>>>   end
>>>>
>>>>   config.before(:each, js: true) do
>>>>     DatabaseCleaner.strategy = :truncation
>>>>   end
>>>>
>>>>   config.before(:each) do
>>>>     DatabaseCleaner.start
>>>>   end
>>>>
>>>>   config.after(:each) do
>>>>     DatabaseCleaner.clean
>>>>   end
>>>>
>>>> and set up fixtures to false as well (spec_helper):
>>>>
>>>> config.use_transactional_fixtures = false
>>>>
>>>> No matter if I keep the line or not:
>>>>
>>>> Client.delete_all
>>>>
>>>> it fails the first time (when I run all the tests) and passes when I 
>>>> run the spec separately.
>>>>
>>>> I also defined a shared_db_connection in support folder:
>>>>
>>>> class ActiveRecord::Base
>>>>   mattr_accessor :shared_connection
>>>>   @@shared_connection = nil
>>>>
>>>>   def self.connection
>>>>     @@shared_connection || retrieve_connection
>>>>   end
>>>> end
>>>> ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
>>>>
>>>> For a more complete description of this setup, check out Avdi Grimm’s 
>>>> Virtuous Code blog:
>>>>   http://devblog.avdi.org/2012/08/31/configuring-database_
>>>> cleaner-with-rails-rspec-capybara-and-selenium/
>>>>
>>>> Any idea? THANK YOU
>>>>
>>>>
>>> @Myron: I pushed to remote a branch that has RSpec features implemented. 
>>> When running all the specs, tests failed (2). When running the same but 
>>> separately, they pass.
>>>
>>> You can clone the branch from https://github.com/Javix/
>>> jlc-invest/tree/rspec_features_back
>>>
>>> Thank you. 
>>>
>>
>> Thanks.  I cloned it and tried to run your specs, but I'm getting 187 
>> failures out of 190 specs.  They're all failing with errors like this:
>>
>> https://gist.github.com/myronmarston/ecc67c1a9f9c2ed89491
>>
>> I had to install postgres for this (I haven't used it in years) and I 
>> haven't used rails in years so I'm pretty rusty with debugging issues with 
>> this.  I also don't have much time to invest in this.  Sorry :(.
>>
>> Maybe someone else can volunteer to take a look at your project?
>>
>> Myron
>>
> Not a problem, Myron.
> For those who could take a look at the error, to set up the projet:
> *- have Postgresql installed*
> *- clone the 
> branch https://github.com/Javix/jlc-invest/tree/rspec_features_back 
> <https://github.com/Javix/jlc-invest/tree/rspec_features_back>*
> *- run bundle install*
> *- rake db:create*
> *- rake db:migrate*
> *- [optional] populate a database: rake db:populate (see 
> lib/tasks/sample_data.rake for more details).*
> *- rake db:test:prepare*
> *- rspec*
>
> Thank you
>

Thanks, I followed these steps and was able to run your specs. Not sure 
what I missed yesterday.  (Clearly it's been a long time since I worked on 
a rails app....).

Anyhow, Aaron's definitely right: the problem is that you have records that 
are being created in the DB outside of transaction and are "leaking" into 
other examples.  Any records created in the DB are essentially "global" 
state that has to be cleaned up between examples (or between example 
groups) and you're responsible for doing that.  With the database cleaner 
transaction strategy that you've hooked up for each example, it'll take 
care of that for any records created within an individual example but can't 
work for records created in the :all hooks.  I've created a PR that shows 
one solution for solving the problem:

https://github.com/Javix/jlc-invest/pull/1

Myron

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/407bfccb-64cf-4706-b510-7066aede38b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to