Rails 2.3.5 table populated by fixtures at end of test run rather than at start - ruby-on-rails

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.

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 remove persisted fixtures from tests

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.

getting rake test to seed the database first

How do I get rails to call db:seed before running my test suite? It appears that some task is being called that recreates the db, but doesn't call seed. Calling rake db:reset does both - it rebuilds the db and re-runs the seed scripts. How do I do this as part of rake test, or at the very least prevent test from resetting the db?
So I finally figured this out on my own (spent two days chasing). The db was being seeded from seeds.rb, but I happened to have empty fixture yaml files for the tables I was looking at. Looking at this SO question gave me the idea to delete them, and voila - seed data abounds.
I won't accept this answer for a little while to give someone a chance to provide a better explanation, but thought I'd throw this out 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.2 unit test passes when run with normal ruby, fails when run with rake test:units

When creating a record in a unit test, I'm getting an 'ActiveRecord::RecordInvalid' when it's run with rake test:units. I don't get this error when I run the unit test manually with ruby ('ruby unit/blah_test.rb'). I've narrowed the issue down to a validation that exists in my model. It is an inclusion validation which is actually performing a find against a different model to get the valid values. I'm thinking this is related to the fixtures not being loaded in time, but I do have 'fixtures :all' in my test_helper.rb (I've also tried including 'fixtures :all' in the unit test itself.
Does anyone have any suggestions on how I can try to narrow this down even further?
Thanks.
Certainly what you are seeing would fit with fixtures being missing. With rake test:units the test database schema will be set up (and cleared) so your included model's fixtures may not be populated. With the direct call you'll be using the test database in the state you last left it which probably does include the fixutres for the included model.
Is there another call to fixtures in the test class which may be causing fixtures :all not to kick in?
You could try doing rake db:test:prepare prior to running your test via ruby which would mean you were running on a fresh test database. This would further highlight if the fixtures aren't being loaded for your included model.

Resources