On 14 Jul 2014, at 20:05, Myron Marston <[email protected]> wrote:
> On Saturday, July 12, 2014 1:15:42 PM UTC-7, Javix wrote:
>> On 11 Jul 2014, at 17:53, Myron Marston wrote:
>>
>>> On Wednesday, July 9, 2014 1:24:33 PM UTC-7, 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
>>>
>>> The `Client.delete_all` line is problematic in that it runs at spec file
>>> load time, and many other examples can be run between that point and the
>>> examples you've listed run. If any other examples create client records
>>> you're going to hit problems like you're seeing. Instead, wrap it in a
>>> `before(:all)` hook if you want it to run once before all examples in that
>>> group.
>>>
>>> That said, I question the need for it at all; your environment is setup to
>>> truncate the DB before the first example runs and then wrap each example in
>>> a transaction, so there shouldn't be any client records in the DB to be
>>> deleted...unless you have some records being put into the DB outside the
>>> scope of an example (e.g. in an example group body or a `before(:all)`
>>> hook). And if you do have that kind of thing -- well, that's probably
>>> what's causing the problem.
>>>
>>> HTH,
>>> Myron
>> Thank you Myron for the reply.
>> So I removed completely Client_delete_all statement as it is managed by
>> DatabaseCleaner truncation feature.
>> And my features/ClientsControllerPage
>>
> WTF, it fails again...
>
> If you can put together a reproducible example, open an issue and we'll take
> a look. It's hard (nearly impossible) for us to debug remotely based on the
> limited information you've given us.
>
> Myron
Tank you Myron.
Just for history, I was going to take a try with RSpec features (never used
them before), instead of Cucumber's ones, that's why I was a little bit stuck
with that. So I just deleted RSpec 'features' folder with all the specs inside
and came back to Cucumber to be able to continue.
If I manage to check out a branch from the point where I had features available
on my project, I'll create a gist or even a branch and come back here with more
details.
Thank you again for your fed back.
>
> --
> You received this message because you are subscribed to a topic in the Google
> Groups "rspec" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/rspec/bwqHFUsldbg/unsubscribe.
> To unsubscribe from this group and all its topics, 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/6006d3c5-44bc-4a31-a958-818fea50d752%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
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/76DDDEE2-CF29-430D-B446-20750AB177E7%40gmail.com.
For more options, visit https://groups.google.com/d/optout.