Testing a Rails app without a database - ruby-on-rails

I have a Rails app (Rails - 2.3.8, Linux) which uses a MySQL database, but the database is generated separately (not through the Rails migration), so I would like to know
How can I implement testing on this application (please note currently there are no test suit at all)?
I would prefer not to have a database at all and test the functionality (including ActiveRecord models through the unit test) (because it will make the test suit independent as I see).
Currently I found this gem (temping - https://github.com/jpignata/temping) via a Stack Overflow link itself.
Please let me know if I'm going in a wrong direction.

This is a great resource on one method of TDD using RSpec which is more powerful than unit testing imo.
http://railscasts.com/episodes/275-how-i-test
If you are avoiding using MySQL you could simply specify Sqlite3 in your Database.yaml file:
test:
adapter: sqlite3
encoding: unicode
database: gc_test
pool: 5
Be sure to include Gem:
gem 'sqlite3-ruby'
If you have having trouble with this install, checkout this question:
Why can't I install the SQLite gem?

Related

switching from sqlite to postgres in rails app I´m working on

I am rather new to rails and I was thinking about how much trouble is it to switch from sqlite to postgres in an app that I´m currently working on. The thing is I have never switched in a middle of the building App process.
My local database has some data, but it is not relevant and may be deleted. I´m more concerned about the tables, columns, models, etc...
My reasons for the switch are:
I want to be able to use the 'groupdate' gem with my 'chartkick' gem in developement.
Later I will deploy to Heroku, I have done that before with adding 'pg' gem to production.
I have Postgres installed on my computer and I also have Postgres.app. I´ve been testing chartkick with groupdate in another project, so Postgres is not the problem.
So the question is basically, how much trouble is to switch from sqlite to PG in a middle of a project and what is the easiest and fastest way to do it?
sorry for the long text, hope someone reads it :)

Ruby on Rails. Predefined data installation in DB

I am new with Ruby on Rails and have some issues with it. I am writing my application on Rails and i have to install some pre-defined data in my Database to use it in my application. This data is read-only and never change (for example users and roles. There are no use cases to create new users and roles).
So i have to install this data when my application runs first time. I tried to use Migrations to solve this problem, but my manager told me that it is not a right way, cause migrations are usually used to define DB structure changes and not to install any data in DB.
Can you please help me and tell better way to install my pre-defined data using Ruby on Rails?
You'll be able to achieve this using the seeds functionality:
To add initial data after a database is created, Rails has a built-in 'seeds' feature that makes the process quick and easy. This is especially useful when reloading the database frequently in development and test environments. It's easy to get started with this feature: just fill up db/seeds.rb with some Ruby code, and run rake db:seed:
#db/seeds.rb
User.create name: "test", description: "info"
$ rake db:seed
--
If you wanted static data, you'll be able to use a gem such as the config gem -- which gives you the ability to allocate values in config/settings.yml:
This data is then accessible as Settings.company...
You should use seeds. See this rails cast
You could also look at Dibber, that allows you to define your seeds via YML files.
What you are looking to do is seed the database. Rails comes with a db/seeds.rb that you can use to do this. There are also gems such as seed-fu that you could use.
Here is a short tutorial on seeding a database in Rails.

How to disable sqlite3 while using neoid?

I am using neoid with rails and want to disable sqlite in my rails application. How can I do this (after that I want to use only neoid and neo4j to store the data)?
Remove
gem 'sqlite3'
from your Gemfile and make sure that your config/database.yml file doesn't contain lines like
adapter: sqlite
From my testing with it today I don't think you can do this with this gem. Neoid adds neo4j storage as an addition to, not replacement for, your traditional RDBMS.
To have a full replacement for RDBMS-backed ActiveRecord you may need to consider the neo4j.rb gem, which requires JRuby.

Rails - disable db connections

There's a Rails 3.2.3 web application which doesn't use any database. But in spite of that if I use the gem 'sqllite3' in GemFile I works perfect. But if I use gem 'pg' in that file it throws an error
ActiveRecord::ConnectionNotEstablished
Of course, I use different versions of database.yml when I use postgreSql or SqlLite3.
But I definitely don't use any database.
Why is it happening? What should I do to solve it? And how to disable using databases?
See the SO question here for how to bypass using a database. It's a little more work than just setting a flag.

Testing different database backends on a rails gem

I need orientation implementing the tests of a gem I'd like to build. It's a small gem that adds a new class method to ActiveRecord::Base. This new method will execute some SQL on demmand.
Trouble is, the SQL to be executed depends on the database backend. It's different for SQLite, Postgres and MySQL. The ruby method signature is the same for all database backends - only the SQL changes.
So, how do I make the tests for this?
To clarify: I know how to ask ActiveRecord "what database backend are you on?". What I need is a way to tell RSpec/Minitest/TestUnit "change the database backend to Postgres, and try all tests again. And then MySQL".
Take a look at how DataMapper does this. They pass an environment variable ADAPTER when running the specs, which alters the gems that are installed/loaded by bundler and changes the connection setup in the spec helper. You should be able to do something similar, basically running the full suite once for each possible database backend.
It looks like this when you run the DM core specs:
ADAPTER=mysql bundle install
ADAPTER=mysql bundle exec spec spec
ADAPTER=sqlite bundle install
ADAPTER=sqlite bundle exec rspec spec
I reckon if you want to get the best coverage, this is a good approach. If you want to selectively test different things for different parts of the tests, you'd just have to use conditionals, though (in general) I see conditionals in tests as a bad thing.

Resources