Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixtures in RSpec

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?

like image 251
Kris Avatar asked Jul 27 '12 08:07

Kris


People also ask

What are RSpec fixtures?

Fixtures allow you to define a large set of sample data ahead of time and store them as YAML files inside your spec directory, inside another directory called fixtures. When you're test sweep first starts up RSpec will use those fixture files to prepopulate your database tables.

What are fixtures in rails?

Fixtures are data that you can feed into your unit testing. They are automatically created whenever rails generates the corresponding tests for your controllers and models. They are only used for your tests and cannot actually be accessed when running the application.

What is Factory in RSpec?

RSpec provides an easy API to write assertions for our tests, while Factory Bot allows us to create stubbed in data for our tests.

What is let in RSpec?

let generates a method whose return value is memoized after the first call. This is known as lazy loading because the value is not loaded into memory until the method is called. Here is an example of how let is used within an RSpec test. let will generate a method called thing which returns a new instance of Thing .


1 Answers

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.

like image 165
infused Avatar answered Oct 09 '22 03:10

infused