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

Keep getting:

NameError: undefined local variable or method `_routes' for PostsController:Class
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/action_controller/metal/url_for.rb:23:in `action_methods'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/abstract_controller/base.rb:143:in `action_method?'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/abstract_controller/base.rb:191:in `method_for_action'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/action_controller/metal/hide_actions.rb:19:in `method_for_action'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/action_controller/metal/implicit_render.rb:14:in `method_for_action'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/action_controller/metal/compatibility.rb:63:in `method_for_action'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/abstract_controller/base.rb:113:in `process'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/abstract_controller/rendering.rb:41:in `process'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/action_controller/metal/testing.rb:12:in `process_with_new_base_test'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/action_controller/test_case.rb:414:in `process'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/action_controller/test_case.rb:47:in `process'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/actionpack-3.0.20/lib/action_controller/test_case.rb:352:in `get'
    /Users/gamov/Temp/@NoTM/active_record_gem.rb:134:in `block (2 levels) in <class:PostsControllerTest>'
    /Users/gamov/Temp/@NoTM/active_record_gem.rb:72:in `with_versioning'
    /Users/gamov/Temp/@NoTM/active_record_gem.rb:126:in `block in <class:PostsControllerTest>'
    /Users/gamov/.rvm/gems/ruby-1.9.3-p484@ector/gems/mocha-0.13.3/lib/mocha/integration/mini_test/version_230_to_2101.rb:36:in `run'

@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