rspec-set 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +69 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/lib/rspec-set.rb +42 -0
- data/rspec-set.gemspec +63 -0
- metadata +152 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.1"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.5.1)
|
6
|
+
bundler (~> 1.0.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.8.7)
|
10
|
+
rcov (0.9.9)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.5.1)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Philippe Creux
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
= rspec-set
|
2
|
+
|
3
|
+
<tt>#set</tt> is a little helper for RSpec to speed-up your specs.
|
4
|
+
|
5
|
+
<tt>#set</tt> can be used as a replacement of <tt>#let</tt>: <tt>#set</tt> will create the resource <tt>before(:all)</tt> your examples and will reload the resource <tt>before(:each)</tt> example.
|
6
|
+
|
7
|
+
<tt>#let</tt> on the other hand creates the resource for every single example.
|
8
|
+
|
9
|
+
You can drastically improve the time spent to run your specs. On an application with 3000 examples we decreased the specs duration by 70%!
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
The following code will create a flight before running the examples and reload the flight from the DB before each example.
|
14
|
+
|
15
|
+
require 'spec_helper'
|
16
|
+
|
17
|
+
describe Flight do
|
18
|
+
set(:flight) do
|
19
|
+
Flight.create!
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be cancellable" do
|
23
|
+
flight.cancel
|
24
|
+
flight.should be_cancelled
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be delayable" do
|
28
|
+
flight.delay
|
29
|
+
flight.should be_delayed
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
=== How does that work?
|
34
|
+
|
35
|
+
RSpec wraps each example in an SQL transaction which gets rolled back at the end of each example.
|
36
|
+
|
37
|
+
<tt>#set</tt> creates a flight once before running any example. Each example uses this flight and changes its state. Since RSpec rolls back the SQL transaction, the flight gets back to its initial state before each example. <tt>#set</tt> takes care of reloading the flight from the DB before each example. Examples won't affect each others then.
|
38
|
+
|
39
|
+
== Notes
|
40
|
+
|
41
|
+
* <tt>#set</tt> works only with ActiveRecord objects saved to the DB so far.
|
42
|
+
* The record created by <tt>#set</tt> won't be deleted from the database. I encourage you to use DatabaseCleaner with the <tt>:truncation</tt> strategy to clean up your database. So far in RSpec 2.0, <tt>before(:all)</tt> runs before all describe/context blocks while <tt>after(:all)</tt> runs after every single describe/context/it blocks. Just make sure that you call <tt>DatabaseCleaner.clean</tt> in a <tt>before(:all)</tt> or <tt>after(:suite)</tt> then. :)
|
43
|
+
* <tt>#set</tt> does not handle multi-level transactions.
|
44
|
+
* You will have to call <tt>DatabaseCleaner.clean</tt> <tt>before(:all)</tt> specs which rely on having an empty database. <tt>#set</tt> don't clean the database for you.
|
45
|
+
|
46
|
+
== TODO
|
47
|
+
|
48
|
+
* add cukes
|
49
|
+
* support non active record objects
|
50
|
+
* support non saved active record objects
|
51
|
+
* make <tt>before(:all) running in a transaction - See: http://rhnh.net/2010/10/06/transactional-before-all-with-rspec-and-datamapper
|
52
|
+
* support multi-level transactions
|
53
|
+
|
54
|
+
|
55
|
+
== Contributing to rspec-set
|
56
|
+
|
57
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
58
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
59
|
+
* Fork the project
|
60
|
+
* Start a feature/bugfix branch
|
61
|
+
* Commit and push until you are happy with your contribution
|
62
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
63
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
64
|
+
|
65
|
+
== Copyright
|
66
|
+
|
67
|
+
Copyright (c) 2010 Philippe Creux. See LICENSE.txt for
|
68
|
+
further details.
|
69
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "rspec-set"
|
16
|
+
gem.homepage = "http://github.com/pcreux/rspec-set"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %{set() is a helper for RSpec which setup active record
|
19
|
+
objects before all tests and restore them to there original state
|
20
|
+
before each test}
|
21
|
+
gem.description = %{set() is a helper for RSpec which setup active record
|
22
|
+
objects before all tests and restore them to there original state
|
23
|
+
before each test}
|
24
|
+
gem.email = "pcreux@gmail.com"
|
25
|
+
gem.authors = ["Philippe Creux"]
|
26
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
27
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
28
|
+
gem.add_runtime_dependency 'rspec', '>= 2'
|
29
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
30
|
+
end
|
31
|
+
Jeweler::RubygemsDotOrgTasks.new
|
32
|
+
|
33
|
+
require 'rake/testtask'
|
34
|
+
Rake::TestTask.new(:test) do |test|
|
35
|
+
test.libs << 'lib' << 'test'
|
36
|
+
test.pattern = 'test/**/test_*.rb'
|
37
|
+
test.verbose = true
|
38
|
+
end
|
39
|
+
|
40
|
+
require 'rcov/rcovtask'
|
41
|
+
Rcov::RcovTask.new do |test|
|
42
|
+
test.libs << 'test'
|
43
|
+
test.pattern = 'test/**/test_*.rb'
|
44
|
+
test.verbose = true
|
45
|
+
end
|
46
|
+
|
47
|
+
task :default => :test
|
48
|
+
|
49
|
+
require 'rake/rdoctask'
|
50
|
+
Rake::RDocTask.new do |rdoc|
|
51
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "rspec-set #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/rspec-set.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Core
|
3
|
+
module Set
|
4
|
+
module ClassMethods
|
5
|
+
# Set @variable_name in a before(:all) block and give access to it
|
6
|
+
# via let(:variable_name)
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# set(:transaction) { Factory(:address) }
|
11
|
+
#
|
12
|
+
# it "should be valid" do
|
13
|
+
# transaction.should be_valid
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
def set(variable_name, &block)
|
17
|
+
before(:all) do
|
18
|
+
self.class.send(:class_variable_set, "@@#{variable_name}".to_sym, instance_eval(&block))
|
19
|
+
end
|
20
|
+
|
21
|
+
let(variable_name) do
|
22
|
+
self.class.send(:class_variable_get, "@@#{variable_name}".to_sym).tap do |i|
|
23
|
+
if i.respond_to?(:new_record?) && i.respond_to?(:reload)
|
24
|
+
i.reload unless i.new_record?
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end # set()
|
29
|
+
|
30
|
+
end # ClassMethods
|
31
|
+
|
32
|
+
def self.included(mod) # :nodoc:
|
33
|
+
mod.extend ClassMethods
|
34
|
+
end
|
35
|
+
end # Set
|
36
|
+
|
37
|
+
class ExampleGroup
|
38
|
+
include Set
|
39
|
+
end # ExampleGroup
|
40
|
+
|
41
|
+
end # Core
|
42
|
+
end # RSpec
|
data/rspec-set.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rspec-set}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Philippe Creux"]
|
12
|
+
s.date = %q{2011-02-04}
|
13
|
+
s.description = %q{set() is a helper for RSpec which setup active record
|
14
|
+
objects before all tests and restore them to there original state
|
15
|
+
before each test}
|
16
|
+
s.email = %q{pcreux@gmail.com}
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.rdoc",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/rspec-set.rb",
|
30
|
+
"rspec-set.gemspec"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/pcreux/rspec-set}
|
33
|
+
s.licenses = ["MIT"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.7}
|
36
|
+
s.summary = %q{set() is a helper for RSpec which setup active record objects before all tests and restore them to there original state before each test}
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
40
|
+
s.specification_version = 3
|
41
|
+
|
42
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
43
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
44
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
45
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
|
46
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
47
|
+
s.add_runtime_dependency(%q<rspec>, [">= 2"])
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
50
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
51
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
52
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
53
|
+
s.add_dependency(%q<rspec>, [">= 2"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
57
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
58
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
59
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
60
|
+
s.add_dependency(%q<rspec>, [">= 2"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-set
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Philippe Creux
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-04 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
type: :development
|
24
|
+
name: shoulda
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
requirement: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
prerelease: false
|
37
|
+
type: :development
|
38
|
+
name: bundler
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 23
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
- 0
|
49
|
+
version: 1.0.0
|
50
|
+
requirement: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
prerelease: false
|
53
|
+
type: :development
|
54
|
+
name: jeweler
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 1
|
61
|
+
segments:
|
62
|
+
- 1
|
63
|
+
- 5
|
64
|
+
- 1
|
65
|
+
version: 1.5.1
|
66
|
+
requirement: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
prerelease: false
|
69
|
+
type: :development
|
70
|
+
name: rcov
|
71
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirement: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
prerelease: false
|
83
|
+
type: :runtime
|
84
|
+
name: rspec
|
85
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
hash: 7
|
91
|
+
segments:
|
92
|
+
- 2
|
93
|
+
version: "2"
|
94
|
+
requirement: *id005
|
95
|
+
description: |-
|
96
|
+
set() is a helper for RSpec which setup active record
|
97
|
+
objects before all tests and restore them to there original state
|
98
|
+
before each test
|
99
|
+
email: pcreux@gmail.com
|
100
|
+
executables: []
|
101
|
+
|
102
|
+
extensions: []
|
103
|
+
|
104
|
+
extra_rdoc_files:
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.rdoc
|
107
|
+
files:
|
108
|
+
- .document
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.rdoc
|
113
|
+
- Rakefile
|
114
|
+
- VERSION
|
115
|
+
- lib/rspec-set.rb
|
116
|
+
- rspec-set.gemspec
|
117
|
+
has_rdoc: true
|
118
|
+
homepage: http://github.com/pcreux/rspec-set
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
hash: 3
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
version: "0"
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
none: false
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
hash: 3
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
version: "0"
|
144
|
+
requirements: []
|
145
|
+
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 1.3.7
|
148
|
+
signing_key:
|
149
|
+
specification_version: 3
|
150
|
+
summary: set() is a helper for RSpec which setup active record objects before all tests and restore them to there original state before each test
|
151
|
+
test_files: []
|
152
|
+
|