Rails remove persisted fixtures from tests - ruby-on-rails

I used to have 3 fixtures in my RSpec tests. I have removed them and went with the FactoryGirl approach. The problem is that, when I run my tests, even though I have no trace of fixtures left, they still appear when running the tests.
If I debug the tests, I can see that the fixtures' creation date is old, older than the objects created when running the current test.
I believe fixtures are somewhere cached, how can I clear this cache? Or, if this is not the case, why are the old fixtures there when running the tests?

rake db:setup will reload your test database from your schema.rb, erasing your fixture data.

After some deep digging, I found out that some sourced files were setting an env var with the development db, not the test db. Pretty trivial mistake, but so hard to find.
As a conclusion, if others stumble upon weird problems like this one, be sure to check whether you are using the right environment variables/configuration options in your app.

Related

Inconsistent Rails Test Results

The first couple times I run rspec spec, I receive failures and if I run it again and thereafter, it passes. Why is this happening and how do I fix it?
This appears to be related to uniqueness code to prevent adding a record with a name that has already been. Below is the test that consistently fails the first time:
it { should validate_uniqueness_of(:name).case_insensitive.with_message(/has already been taken. Please use a different name./) }
The full repo can be found here: https://github.com/melissajstudent/koth
You are right in thinking that the uniquess is a good indicator of what’s happening. Likely your test database has some remaining records from the last time you ran the tests and is trying to creat records with the same factories.
To that end, there are a few ways to clean your database in between runs of your test suites. Sometimes it’s helpful just to run bundle exec rake db:test:prepare Before running your test suite. In other cases you can implement some more robust database cleaning throughout your run with the database cleaner gem: https://github.com/DatabaseCleaner/database_cleaner

rails test database not clearing after some runs

I am using rspec to test and I notice that after running the tests, it sometimes leaves some records in the test database. I'm not sure why. I have use_transactional_fixtures set to true in my config file. But they don't go away until I manually delete the records. Does anyone have a way to stop this?
EDIT; before i said it was only when tests fail. that's no longer true.
I found the problem. Before(:all) blocks are not transactional
Try database_cleaner gem.
Truncation or Transaction strategy will work for you.
Caution: It can make your test suite run terribly slow.
Normally, proper use before, after in RSpec(if you are using it) works usually fine.

Ruby on Rails RSpec wipes the database

I'm kinda new to RSpec.
I wanted to run some example Rspec spec ( using rake spec command), having
config.use_transactional_fixtures = false
in configuration file, as it was recommended in some manual.
But, it still wipes the database and it just frustrates me, cause I had sensitive data in it and now it's all gone. Who the hell actually came up with an idea of clearing the database during the testing ?
How to avoid this behaviour ?
Thanks in advance!
Transactions are there to ensure your test database stays clean so that your tests stay clean and predictable. You should be using them. To turn them off for singular example groups, use self.use_transactional_fixtures = false after the describe line. You will however if you do this need a after(:each) block that cleans up afterwards.
I don't understand why you have sensitive data in your test database, sounds like you're doing something wrong there.

Rails 3 Rake Clone Database for Testing Environment

Is there a rake command in Rails 3 to clone my development database data? I noticed rake db:test:prepare and rake db:test:clone are mentioned throughout various blogs, but running them seems to do nothing. Furthermore, rake -T shows no db:test cases. I've resorted to loading a sql dump for now, but it would be great if I could just clone my existing development data for up-to-date testing.
EDIT --
I desire to test on a database since I am dealing with legacy data that I run through model filters when accessed. Factories won't work for me in this context, since data passed through create is defined as a different schema than that of the legacy data.
rake db:test:prepare is still there even though it doesn't show up in rake -Tdb. I guess the Rails team decided to de-clutter the rake -T output?
I would suggest you not clone your development database but rather rely on factories to give you predictable data you can craft for your exact test cases. Sooner or later, relying on having reliable test data in a database you can access will break your tests. It will also break the tests of anyone else who works on the project. And changes/additions to the data will not propagate to other developers as would your carefully constructed factories.
Look over Machinist, FixJour, FactoryGirl and the lot. They really solve the test data problem well and you check them into version control so the rest of your team has access to them.

Rails 2.3.5 table populated by fixtures at end of test run rather than at start

I start with a test database containing the schema but with no data in the tables. I run a test like so
cd test/
ruby unit/directive_test.rb
I get failures indicating that the code found no data in the data tables. However, I look at the tables after running that test and the data is now in the the table. In fact, if I immediately run the test again I get no failures.
So it appears that the fixture is being loaded into the table too late for one of my modules to find it.
When are the fixtures loaded? After or before the app/model/*.rb files are executed?
If it is after the models are executed is there a way to delay the loading?
This issue is also relevant when running rake test:units since that task clears the test data after it finished.
first of all see this thread and see if it can help you.
if you run the rail task rake test:units it will for sure load all fixtures before you run your code. if you are running just the test, and your unit test has no reference to the test_help.rb probably it is not loading the fixtures. You should try to run it through the rake tasks.
Another tip that i give you is that you forget the fixtures and use factories (here i recommend factory_girl). It takes sometime to get used, but it worth. Fixtures are too hard to manage, update and etc.
there is another post explaing little about the concept behind factories.

Resources