➜ active_record_test be rspec rollback_spec.rb
catched
.
Finished in 0.03118 seconds (files took 0.55192 seconds to load)
1 example, 0 failures
Last active
August 26, 2015 20:53
-
-
Save chischaschos/bd864da9050b362d758d to your computer and use it in GitHub Desktop.
Pure ActiveRecord + RSpec + SQLite example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'sqlite3' | |
gem 'rspec' | |
gem 'rspec-collection_matchers' | |
gem 'activerecord' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
require 'rspec/collection_matchers' | |
ActiveRecord::Base.establish_connection( | |
'adapter' => 'sqlite3', | |
'database' => 'test.sqlite3' | |
) | |
class User < ActiveRecord::Base | |
end | |
describe 'Rollback' do | |
before :all do | |
ActiveRecord::Base.connection.create_table :users do |t| | |
t.string :name | |
end | |
end | |
after :all do | |
ActiveRecord::Base.connection.drop_table :users do | |
end | |
end | |
it 'rollbacks' do | |
user = User.new | |
user.name = 'maik' | |
user.save! | |
expect(User.all).to have(1).item | |
User.transaction do | |
user = User.new | |
user.name = 'maik' | |
user.save! | |
expect(User.all).to have(2).item | |
raise ActiveRecord::Rollback | |
end | |
expect(User.all).to have(1).item | |
User.transaction do | |
begin | |
user = User.new | |
user.name = 'maik' | |
user.save! | |
expect(User.all).to have(2).item | |
raise ActiveRecord::Rollback | |
rescue => e | |
puts "catched" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment