Fixtures in RSpec

Kris picture Kris · Jul 27, 2012 · Viewed 32.3k times · Source

I'm new to using RSpec for writing tests in a Rails application which uses a MySQL database. I have defined my fixtures and am loading them in my spec as follows:

before(:all) do
  fixtures :student
end

Does this declaration save the data defined in my fixtures in the students table or does it just load the data in the table while the tests are running and remove it from the table after all the tests are run?

Answer

infused picture infused · Jul 11, 2014

If you want to use fixtures with RSpec, specify your fixtures in the describe block, not within a before block:

describe StudentsController do
  fixtures :students

  before do
    # more test setup
  end
end

Your student fixtures will get loaded into the students table and then rolled back at the end of each test using database transactions.