Skip to content

Instantly share code, notes, and snippets.

@gamov
Last active August 29, 2015 13:57
Show Gist options
  • Save gamov/9501394 to your computer and use it in GitHub Desktop.
Save gamov/9501394 to your computer and use it in GitHub Desktop.
Paper Trail single file Rails test
# Activate the gem you are reporting the issue against.
gem 'rails', '3.0.20'
gem 'paper_trail', '3.0.0'
require 'rails/all'
require 'action_controller/railtie'
# ENV["RAILS_ENV"] = "test"
require 'rails/test_help'
require 'test/unit'
require 'active_support/core_ext/kernel/requires'
require 'active_support/test_case'
require 'action_view'
require 'action_controller/test_case'
require 'action_dispatch/testing/integration'
require 'active_record/test_case'
require 'logger'
require 'paper_trail'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string :name
t.timestamps
end
create_table :version_posts do |t|
t.string :item_type, :null => false
t.integer :item_id, :null => false
t.string :event, :null => false
t.string :whodunnit
t.text :object
t.datetime :created_at
end
end
class TestApp < Rails::Application
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: 'cookie_store_key'
config.secret_token = 'secret_tokeneeeeeeeeeeeeeeeeeeeeeeeeeeeee'
config.secret_key_base = 'secret_key_base'
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
resources :posts do
member do
get :versions
end
end
end
end
PaperTrail.enabled = false
def with_versioning
was_enabled = PaperTrail.enabled?
PaperTrail.enabled = true
begin
yield
ensure
PaperTrail.enabled = was_enabled
end
end
class Post < ActiveRecord::Base
has_paper_trail :on => [:update, :destroy],
:class_name => 'PostVersion',
if: Proc.new {|t| t.changed? && t.created_at < (Time.now - 1.day)}
end
class PostVersion < PaperTrail::Version #paper_trail 3
self.table_name = :version_posts
end
class PostsController < ActionController::Base
include Rails.application.routes.url_helpers
def versions
@versions = Post.find(params[:id]).versions
render nothing: true
end
end
# unit test ok
class BugTest < ActiveSupport::TestCase
def test_versioning
post = Post.create! name: 'post name', created_at: 2.days.ago
with_versioning do
post.name = 'name changed'
post.save
assert_equal 1, post.versions.count
end
end
end
require 'minitest/autorun'
require 'rack/test'
# functional test
class PostsControllerTest < ActionController::TestCase
# tests PostsController
# include ActionDispatch::Routing
setup do
@post = Post.create! name: 'setup post', created_at: 2.days.ago
end
#if you change this test name to start with a 'w', all tests pass!!!!!!
test 'versions' do
get :versions, id: @post.to_param
assert_empty assigns(:versions)
end
test 'versions 2' do
with_versioning do
assert PaperTrail.enabled?
@post.name = 'name changed'
@post.save
# puts post.versions.inspect
assert_equal 1, @post.versions.count
get :versions, id: @post.to_param
assert_response :success
assert_equal 1, assigns(:versions).count
end
end
end
@gamov
Copy link
Author

gamov commented Mar 12, 2014

I refactored it, working now.

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