Is there a way to keep rspec from trying to recreate the test database on each test iteration? I am developing a reporting app and I want to test against the legacy read only data.
Rails 3.1.1
rspec-rails 2.7.0
I thought I'd answer my own question in case it can help someone else.
I have given up trying to get rspec to use legacy, read only data, for testing. It appears this is "swimming against the tide" and is probably not considered to be the "Rails Way". I suppose it would violate the theory that your tests should not be dependent on external data.
So I am using a sqllite db and creating data in the tests.
Related
A contractor for our startup installed the Selenium-based Nightwatch testing framework, since our stack is React-heavy. But he told me that it could even be used to test our Rails code. A new contractor said, to the contrary, that Nightwatch couldn't do unit tests of our Rails controllers and models (which makes sense to me).
Who is right? Do you suppose the first programmer had in mind just that we would do end-to-end testing (certain inputs lead to certain outputs), and that we need not test the details of the Rails code? Do we, as I suspect and as the new contractor asserts, need RSpec or some other Ruby-based testing framework to handle our Rails code, if we want to be a TDD shop?
Yes it can be used to test Rails. But only from the outside (only through the Browser). So no Unit/Controller/View Tests.
You'll need MiniTest or Rspec for those.
My two cents (also see comment by #SteveCarey): Since I prefer to stick with what comes with Rails and use as little external tools as possible:
Have a look at System Tests that have been introduced with Rails 5.1 or, if you are on a older version, see if you can write those tests using Capybara/Integration Tests.
Update:
You can find more details on testing framework here: http://guides.rubyonrails.org/v4.1/testing.html#brief-note-about-minitest
It was Test::Unit and nowadays is Minitest. But the basics are the same so it does not really matter.
Another popular testing framework is RSpec. Which you can use instead of Minitest/TestUnit if you want to. I prefer Minitest but there are pros and cons for both frameworks.
Rails 4.2 came with Unit-tests, Functional/Controller-tests and Integration-tests. The built in thing that resembles Nightwatch the most are Integration-tests: http://guides.rubyonrails.org/v4.1/testing.html#integration-testing
You can also look at libraries such as Capybara (https://github.com/teamcapybara/capybara) which calls itself an Acceptance Test framework. It integrates nicely with TestUnit/Minitest/Rspec.
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.
I am writing integration tests for a Rails application using Watir and RSpec. What is the best way to set up data in the database that spans each test? For instance, I have the same user I want to use across a suite of tests. How can I seed that common data (e.g. user) before all the tests run and then clean it up for the next suite of tests?
Generally having some global data for tests is a bad idea and might introduce dependencies within tests (which is always a bad idea). To avoid that, each test should generate the data it needs for itself and not rely on some globally inserted data.
If you're using RSpec then it does some of the job for you (it even runs your tests in a random order to avoid dependencies) - it creates a database savepoint before running the test and rollbacks all the changes done by the test. Ideally this means that test database is empty before test runs and it will be empty after test has been run.
In Rails you could use Rails own fixtures to seed the needed data or use some third party gem like FactoryGirl.
Again, do not create any global data, which will be used by all tests because sooner or later this will come back to haunt you.
I believe Jeff talks about that in "Using the database" chapter of Cucumber & Cheese book.
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.
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.