0

Like suggested from the title, I've a strange mistake. In fact when i try to make an operation reserved to the test enviroment (for example: rake test:prepare), in some way it influence the other environments and delete all the record in my database (mysql). (I'm using rspec, and i'm following the tutorials from Michael Hartl.) Another example (maybe clearer) is when I write the tests that implicate the creation and the cancellation of new users throught FactoryGirl:

Location: spec/factories.rb

FactoryGirl.define do

  factory :user do

  sequence(:name)          {|n| "Person #{n}"}
  sequence(:email)         {|n| "person_#{n}@example.it"}
  password                 "luckyluke"
  password_confirmation    "luckyluke"

  factory :admin do
    admin true
  end
end

Location: spec/requests/users_pages_spec.rb

describe "pagination" do

  before(:all) { 30.times { FactoryGirl.create(:user) } }
  after(:all)  { User.delete_all }

  # tests...
end

Although I'm into the test enviroment, when i run bundle exec rspec spec/requests/users_pages_spec.rb , rails really create 30 users on my database, and ater really delete ALL the users from database. (Also the users created and stored before the test!!).

P.S. sorry for my english

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93

1 Answers1

0

Mocking the database should happen before each test, to ensure isolation. This way, FactoryGirl is also responsible for deleting all its created (as is described in this answer):

describe "pagination" do

  before(:each) { 30.times { FactoryGirl.create(:user) } }

  # tests...
end

Also, it is advisable that the database will not contain any data from before the test, since it might change the behavior of the tests - it should contain only what the test expects it to contain.

Community
  • 1
  • 1
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93