Open source Rails apps that use fixtures - ruby-on-rails

I'm working on an existing app with a test suite based on vanilla Rails fixtures and MiniTest. Switching to FactoryGirl or the like is not an option.
I'd like to find a relatively complex open source Rails app to learn more about best practices with using fixtures. Any suggestions?

Take a look at https://github.com/Shopify/active_merchant, it also uses some fixtures

I've discovered Faker for myself, what is important:
It creates unique data every time while seeding database.
In some cases it does matter and better than same data. So, it is suitable for fixtures only;-)
It has handy templates for most cases e.g. phone.
You can check example in GitLab repo.

Related

What's wrong with Rspec, FactoryGirl and real sample data?

I've been leaning some of RoR, and when I got to TDD, stuff started to get more complicated. At some point of my App, I thought it would be better to run my tests over the real data.
Real Data vs Sample Data
Searching the web, I found that tests were not meant to run over real data, but over sample data. But yet I couldn't agree with that.
Let's supose my app had an Alias System. So when you access a random url it figures out what that fragment wants and redirects to the proper canonical url. And let's add that an alias dictionary is stored in some models. How would we test agains that dictionary? Hard code spec files for every alias/keyword?
Sticking with real data
The first two things I've realized, yet very unsurely, is:
Rspec testing environment wouldn't access development model's data.
FactoryGirl rules over my testing database, so it's not my option to populate it.
The best solution I could figure out, as the complete newbie I am, is that I could create some classes in spec/support folder and call them inside my factories so as to get that real data. Those classes have a short sample of my real database info, nested, and so my test can go 'real'.
What can pros around suggest to improve it?
I think you may want to look into building a seeds.rb file to populate your databases. This is usually used to initialize the development database so it can be used in your app (and queried in the rails console), but you can use it to seed your test database as described in this answer.
You certainly should not use your development database for testing. You can either seed the test database, or create factories that reflect various scenarios.
FactoryGirl rules over my testing database, so it's not my option to populate it.
You can use more than one factory to represent a business entity, depending on the scenario under test. FactoryGirl makes this easy by allowing you to nest factories. You can define a factory with a basic set of valid attributes and use that in unit tests. For integration (feature) tests, you can use a nested factory that expands on the basic attributes to implement a particular scenario. You can have as many variations of these implementation-specific factories as you need.

How to populate database with fake data?

I heard about gems like faker or populator but they are a little bit old (populator does not seem to play well with Rails 3).
So my question is, what do you use to generate fixtures ?
If you are interested in new fixture framework for ruby, take a look on Fabrication. It's looks like FactoryGirl Rails but have more interested features. We are using it with Faker and it's fine. I think it's the best solution nowadays :)
Most people use FactoryGirl Rails, which works nicely with Faker for doing things like random names, to make test data during their tests.
If sometimes you just want to populate your Dev DB with some fake data to make your application work, you can still use FactoryGirl Rails and make a Rake task that will populate some data you need.

How to seed the test database before to run Cucumber features?

I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0 and rspec-rails-2.8.1. Since I have some "system" data stored in the database, and since I would like to test my application that needs that data in order to properly work, I would like to seed the test database before to run Cucumber features.
How can I make that? What do you advice about?
Rails has a feature called Fixtures which prepopulates the test database before testing. Fixtures uses YAML to seed a table of the same name with data.
The Ruby on Rails guides have a low down on fixtures that may be beneficial to have a look at.
Thare is also FactoryGirl.
It can read from any place but generally uses features/factories.rb or features/factories/*.rb
See RailsCast 275. Ryan uses it with RSpec, but the principles are the same. Note FactoryGirl has had a major revision since then, so all the API has evolved.

What are best practices to handle database data in 'test' mode?

I am using Ruby on Rails 3.2.2, cucumber-rails-1.3.0, rspec-rails-2.8.1 and capybara-1.1.2. I have this problem but I started thinking that maybe I'm doing something wrong... mostly about seeding data in the test database for testing purposes. Specifically, my issue is related to how to properly manage data in the test database when I have to test my application.
My doubt is: By seeding data (For Your Information: I use the ROOT_PATH/db/seed.rb file to inject that data) in the test database I'm doing things as they should be done? That is, how should I populate the test database since the data in that database* is required in order to make to properly work my application for testing purposes? Should I populate the test database at all?
In other words, what are best practices to handle database data in test mode (in my case)? And, generally speaking, how the situation should be handled?
***** For example, in order to work my application requires at least data related to an "anonymous" user, to "basic" articles, to "basic" article categories, etc.
You should use one of the following:
Fixtures. See corresponding Rails documentation
Factories. The most popular tool to created/manage factories is FactoryGirl. IMHO that's the best solution.
Make sure data is seeded into test database. See this StackOverflow question.
I had a similar problem, association made it necessary to have a bit of seed data:
Factories will make your tests really slow, they are perfectly OK for single objects, but not for a lot of seed data that has to be created for each test
Fixture Builder - http://github.com/rdy/fixture_builder
I created a bunch of fixtures and just load them from the DB for each test, cut my test time down by 40%. You can also load the seeds file instead.
But be careful, deleting or updating records will create unwanted side effects. Use factories for those specs.
Mock and Stub everything so that your tests rarely touch the DB.
This has become very unpopular, you will eventually get passing specs that don't pick up on your actual errors.

factory_girl vs fixtures for development data (Rails)

I like the factory_girl approach in tests, but one thing I'm a little unclear on is what to do about development data.
Usually with fixtures we had some dummy users, models etc in development. Then in tests we can create more on the fly or reference the existing ones but every developer had the same data in dev.
What's the common way to create development data with factory_girl? I saw rake db:seed but this looks like it's intended to be used in production also, so not quite the same thing.
I generally create fake data using Factories inside seeds.rb.
I just prepend the code with if Rails.env.development?

Resources