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 ?