I try to change my database from sqlite to mysql.
However, after I modify my database.yml from sqlite to mysql, my test fail which the test, “/spec/requests/apis/v1/cats_controller_spec.rb”, was fine, when the database was sqlite.
So I setup test environment to use sqlite, and production, and development environment to use mysql, and my test works again.
The error message is following, when I setup the database to mysql.
"ActiveRecord::RecordNotFound"
Can I use mysql database with Factory Girl at the test environment, how to do that?
My database.yml is following, when I setup mysql at test environment.
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: mysql2
encoding: utf8
database: development_test_api
pool: 5
username: root
password: "123456"
socket: /tmp/mysql.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
database: test_test_api
pool: 5
username: root
password: "123456"
socket: /tmp/mysql.sock
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
My test, “/spec/requests/apis/v1/cats_controller_spec.rb” is following.
require 'spec_helper'
describe 'Cat management' do
it 'returns a list of cats' do
# create a cat in the database with name Persian
FactoryGirl.create(:cat, name: 'Persian')
FactoryGirl.create(:cat, name: 'Garfield')
# make a get request to the cats index (note the format json)
get '/apis/v1/cats', format: 'json'
# parse the json into ruby
result = JSON.load(response.body)
# asert that the cat returned from the API has the name Persian
# note it's an array of hashes so i'm using ".first" on the array
expect(result.first['name']).to eq 'Persian'
expect(result.last['name']).to eq 'Garfield'
end
it 'show a cat' do
FactoryGirl.create(:cat, name: 'Persian')
get '/apis/v1/cats/1', format: 'json'
result = JSON.load(response.body)
expect(result['name']).to eq 'Persian'
end
it 'creates a cat' do
post '/apis/v1/cats', :cat => {:name => 'Persian'}, format: 'json'
result = JSON.load(response.body)
expect(result['name']).to eq 'Persian'
expect(response.status).to eq 201
end
it 'update the name of a cat' do
FactoryGirl.create(:cat, name: 'Persian')
put '/apis/v1/cats/1', :cat => {:name => 'Garfield'}, format: 'json'
result = JSON.load(response.body)
expect(result['name']).to eq 'Garfield'
expect(response.status).to eq 200
end
it 'destroy a cat record' do
FactoryGirl.create(:cat, name: 'Persian')
delete '/apis/v1/cats/1', format: 'json'
result = JSON.load(response.body)
expect(result['name']).to eq 'Persian'
expect(response.status).to eq 200
end
end
The error messages, when I setup mysql as my test environment database.
Cat management
returns a list of cats
show a cat (FAILED - 1)
update the name of a cat (FAILED - 2)
destroy a cat record (FAILED - 3)
creates a cat
Failures:
1) Cat management show a cat
Failure/Error: get '/apis/v1/cats/1', format: 'json'
ActiveRecord::RecordNotFound:
Couldn't find Cat with id=1
# ./app/controllers/apis/v1/cats_controller.rb:11:in `show'
# ./spec/requests/apis/v1/cats_controller_spec.rb:24:in `block (2 levels) in <top (required)>'
2) Cat management update the name of a cat
Failure/Error: put '/apis/v1/cats/1', :cat => {:name => 'Garfield'}, format: 'json'
ActiveRecord::RecordNotFound:
Couldn't find Cat with id=1
# ./app/controllers/apis/v1/cats_controller.rb:34:in `update'
# ./spec/requests/apis/v1/cats_controller_spec.rb:43:in `block (2 levels) in <top (required)>'
3) Cat management destroy a cat record
Failure/Error: delete '/apis/v1/cats/1', format: 'json'
ActiveRecord::RecordNotFound:
Couldn't find Cat with id=1
# ./app/controllers/apis/v1/cats_controller.rb:46:in `destroy'
# ./spec/requests/apis/v1/cats_controller_spec.rb:54:in `block (2 levels) in <top (required)>'
Finished in 0.10862 seconds
5 examples, 3 failures
Failed examples:
rspec ./spec/requests/apis/v1/cats_controller_spec.rb:21 # Cat management show a cat
rspec ./spec/requests/apis/v1/cats_controller_spec.rb:40 # Cat management update the name of a cat
rspec ./spec/requests/apis/v1/cats_controller_spec.rb:51 # Cat management destroy a cat record
Randomized with seed 35843
My source codes is following.
Many thanks for reply.