bluff 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +3 -0
- data/README.md +134 -0
- data/bluff.gemspec +13 -13
- data/lib/bluff.rb +10 -1
- data/lib/bluff/bluffs/data_bluffs.rb +16 -0
- data/lib/bluff/builder.rb +74 -0
- data/lib/bluff/configuration.rb +20 -0
- data/lib/bluff/version.rb +1 -1
- data/spec/models/api_spec.rb +46 -0
- data/spec/models/data_bluffs_spec.rb +4 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/matchers/bluff.rb +9 -0
- data/spec/support/matchers/bluff_instance_of.rb +5 -0
- metadata +72 -46
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
= bluff
|
2
|
+
|
3
|
+
* https://github.com/islandr/bluff
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
A single source of lies for all your testing needs.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* TODO
|
12
|
+
|
13
|
+
== CONTRIBUTING:
|
14
|
+
|
15
|
+
Pull requests are welcome! See the DEVELOPERS section at the bottom of this page for more information.
|
16
|
+
|
17
|
+
== SYNOPSIS:
|
18
|
+
|
19
|
+
My original intentions for bluff are focused on rails/activerecord but I'm open
|
20
|
+
to other libraries if it doesn't pollute the API.
|
21
|
+
|
22
|
+
The goal is to minimize the amount of mocking/faking pollution in your test suits,
|
23
|
+
and to do so via an API that feels natural to rails/activerecord users. I favor
|
24
|
+
a slightly more wordy API for defining bluffs that offers the full power and flexibility
|
25
|
+
of Ruby over a cute DSL that limits customization.
|
26
|
+
|
27
|
+
=== MODEL AND DATA BLUFFS
|
28
|
+
|
29
|
+
Bluff comes with a small set of predefined data bluffs (see lib/bluff/bluffs/data_bluffs.rb)
|
30
|
+
but its easy to add your own.
|
31
|
+
|
32
|
+
The standard practice is to define your bluffs within spec/bluffs. If you are
|
33
|
+
bluffing a model, name the file *_model_\_bluff.rb*. If you need to bluff custom
|
34
|
+
data, do so in *data_bluffs.rb*.
|
35
|
+
|
36
|
+
A bluff is a bluff, whether it's just a piece of data or a full blown model. All bluffs
|
37
|
+
share the same namespace (the root namespace is the *only* namespace) so choose your bluff
|
38
|
+
names accordingly.
|
39
|
+
|
40
|
+
```
|
41
|
+
# spec/bluffs/user_bluff.rb
|
42
|
+
Bluff.for(:user) do |attributes|
|
43
|
+
attributes[:name] ||= Bluff.name
|
44
|
+
attributes[:email] ||= Bluff.email(attributes[:name])
|
45
|
+
|
46
|
+
# return your bluffed instance
|
47
|
+
User.new(attributes)
|
48
|
+
end
|
49
|
+
|
50
|
+
# spec/bluffs/data_bluffs.rb
|
51
|
+
Bluff.for(:name) { Faker::Name.name } # Call via Bluff.name
|
52
|
+
Bluff.for(:company_name) { Faker::Company.name }
|
53
|
+
```
|
54
|
+
|
55
|
+
==== CALLING YOUR BLUFFS
|
56
|
+
```
|
57
|
+
User.bluff # returns an unsaved instance -- could also use Bluff.user
|
58
|
+
User.bluff! # returns a saved instance -- could also use Bluff.user!
|
59
|
+
User.bluff({:name => 'Ryan'}) # supply overrides in the attributes hash
|
60
|
+
```
|
61
|
+
|
62
|
+
=== METHOD BLUFFS (MOCKS / STUBS)
|
63
|
+
|
64
|
+
All objects and classes are given a `bluff()` method, which is
|
65
|
+
used for bluffing both models and methods. If given a symbol, the
|
66
|
+
`bluff()` method can be used to define method stubs as follows:
|
67
|
+
|
68
|
+
```
|
69
|
+
User.bluff(:find) { |id| :value }
|
70
|
+
User.bluff(:save) { true }
|
71
|
+
|
72
|
+
user = Bluff.mock
|
73
|
+
user.bluff(:name).as(:value)
|
74
|
+
```
|
75
|
+
|
76
|
+
The mocking abilities of bluff are no more than a thin wrapper around
|
77
|
+
rspec-mocks. Calling `Bluff.mock('account')` is identical to calling `double('account')`
|
78
|
+
|
79
|
+
== REQUIREMENTS:
|
80
|
+
|
81
|
+
* rspec-mocks
|
82
|
+
* faker
|
83
|
+
|
84
|
+
== INSTALL:
|
85
|
+
|
86
|
+
* `gem install bluff`
|
87
|
+
* or simply add bluff to your Gemfile and run `bundle install`
|
88
|
+
|
89
|
+
```
|
90
|
+
group :test do
|
91
|
+
gem 'bluff'
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
== FURTHER READING:
|
96
|
+
|
97
|
+
http://faker.rubyforge.org/
|
98
|
+
|
99
|
+
https://www.relishapp.com/rspec/rspec-mocks/
|
100
|
+
https://www.relishapp.com/rspec/rspec-mocks/docs/method-stubs/stub-on-any-instance-of-a-class
|
101
|
+
https://www.relishapp.com/rspec/rspec-mocks/docs/outside-rspec/configure-any-test-framework-to-use-rspec-mocks
|
102
|
+
|
103
|
+
== DEVELOPERS:
|
104
|
+
|
105
|
+
After checking out the source, run:
|
106
|
+
|
107
|
+
$ rake setup
|
108
|
+
|
109
|
+
This task will install any missing dependencies and run the tests/specs.
|
110
|
+
|
111
|
+
== LICENSE:
|
112
|
+
|
113
|
+
(The MIT License)
|
114
|
+
|
115
|
+
Copyright (c) 2011 Ryan Mohr (github.com/islandr)
|
116
|
+
|
117
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
118
|
+
a copy of this software and associated documentation files (the
|
119
|
+
'Software'), to deal in the Software without restriction, including
|
120
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
121
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
122
|
+
permit persons to whom the Software is furnished to do so, subject to
|
123
|
+
the following conditions:
|
124
|
+
|
125
|
+
The above copyright notice and this permission notice shall be
|
126
|
+
included in all copies or substantial portions of the Software.
|
127
|
+
|
128
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
129
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
130
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
131
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
132
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
133
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
134
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/bluff.gemspec
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path(
|
3
|
-
require
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'bluff/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
6
|
+
s.name = 'bluff'
|
7
7
|
s.version = Bluff::VERSION
|
8
|
-
s.authors = [
|
9
|
-
s.email = [
|
10
|
-
s.homepage =
|
8
|
+
s.authors = ['Ryan Mohr']
|
9
|
+
s.email = ['ryan.mohr@gmail.com']
|
10
|
+
s.homepage = 'https://github.com/islandr/bluff'
|
11
11
|
s.summary = %q{A single source of lies for all your testing needs}
|
12
12
|
s.description = %q{}
|
13
13
|
|
14
|
-
s.rubyforge_project =
|
14
|
+
s.rubyforge_project = 'bluff'
|
15
15
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
17
|
-
s.test_files = `git ls-files --
|
18
|
-
s.require_paths = [
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.add_development_dependency 'activesupport', '~> 3.0'
|
21
|
+
s.add_development_dependency 'rspec', '~> 2.8'
|
22
|
+
s.add_development_dependency 'faker', '~> 1.0'
|
23
23
|
end
|
data/lib/bluff.rb
CHANGED
@@ -1,4 +1,13 @@
|
|
1
|
-
require
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
require 'bluff/version'
|
5
|
+
require 'bluff/configuration'
|
6
|
+
require 'bluff/builder'
|
2
7
|
|
3
8
|
module Bluff
|
9
|
+
extend Bluff::Configuration::ClassMethods
|
10
|
+
extend Bluff::Builder::ClassMethods
|
4
11
|
end
|
12
|
+
|
13
|
+
require 'bluff/bluffs/data_bluffs'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'faker'
|
2
|
+
|
3
|
+
# people
|
4
|
+
Bluff.for(:name) { Faker::Name.name }
|
5
|
+
Bluff.for(:username) {|name = nil| (name ||= Bluff.name).parameterize }
|
6
|
+
Bluff.for(:email) {|name = nil| Faker::Internet.email(name) }
|
7
|
+
|
8
|
+
# companies
|
9
|
+
Bluff.for(:company_name) { Faker::Company.name }
|
10
|
+
|
11
|
+
# internet
|
12
|
+
Bluff.for(:domain) { Faker::Internet.domain_name }
|
13
|
+
|
14
|
+
# words
|
15
|
+
Bluff.for(:words) {|count = 3| Faker::Lorem.words(count) }
|
16
|
+
Bluff.for(:phrase) { Faker::Company.bs }
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Bluff
|
2
|
+
module Builder
|
3
|
+
module ClassMethods
|
4
|
+
# def insist(field)
|
5
|
+
# raise ArgumentError, "#{field} cannot be bluffed for #{target}"
|
6
|
+
# end
|
7
|
+
|
8
|
+
# options: class_name
|
9
|
+
def for(field, options = {}, &block)
|
10
|
+
options = {:bang => true}.merge(options)
|
11
|
+
|
12
|
+
extend_bluff(field, options, &block)
|
13
|
+
extend_target(field, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def extend_bluff(field, options, &block)
|
18
|
+
define_bluff(field, &block)
|
19
|
+
define_bluff_bang(field) if options[:bang]
|
20
|
+
end
|
21
|
+
|
22
|
+
def define_bluff(field, &block)
|
23
|
+
define_singleton_method(field) do |*args|
|
24
|
+
bluffed_object = nil
|
25
|
+
|
26
|
+
config.max_attempts.times do
|
27
|
+
bluffed_object = block.call(*args)
|
28
|
+
break if !bluffed_object.respond_to?(:valid?) || bluffed_object.valid?
|
29
|
+
puts "!!!! FAILED BLUFF -- REATTEMPTING"
|
30
|
+
end
|
31
|
+
|
32
|
+
bluffed_object
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def define_bluff_bang(field)
|
37
|
+
define_singleton_method "#{field}!" do |*args|
|
38
|
+
send(field, *args).tap do |record|
|
39
|
+
ActiveRecord::Base.transaction do
|
40
|
+
record.class.reflect_on_all_associations(:belongs_to).each do |reflection|
|
41
|
+
association = record.send(reflection.name)
|
42
|
+
association.save! if association && association.new_record?
|
43
|
+
end
|
44
|
+
|
45
|
+
record.save!
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def extend_target(field, options)
|
52
|
+
class_name = options[:class_name] || field.to_s.classify
|
53
|
+
|
54
|
+
if Kernel.const_defined?(class_name)
|
55
|
+
klass = Kernel.const_get(class_name)
|
56
|
+
|
57
|
+
# just forward the calls back to Bluff
|
58
|
+
klass.define_singleton_method :bluff do |*args|
|
59
|
+
Bluff.send(field, *args)
|
60
|
+
end
|
61
|
+
|
62
|
+
if options[:bang]
|
63
|
+
klass.define_singleton_method :bluff! do |*args|
|
64
|
+
Bluff.send("#{field}!", *args)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
else
|
68
|
+
# not a big deal. bluff might be data instead of a model.
|
69
|
+
# puts "Bluff class not found: #{class_name}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Bluff
|
2
|
+
module Configuration
|
3
|
+
class Base
|
4
|
+
attr_accessor :max_attempts
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
# since we're faking the data but using real objects, there may be times where
|
8
|
+
# an object fails to validate due to duplicates etc. Bluff will retry up to
|
9
|
+
# max_attempts times before calling it quits
|
10
|
+
@max_attempts = 10
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def config
|
16
|
+
@config ||= Configuration::Base.new
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/bluff/version.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Bluff do
|
4
|
+
# define the basic data API
|
5
|
+
describe '.for' do
|
6
|
+
context 'at the basic level' do
|
7
|
+
it 'accepts a target and a block and returns the value of the block' do
|
8
|
+
Bluff.for(:foo) { 'bar' }
|
9
|
+
Bluff.foo.should eq('bar')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'data' do
|
15
|
+
context 'when requesting a defined data bluff' do
|
16
|
+
bluffs = [
|
17
|
+
#
|
18
|
+
# alright to add to these but DO NOT REMOVE them without
|
19
|
+
# good reason and without bumping the major version
|
20
|
+
#
|
21
|
+
|
22
|
+
# people
|
23
|
+
:name,
|
24
|
+
:username,
|
25
|
+
:email,
|
26
|
+
|
27
|
+
# companies
|
28
|
+
:company_name,
|
29
|
+
|
30
|
+
# words
|
31
|
+
:words,
|
32
|
+
:phrase
|
33
|
+
]
|
34
|
+
|
35
|
+
bluffs.each do |target|
|
36
|
+
specify { should bluff_for(target) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when requesting an undefined data bluff' do
|
41
|
+
it 'should raise an exception' do
|
42
|
+
lambda{ Bluff.missing }.should raise_error
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
SPEC_ROOT = File.expand_path(File.dirname(__FILE__)) # ENV["RAILS_ENV"] ||= 'test'
|
2
|
+
#
|
3
|
+
# require_relative '../../config/environment'
|
4
|
+
# require 'rspec/rails'
|
5
|
+
|
6
|
+
require_relative '../lib/bluff.rb'
|
7
|
+
|
8
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
9
|
+
# in spec/support/ and its subdirectories.
|
10
|
+
%w(support bluffs).each do |dir|
|
11
|
+
Dir[File.join(SPEC_ROOT, dir, '**', '*.rb')].each {|f| require f}
|
12
|
+
end
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.mock_with :rspec
|
16
|
+
end
|
metadata
CHANGED
@@ -1,73 +1,99 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: bluff
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Ryan Mohr
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
12
|
+
date: 2012-01-12 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &2160674180 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2160674180
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &2160673400 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.8'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2160673400
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: faker
|
38
|
+
requirement: &2160672460 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '1.0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2160672460
|
47
|
+
description: ''
|
48
|
+
email:
|
24
49
|
- ryan.mohr@gmail.com
|
25
50
|
executables: []
|
26
|
-
|
27
51
|
extensions: []
|
28
|
-
|
29
52
|
extra_rdoc_files: []
|
30
|
-
|
31
|
-
files:
|
53
|
+
files:
|
32
54
|
- .gitignore
|
55
|
+
- CHANGELOG.md
|
33
56
|
- Gemfile
|
57
|
+
- README.md
|
34
58
|
- Rakefile
|
35
59
|
- bluff.gemspec
|
36
60
|
- lib/bluff.rb
|
61
|
+
- lib/bluff/bluffs/data_bluffs.rb
|
62
|
+
- lib/bluff/builder.rb
|
63
|
+
- lib/bluff/configuration.rb
|
37
64
|
- lib/bluff/version.rb
|
38
|
-
|
39
|
-
|
65
|
+
- spec/models/api_spec.rb
|
66
|
+
- spec/models/data_bluffs_spec.rb
|
67
|
+
- spec/spec_helper.rb
|
68
|
+
- spec/support/matchers/bluff.rb
|
69
|
+
- spec/support/matchers/bluff_instance_of.rb
|
70
|
+
homepage: https://github.com/islandr/bluff
|
40
71
|
licenses: []
|
41
|
-
|
42
72
|
post_install_message:
|
43
73
|
rdoc_options: []
|
44
|
-
|
45
|
-
require_paths:
|
74
|
+
require_paths:
|
46
75
|
- lib
|
47
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
77
|
none: false
|
49
|
-
requirements:
|
50
|
-
- -
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
|
53
|
-
|
54
|
-
- 0
|
55
|
-
version: "0"
|
56
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
83
|
none: false
|
58
|
-
requirements:
|
59
|
-
- -
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
|
62
|
-
segments:
|
63
|
-
- 0
|
64
|
-
version: "0"
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
65
88
|
requirements: []
|
66
|
-
|
67
89
|
rubyforge_project: bluff
|
68
|
-
rubygems_version: 1.
|
90
|
+
rubygems_version: 1.8.10
|
69
91
|
signing_key:
|
70
92
|
specification_version: 3
|
71
93
|
summary: A single source of lies for all your testing needs
|
72
|
-
test_files:
|
73
|
-
|
94
|
+
test_files:
|
95
|
+
- spec/models/api_spec.rb
|
96
|
+
- spec/models/data_bluffs_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/support/matchers/bluff.rb
|
99
|
+
- spec/support/matchers/bluff_instance_of.rb
|