Skip to content

Instantly share code, notes, and snippets.

@t-oginogin
Created February 27, 2014 09:14
Show Gist options
  • Save t-oginogin/9246823 to your computer and use it in GitHub Desktop.
Save t-oginogin/9246823 to your computer and use it in GitHub Desktop.

MiniTest Specを使う

Gemfileに次の行を追加

group :test do
  gem 'factory_girl_rails'
  gem 'minitest-spec-rails'
  gem 'database_cleaner'
end

test/test_helper.rb

ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/spec'
require 'database_cleaner'

class ActiveSupport::TestCase
  ActiveRecord::Migration.check_pending!
end

DatabaseCleaner.strategy = :truncation
class MiniTest::Spec
  before :each do
    DatabaseCleaner.start
  end

  after :each do
    DatabaseCleaner.clean
  end
end

test/factories/users.rb

FactoryGirl.define do
  factory :user1, class: User do
    name 'Test User'
  end
end

test/controllers/users_controller_test.rb

require 'test_helper'

describe UsersController do
  describe 'create new user' do
    before do
      user = FactoryGirl.build(:user1)
      post :create, user: { name: user.name }
    end

    it 'count is 1' do
      User.count.must_equal 1
    end

    it 'name is set' do
      user = User.first
      user.name.must_equal 'Test User'
    end
  end
end

#Rails+SQLite3でのrake test時のエラー対応

Railsでテストを実行すると次のエラーが発生しました。

ActiveRecord::StatementInvalid: SQLite3::SQLException: near "SAVEPOINT": syntax error: SAVEPOINT active_record_1

環境は次の通り。

OS:CentOS5.5

Rails:4.0.0

sqlite3:3.3.6

調べてみると、sqlite3でsavepointが対応されたのは3.6.8以降らしいので、 sqlite3を更新します。

http://stackoverflow.com/questions/7367274/rails-3-1-app-cant-install-sqlite3-gem-because-libraries-are-out-of-date

$ cd /usr/local/src/
$ sudo wget http://www.sqlite.org/2014/sqlite-autoconf-3080301.tar.gz
$ sudo tar xvzf sqlite-autoconf-3080301.tar.gz
$ cd sqlite-autoconf-3080301
$ sudo ./configure
$ sudo make
$ sudo make install

gemのsqlite3の更新が必要なので、次のコマンドを実行。

$ bundle config build.sqlite3 --with-sqlite3-include=/usr/local/include --with-sqlite3-lib=/usr/local/lib --with-sqlite3-dir=/usr/local/bin
$ bundle install

再度テストを実行するとエラーはなくなりました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment