chai-backbone-rails 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -99,14 +99,32 @@ other objects as you see fit:
|
|
99
99
|
|
100
100
|
### Traits
|
101
101
|
|
102
|
-
you can also use 'traits'
|
102
|
+
you can also use 'traits'.
|
103
|
+
Traits are flags that are set when the user calls create with the
|
104
|
+
factory name prefixed with terms separated by dashes.
|
103
105
|
|
104
|
-
|
105
|
-
|
106
|
-
|
106
|
+
Like: 'female-admin-user'
|
107
|
+
|
108
|
+
This will call the 'user' factory, and provide the terms 'female' and
|
109
|
+
'admin' as traits for this user
|
110
|
+
|
111
|
+
this list is accessible in the factory callback using `this.traits`
|
112
|
+
|
113
|
+
There are 2 helper methods to help check if traits are set:
|
114
|
+
|
115
|
+
this.trait('returns', 'one', 'of', 'these', 'values')
|
116
|
+
|
117
|
+
and
|
118
|
+
|
119
|
+
this.hasTrait('admin') # returns a boolean value
|
120
|
+
|
121
|
+
Extended example:
|
122
|
+
|
123
|
+
Factory.define 'user', (attributes = {}) ->
|
124
|
+
attributes.gender = @trait('male', 'female') || 'male'
|
107
125
|
|
108
126
|
returningClass = User
|
109
|
-
if
|
127
|
+
if @hasTrait('admin')
|
110
128
|
returningClass = AdminUser
|
111
129
|
|
112
130
|
new returningClass attributes
|
@@ -114,6 +132,7 @@ you can also use 'traits':
|
|
114
132
|
Factory.create 'user', name: 'Matthijs' # => new User name: 'Matthijs'
|
115
133
|
Factory.create 'male-user', name: 'Matthijs' # => new User name: 'Matthijs', gender: 'male'
|
116
134
|
Factory.create 'male-admin-user', name: 'Matthijs' # => new AdminUser name: 'Matthijs', gender: 'male'
|
135
|
+
Factory.create 'female-user', name: 'Beppie' # => new User name: 'Beppie', gender: 'female'
|
117
136
|
|
118
137
|
### Sequences
|
119
138
|
|
@@ -136,6 +155,15 @@ You can also yield results:
|
|
136
155
|
Factory.create('abc') => 'a'
|
137
156
|
Factory.create('abc') => 'b'
|
138
157
|
|
158
|
+
### Sampling
|
159
|
+
|
160
|
+
You can sample a value from a list
|
161
|
+
|
162
|
+
Factory.define 'sampler', ->
|
163
|
+
@sample 'a', 'b', 'c'
|
164
|
+
|
165
|
+
Will randomly return a, b or c every time
|
166
|
+
|
139
167
|
Running the tests
|
140
168
|
=================
|
141
169
|
|
@@ -79,6 +79,8 @@
|
|
79
79
|
Backbone.history = new Backbone.History
|
80
80
|
|
81
81
|
spy = sinon.spy router, methodName # spy on our expected method call
|
82
|
+
@assert router._bindRoutes?, 'provided router is not a Backbone.Router'
|
83
|
+
|
82
84
|
router._bindRoutes() # inject router routes into our history
|
83
85
|
if options.considering? # if multiple routers are provided load their routes aswell
|
84
86
|
consideredRouter._bindRoutes() for consideredRouter in options.considering
|
@@ -19,14 +19,30 @@ window.Factory =
|
|
19
19
|
@factories[factoryName] =
|
20
20
|
sequences: {}
|
21
21
|
factory: builder
|
22
|
-
sequence: sequencer
|
23
22
|
|
24
|
-
create: (nameWithTraits,
|
23
|
+
create: (nameWithTraits, args...) ->
|
25
24
|
traits = nameWithTraits.split '-'
|
26
25
|
factoryName = traits.pop()
|
27
26
|
unless @factories[factoryName]?
|
28
27
|
throw "Factory #{factoryName} does not exist"
|
29
|
-
|
28
|
+
|
29
|
+
f = @factories[factoryName]
|
30
|
+
obj =
|
31
|
+
sequences: f.sequences
|
32
|
+
factory: f.factory
|
33
|
+
sequence: sequencer
|
34
|
+
traits: traits
|
35
|
+
hasTrait: (name) -> ~@traits.indexOf(name)
|
36
|
+
trait: (names...) ->
|
37
|
+
for name in @traits
|
38
|
+
return name if ~names.indexOf(name)
|
39
|
+
sample: (values...) ->
|
40
|
+
values[Math.floor(Math.random() * values.length)]
|
41
|
+
|
42
|
+
r = obj.factory args...
|
43
|
+
f.sequences = obj.sequences
|
44
|
+
obj = null
|
45
|
+
r
|
30
46
|
|
31
47
|
resetFactories: ->
|
32
48
|
@factories = []
|
@@ -3,8 +3,8 @@
|
|
3
3
|
describe 'Factory', ->
|
4
4
|
|
5
5
|
beforeEach ->
|
6
|
-
Factory.define 'user', (
|
7
|
-
|
6
|
+
Factory.define 'user', (args...) ->
|
7
|
+
args: args
|
8
8
|
|
9
9
|
afterEach ->
|
10
10
|
Factory.resetFactories()
|
@@ -13,8 +13,7 @@ describe 'Factory', ->
|
|
13
13
|
|
14
14
|
it 'registers a factory', ->
|
15
15
|
result = Factory.create 'user'
|
16
|
-
result
|
17
|
-
result[1].should.deep.equal []
|
16
|
+
result.args.should.deep.equal []
|
18
17
|
|
19
18
|
it 'raises an error on existing factory', ->
|
20
19
|
expect(-> Factory.define('user', ->)).to.throw 'Factory user is already defined'
|
@@ -28,25 +27,61 @@ describe 'Factory', ->
|
|
28
27
|
result = Factory.create 'user'
|
29
28
|
hello: 'world'
|
30
29
|
other: 'value'
|
31
|
-
result[0].should.deep.equal { hello: 'world', other: 'value' }
|
30
|
+
result.args[0].should.deep.equal { hello: 'world', other: 'value' }
|
32
31
|
|
33
|
-
it '
|
34
|
-
result = Factory.create '
|
35
|
-
result
|
32
|
+
it 'accepts multiple arguments', ->
|
33
|
+
result = Factory.create 'user', 1, 2, 3
|
34
|
+
result.args.should.deep.equal [1, 2, 3]
|
36
35
|
|
37
36
|
|
38
37
|
describe 'helpers', ->
|
39
38
|
|
39
|
+
describe 'traits', ->
|
40
|
+
beforeEach ->
|
41
|
+
Factory.define 'traits', ->
|
42
|
+
traits: @traits
|
43
|
+
|
44
|
+
it 'delivers traits to the callback', ->
|
45
|
+
result = Factory.create 'something-with-traits'
|
46
|
+
result.traits.should.deep.equal ['something', 'with']
|
47
|
+
|
48
|
+
describe '#hasTrait', ->
|
49
|
+
|
50
|
+
it 'checks if trait is set', ->
|
51
|
+
Factory.define 'hasTrait', ->
|
52
|
+
@hasTrait('hello')
|
53
|
+
|
54
|
+
Factory.create('hello-hasTrait').should.be.ok
|
55
|
+
Factory.create('bye-hasTrait').should.not.be.ok
|
56
|
+
Factory.create('bye-other-hello-somewhere-hasTrait').should.be.ok
|
57
|
+
|
58
|
+
describe '#trait', ->
|
59
|
+
|
60
|
+
beforeEach ->
|
61
|
+
Factory.define 'trait', ->
|
62
|
+
@trait('red', 'green', 'refactor')
|
63
|
+
|
64
|
+
it 'returns undefined if no values match', ->
|
65
|
+
expect(Factory.create('refgreen-trait')).to.be.undefined
|
66
|
+
|
67
|
+
it 'returns one of the provided trait values', ->
|
68
|
+
Factory.create('green-trait').should.equal 'green'
|
69
|
+
Factory.create('red-trait').should.equal 'red'
|
70
|
+
|
71
|
+
it 'returns the first encountered if multiple values match', ->
|
72
|
+
Factory.create('green-red-trait').should.equal 'green'
|
73
|
+
Factory.create('refactor-green-trait').should.equal 'refactor'
|
74
|
+
|
40
75
|
describe 'sequence', ->
|
41
76
|
|
42
77
|
beforeEach ->
|
43
|
-
Factory.define 'counter',
|
78
|
+
Factory.define 'counter', ->
|
44
79
|
@sequence('property')
|
45
80
|
|
46
|
-
Factory.define 'otherCounter',
|
81
|
+
Factory.define 'otherCounter', ->
|
47
82
|
@sequence('property')
|
48
83
|
|
49
|
-
Factory.define 'abc',
|
84
|
+
Factory.define 'abc', ->
|
50
85
|
@sequence((c) -> ['a', 'b', 'c'][c])
|
51
86
|
|
52
87
|
it 'provides sequencers scoped to factory and property', ->
|
@@ -58,3 +93,20 @@ describe 'Factory', ->
|
|
58
93
|
Factory.create('abc').should.equal 'a'
|
59
94
|
Factory.create('abc').should.equal 'b'
|
60
95
|
|
96
|
+
describe 'sample', ->
|
97
|
+
|
98
|
+
beforeEach ->
|
99
|
+
Factory.define 'sampling', ->
|
100
|
+
@sample('henk', 'piet', 'kees')
|
101
|
+
|
102
|
+
afterEach ->
|
103
|
+
@mathStub?.restore()
|
104
|
+
|
105
|
+
it 'returns one of the provided values', ->
|
106
|
+
@mathStub = sinon.stub Math, 'random', -> 0.01
|
107
|
+
Factory.create('sampling').should.eql 'henk'
|
108
|
+
|
109
|
+
it 'uses random to decide value', ->
|
110
|
+
@mathStub = sinon.stub Math, 'random', -> 0.99
|
111
|
+
Factory.create('sampling').should.eql 'kees'
|
112
|
+
|
metadata
CHANGED
@@ -1,23 +1,33 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: chai-backbone-rails
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Matthijs Groen
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
17
|
+
|
18
|
+
date: 2012-09-30 00:00:00 Z
|
13
19
|
dependencies: []
|
20
|
+
|
14
21
|
description: Chai.js matchers for Backbone.js framework
|
15
|
-
email:
|
22
|
+
email:
|
16
23
|
- matthijs.groen@gmail.com
|
17
24
|
executables: []
|
25
|
+
|
18
26
|
extensions: []
|
27
|
+
|
19
28
|
extra_rdoc_files: []
|
20
|
-
|
29
|
+
|
30
|
+
files:
|
21
31
|
- .gitignore
|
22
32
|
- Gemfile
|
23
33
|
- LICENSE
|
@@ -38,26 +48,36 @@ files:
|
|
38
48
|
- vendor/assets/javascripts/spec/factory_spec.js.coffee
|
39
49
|
homepage: https://github.com/matthijsgroen/chai-backbone-rails
|
40
50
|
licenses: []
|
51
|
+
|
41
52
|
post_install_message:
|
42
53
|
rdoc_options: []
|
43
|
-
|
54
|
+
|
55
|
+
require_paths:
|
44
56
|
- lib
|
45
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
58
|
none: false
|
47
|
-
requirements:
|
48
|
-
- -
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
|
51
|
-
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
67
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
57
75
|
requirements: []
|
76
|
+
|
58
77
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.8.
|
78
|
+
rubygems_version: 1.8.15
|
60
79
|
signing_key:
|
61
80
|
specification_version: 3
|
62
81
|
summary: A set of assertion matchers to test Backbone code using Konacha in Rails
|
63
82
|
test_files: []
|
83
|
+
|