2

I'm new to Rails so probably I'm missing something obvoius... I'm trying to write a Rails app that uses Mongodb with the Mongoid gem.

I have some troubles with testing. I want to check if an html table generated from db content is correct.

I'm using Fabrication to generate data in the test db.

My problem is that the generated data is not available to the test. To have the test to fail/pass correctly I have to run it twice and avoid to empty the collection. But this causes troubles in other tests.

spec: require 'spec_helper'

describe "OperatorsPages" do
  let(:operator) { Fabricate(:operator) }
  subject { page }

  before do
    sign_in_operator operator
  end

  describe "index page " do
    before(:all) do
      3.times { Fabricate(:operator) }
      visit operators_path
    end


    after(:all) do
      Operator.all.destroy
    end

    Operator.all.each do |operator|
      it { should have_selector 'td', text: operator.name }
      it { should have_selector 'td', text: operator.email }
    end
  end
end

If the test runs against an empty db Operators.all.each.to_a is empty, so the assertions inside the loop aren't checked. If I run it a second time the loop works, but (obviously) only if i delete the after(:all) filter

How do I get this test to run correctly at the first time AND to leave an empty collection after ?

Mir
  • 1,575
  • 1
  • 18
  • 31
  • It looks like cache. Replace before(:all) / after(:all) with before(:each) / after(:each). You can try the gem factory_girl for generating data and the gem database_cleaner to automatically clean the database. – Aymeric Feb 22 '13 at 23:42
  • Nope. it doesn't works. I used FactoryGirl and database_cleaner at the beginning, but I switched to the actual setting trying to find the source of the problem. – Mir Feb 23 '13 at 13:16
  • I'm having the same problem :( `before :all` doesn't seem to work with Mongoid (documents are not persisted and "disappear" between tests). In my case it works if I use exclusively `before :each` when it comes to putting things in the DB. – alestanis Mar 10 '13 at 20:54

0 Answers0