1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Improving the structure and syntax of the RSpec helper, modifying it to work with rspec in addition to rspec-rails

This commit is contained in:
Ben Atkins 2013-08-27 15:12:33 -04:00
parent 4eaaac5dd8
commit e3a039b3db
12 changed files with 165 additions and 102 deletions

View file

@ -839,20 +839,55 @@ sql> delete from versions where created_at < 2010-06-01;
## Testing
PaperTrail uses Bundler to manage its dependencies (in development and testing). You can run the tests with `bundle exec rake test`. (You may need to `bundle install` first.)
You may want to turn PaperTrail off to speed up your tests. See the [Turning PaperTrail Off/On](#turning-papertrail-offon) section above.
It's a good idea to reset PaperTrail before each test so data from one test doesn't spill over another. For example:
### RSpec
PaperTrail provides a helper that works with RSpec to make it easier to control when `PaperTrail` is turned on. By default, it will be
turned off for all tests. When you wish to enable PaperTrail for a test you can either wrap the test in a `with_versioning` block, or pass
in `:versioning => true` option to a test block, like so:
```ruby
RSpec.configure do |config|
config.before :each do
PaperTrail.controller_info = {}
PaperTrail.whodunnit = nil
describe "RSpec test group" do
it 'by default, PaperTrail will be turned off' do
PaperTrail.should_not be_enabled
end
with_versioning do
it 'within a `with_versioning` block it will be turned on' do
PaperTrail.should be_enabled
end
end
it 'can be turned on at the `it` or `describe` level like this', :versioning => true do
PaperTrail.should be_enabled
end
end
```
You may want to turn PaperTrail off to speed up your tests. See the [Turning PaperTrail Off/On](#turning-papertrail-offon) section above.
The helper will also reset the `PaperTrail.whodunnit` value to `nil` before each test to help prevent data spillover between tests.
If you are using PaperTrail with Rails, the helper will automatically set the `PaperTrail.controller_info` value to `{}` as well, again, to help prevent data spillover between tests.
There is also a `be_versioned` matcher provided by PaperTrail's RSpec helper which can be leveraged like so:
```ruby
class Widget < ActiveRecord::Base
end
describe Widget do
it { should_not be_versioned }
describe "add versioning to the `Widget` class" do
before(:all) do
class Widget < ActiveRecord::Base
has_paper_trail
end
end
it { should be_versioned }
end
end
```
## Articles
@ -912,6 +947,7 @@ Many thanks to:
* [Tyler Rick](https://github.com/TylerRick)
* [Bradley Priest](https://github.com/bradleypriest)
* [David Butler](https://github.com/dwbutler)
* [Paul Belt](https://github.com/belt)
## Inspirations

View file

@ -2,8 +2,7 @@ require 'bundler'
Bundler::GemHelper.install_tasks
require 'rake/testtask'
desc 'Test the paper_trail plugin.'
desc 'Run tests on PaperTrail with Test::Unit.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
@ -11,5 +10,15 @@ Rake::TestTask.new(:test) do |t|
t.verbose = false
end
require 'rspec/core/rake_task'
desc 'Run PaperTrail specs for the RSpec helper.'
RSpec::Core::RakeTask.new(:spec)
desc 'Run all available test suites'
task :run_all_tests do
system('rake test')
system('rake spec')
end
desc 'Default: run unit tests.'
task :default => :test
task :default => :run_all_tests

View file

@ -6,8 +6,6 @@ require 'paper_trail/cleaner'
Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'frameworks', '*.rb')].each { |file| require file }
Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'serializers', '*.rb')].each { |file| require file }
require 'paper_trail/rspec' if defined?(RSpec)
# PaperTrail's module methods can be called in both models and controllers.
module PaperTrail
extend PaperTrail::Cleaner

View file

@ -0,0 +1,37 @@
if defined?(RSpec)
require File.expand_path('../rspec/extensions', __FILE__)
RSpec.configure do |config|
config.include ::PaperTrail::RSpec::Extensions
config.before(:each) do
::PaperTrail.enabled = false
::PaperTrail.whodunnit = nil
end
config.before(:each, versioning: true) do
::PaperTrail.enabled = true
end
end
RSpec::Matchers.define :be_versioned do
# check to see if the model has `has_paper_trail` declared on it
match { |actual| actual.kind_of?(::PaperTrail::Model::InstanceMethods) }
end
# The `Rails` helper also sets the `controller_info` config variable in a `before_filter`...
if defined?(::Rails) && defined?(RSpec::Rails)
module RSpec
module Rails
class Railtie < ::Rails::Railtie
initializer 'paper_trail.rspec_extensions' do
RSpec.configure do |config|
config.before(:each) { ::PaperTrail.controller_info = {} }
end
end
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module PaperTrail
module RSpec
module Extensions
# :call-seq:
# with_versioning
#
# enable versioning for specific blocks
def with_versioning
was_enabled = ::PaperTrail.enabled?
::PaperTrail.enabled = true
begin
yield
ensure
::PaperTrail.enabled = was_enabled
end
end
end
end
end

View file

@ -1,29 +0,0 @@
require 'paper_trail/rspec/paper_trail_extensions'
module RSpec
module Rails
class Railtie < ::Rails::Railtie
initializer 'paper_trail.rspec_extensions' do
RSpec.configure do |config|
config.include RSpec::PaperTrailExtensions
config.before(:each) do
::PaperTrail.enabled = false
::PaperTrail.controller_info = {}
::PaperTrail.whodunnit = nil
end
config.before(:each, versioning: true) do
::PaperTrail.enabled = true
end
end
RSpec::Matchers.define :be_versioned do
match do |actual|
actual.respond_to?(:versions)
end
end
end
end
end
end

View file

@ -1,21 +0,0 @@
require 'rspec/core'
require 'rspec/expectations'
module RSpec
module PaperTrailExtensions
# :call-seq:
# with_versioning
#
# enable versioning for specific blocks
def with_versioning
was_enabled = ::PaperTrail.enabled?
::PaperTrail.enabled = true
begin
yield
ensure
::PaperTrail.enabled = was_enabled
end
end
end
end

View file

@ -1,3 +1,14 @@
# == Schema Information
#
# Table name: widgets
#
# id :integer not null, primary key
# name :string(255)
# an_integer :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Widget < ActiveRecord::Base
has_paper_trail
end

View file

@ -1,18 +1,19 @@
require 'spec_helper'
describe Version do
describe PaperTrail::Version do
context 'default' do
it 'should have versioning off by default' do
::PaperTrail.enabled?.should_not be_true
end
it 'should turn versioning on in a with_versioning block' do
::PaperTrail.enabled?.should_not be_true
::PaperTrail.enabled?.should be_false
with_versioning do
::PaperTrail.enabled?.should be_true
end
::PaperTrail.enabled?.should_not be_true
::PaperTrail.enabled?.should be_false
end
end
context 'versioning: true', versioning: true do
it 'should have versioning on by default' do
::PaperTrail.enabled?.should be_true
@ -27,11 +28,14 @@ describe Version do
end
end
describe Model do
describe Widget do
it { should be_versioned }
context 'be_versioned matcher', versioning: true do
it 'should respond to be_versioned' do
model = Model.create name: 'Bob', color: 'blue'
model.should be_versioned
widget = Widget.create name: 'Bob', an_integer: 1
widget.should be_versioned
widget.versions.size.should == 1
end
end
end

View file

@ -1,5 +0,0 @@
+--colour
+--format progress
+--loadby mtime
+--reverse

View file

@ -1,38 +1,41 @@
require 'rubygems'
require 'bundler/setup'
ENV['RAILS_ENV'] ||= 'test'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path('../dummy/config/environment', __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'rails'
require 'active_record'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
end
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define(:version => 1) do
create_table :models do |t|
t.string :name
t.string :color
end
create_table :versions do |t|
t.string :item_type, null: false
t.string :item_id, null: false
t.string :event, null: false
t.string :whodunnit
t.text :object
t.datetime :created_at
end
end
class Model < ActiveRecord::Base
has_paper_trail
attr_accessible :name, :color
end

View file