chai-backbone-rails 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitmodules ADDED
@@ -0,0 +1,9 @@
1
+ [submodule "chai-backbone"]
2
+ path = chai-backbone
3
+ url = git://github.com/matthijsgroen/chai-backbone.git
4
+ [submodule "chai-changes"]
5
+ path = chai-changes
6
+ url = git://github.com/matthijsgroen/chai-changes.git
7
+ [submodule "js-factories"]
8
+ path = js-factories
9
+ url = git://github.com/matthijsgroen/js-factories.git
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'rake'
3
4
  # Specify your gem's dependencies in chai-backbone-rails.gemspec
4
5
  gemspec
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
- Backbone::Chai
2
- ==============
1
+ Chai-Backbone-Rails
2
+ ===================
3
3
 
4
- - Adds Chai matchers for common backbone assertions
5
4
  - Adds Chai matchers to assert changes
6
- - Adds Chai matchers for common sinon assertions
5
+ - Adds Chai matchers for common backbone assertions
7
6
  - Adds support for Factories
8
7
 
9
8
  Installation
@@ -21,69 +20,50 @@ Or install it yourself as:
21
20
 
22
21
  $ gem install chai-backbone-rails
23
22
 
24
- Using Backbone Chai Matchers
25
- ----------------------------
23
+ Dependencies
24
+ ------------
26
25
 
27
- #= require chai-backbone
26
+ The provided libraries have dependencies to
27
+ [backbone](http://documentcloud.github.com/backbone/),
28
+ [underscore](http://documentcloud.github.com/underscore/) and
29
+ [sinon](http://sinonjs.org/)
28
30
 
29
- ### Triggers
31
+ to install these dependencies, you can add the following to your
32
+ `Gemfile`:
30
33
 
31
- model.should.trigger("change", with: [model]).when -> model.set attribute: "value"
34
+ gem 'sinonjs-rails'
35
+ gem 'rails-backbone'
32
36
 
33
- this can also be chained further:
37
+ and in your `spec_helper`:
34
38
 
35
- model.should.trigger("change").and.trigger("change:attribute").when -> model.set attribute: "value"
36
- model.should.trigger("change").and.not.trigger("reset").when -> model.set attribute: "value"
39
+ #= require sinon/sinon-1.5.0
40
+ #= require underscore
41
+ #= require backbone
42
+ #= require chai-backbone-rails
37
43
 
38
- ### Routing
44
+ to run all specs of the libraries:
39
45
 
40
- "page/3".should.route.to myRouter, "openPage", arguments: ["3"]
41
- "page/3".should.route.to myRouter, "openPage", considering: [conflictingRouter]
46
+ #= require chai-backbone-rails-spec
42
47
 
43
48
  Using Changes Chai matchers
44
49
  ---------------------------
45
50
 
46
51
  #= require chai-changes
47
52
 
48
- Plain change checking:
49
-
50
- expect(-> result).to.change.when -> result += 1
51
- expect(-> result).to.not.change.when -> somethingElse()
52
-
53
- Changes by delta: 'change.by'
54
-
55
- expect(-> view.$('p').length).to.change.by(4).when -> collection.add [{}, {}, {}, {}]
56
-
57
- Changes to end result: 'change.to'
58
-
59
- result = ['a']
60
- expect(-> result).to.change.to(['b']).when -> result = ['b']
61
-
62
- Changes from begin result: 'change.from'
53
+ See [the Chai-changes plugin page](http://chaijs.com/plugins/chai-changes)
63
54
 
64
- result = ['a']
65
- expect(-> result).to.change.from(['a']).when -> result = ['b']
55
+ or [the Node.js package page](https://npmjs.org/package/chai-changes)
66
56
 
67
- Mix and match:
68
57
 
69
- result = 3
70
- expect(-> result).to.change.from(3).to(5).by(2).when -> result = 5
71
-
72
-
73
- Using Sinon Chai Matchers
74
- -------------------------
58
+ Using Backbone Chai Matchers
59
+ ----------------------------
75
60
 
76
- Matchers have also been added for sinonjs.
61
+ #= require chai-backbone
77
62
 
78
- #= require chai-sinon
63
+ See [the Chai-backbone plugin page](http://chaijs.com/plugins/chai-backbone)
79
64
 
80
- These are not complete yet, see tests and code for details.
65
+ or [the Node.js package page](https://npmjs.org/package/chai-backbone)
81
66
 
82
- spy.should.have.been.called.exactly(x).times
83
- spy.should.have.been.called.before otherSpy
84
- spy.should.have.been.called.after otherSpy
85
- spy.should.have.been.called.with "argument1", 2, "argument3"
86
- spy.should.not.have.been.called
87
67
 
88
68
  Using Factories
89
69
  ---------------
@@ -93,98 +73,34 @@ other objects as you see fit:
93
73
 
94
74
  #= require factories
95
75
 
96
- Factory.define 'user', (attributes = {}) ->
97
- new User attributes
98
-
99
- Factory.create 'user', name: 'Matthijs'
100
-
101
- ### Traits
102
-
103
- you can also use 'traits'.
104
- Traits are flags that are set when the user calls create with the
105
- factory name prefixed with terms separated by dashes.
106
-
107
- Like: 'female-admin-user'
108
-
109
- This will call the 'user' factory, and provide the terms 'female' and
110
- 'admin' as traits for this user
111
-
112
- this list is accessible in the factory callback using `this.traits`
76
+ See [the Node.js package page](https://npmjs.org/package/js-factories)
113
77
 
114
- There are 2 helper methods to help check if traits are set:
115
-
116
- this.trait('returns', 'one', 'of', 'these', 'values')
117
-
118
- and
119
-
120
- this.is('admin') # returns a boolean value
121
-
122
- Extended example:
123
-
124
- Factory.define 'user', (attributes = {}) ->
125
- attributes.gender = @trait('male', 'female') || 'male'
126
-
127
- returningClass = User
128
- if @is('admin')
129
- returningClass = AdminUser
130
-
131
- new returningClass attributes
132
-
133
- Factory.create 'user', name: 'Matthijs' # => new User name: 'Matthijs'
134
- Factory.create 'male-user', name: 'Matthijs' # => new User name: 'Matthijs', gender: 'male'
135
- Factory.create 'male-admin-user', name: 'Matthijs' # => new AdminUser name: 'Matthijs', gender: 'male'
136
- Factory.create 'female-user', name: 'Beppie' # => new User name: 'Beppie', gender: 'female'
137
-
138
- ### Sequences
139
-
140
- Sequences are also supported:
141
-
142
- Factory.define 'counter', ->
143
- {
144
- amount: @sequence('amount')
145
- other: @sequence('other')
146
- }
147
-
148
- This does not conflict with similar names in other factory definitions.
149
-
150
- You can also yield results:
151
-
152
- Factory.define 'abc', ->
153
- @sequence (i) -> ['a','b','c'][i]
154
-
155
- # results in:
156
- Factory.create('abc') => 'a'
157
- Factory.create('abc') => 'b'
158
-
159
- ### Sampling
160
-
161
- You can sample a value from a list
162
-
163
- Factory.define 'sampler', ->
164
- @sample 'a', 'b', 'c'
165
-
166
- Will randomly return a, b or c every time
167
78
 
168
79
  Running the tests
169
80
  =================
170
81
 
171
- You can runn the tests by including:
82
+ You can run the tests by including:
172
83
 
173
84
  #= require chai-backbone_spec
174
85
 
175
86
  or you can run the suites seperately:
176
87
 
177
88
  #= require spec/chai-backbone_spec
178
- #= require spec/chai-sinon_spec
179
89
  #= require spec/chai-changes_spec
180
90
  #= require spec/factory_spec
181
91
 
182
92
 
183
93
  Contributing
184
- ------------
94
+ ============
185
95
 
186
96
  1. Fork it
187
97
  2. Create your feature branch (`git checkout -b my-new-feature`)
188
98
  3. Commit your changes (`git commit -am 'Added some feature'`)
189
99
  4. Push to the branch (`git push origin my-new-feature`)
190
100
  5. Create new Pull Request
101
+
102
+ Todo
103
+ ----
104
+
105
+ - Add a rake task to update dependencies
106
+
data/USAGE-CHANGES.md ADDED
@@ -0,0 +1,74 @@
1
+ chai-changes
2
+ ============
3
+
4
+ chai-changes is an extension to the [chai](http://chaijs.com/) assertion library that
5
+ provides a set of change-specific assertions.
6
+
7
+ Usage
8
+ -----
9
+
10
+ Include `chai-changes.js` in your test file, after `chai.js` (version 1.0.0-rc1 or later):
11
+
12
+ <script src="chai-changes.js"></script>
13
+
14
+ Use the assertions with chai's `expect` or `should` assertions.
15
+
16
+ Assertions
17
+ ----------
18
+
19
+ All assertions use a `when` mechanism.
20
+
21
+
22
+ Using 'expect':
23
+
24
+ expect(-> codeThatYieldsAChangedResult).to....when ->
25
+ executeTheCodeThatCausesTheChange()
26
+
27
+ The code within the `expect` section will be executed first, then the
28
+ code in the `when` section will be executed and then the code in the
29
+ `expect` section will be executed again and the differences will be
30
+ asserted.
31
+
32
+ Same test using 'should':
33
+
34
+ (-> codeThatYieldsAChangedResult).should....when ->
35
+ executeTheCodeThatCausesTheChange()
36
+
37
+ ### `change`
38
+
39
+ Assert if the 'expect/should' changes its outcome when 'when' is
40
+ executed
41
+
42
+ result = 0
43
+ (-> result).should.change.when -> result += 1
44
+ expect(-> result).to.change.when -> result -= 1
45
+ expect(-> result).not.to.change.when -> result = result * 1
46
+
47
+ ### `change.by(delta)`
48
+
49
+ Assert if the change of the 'expect/should' has the provided delta
50
+
51
+ result = 0
52
+ (-> result).should.change.by(3).when -> result += 3
53
+ expect(-> result).not.to.change.by(-3).when -> result += 1
54
+ expect(-> result).to.change.by(-2).when -> result -= 2
55
+
56
+ ### `change.from(startValue)`
57
+
58
+ Assert if the change starts from a certain value. The value is
59
+ compared using a deep equal.
60
+
61
+ result = ['a', 'b']
62
+ (-> result).should.change.from(['a', 'b']).when -> result.push('c')
63
+ (-> result).should.change.from(['a', 'b']).to(['a', 'b', 'c']).when -> result.push('c')
64
+
65
+ ### `change.to(endValue)`
66
+
67
+ Assert if the change ends in a certain value. The value is
68
+ compared using a deep equal.
69
+
70
+ result = ['a', 'b']
71
+ (-> result).should.change.to(['a', 'b', 'c']).when -> result.push('c')
72
+ (-> result).should.change.from(['a', 'b']).to(['a', 'c']).when -> result = ['a', 'c']
73
+
74
+
@@ -0,0 +1,3 @@
1
+ #= require spec/chai-backbone_spec
2
+ #= require spec/chai-changes_spec
3
+ #= require spec/factory_spec
@@ -0,0 +1,3 @@
1
+ #= require chai-changes
2
+ #= require chai-backbone
3
+ #= require factories
@@ -1,7 +1,7 @@
1
1
  module Chai
2
2
  module Backbone
3
3
  module Rails
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,5 +1,3 @@
1
- #= require underscore
2
- #= require ./chai-changes
3
1
 
4
2
  ((chaiBackbone) ->
5
3
  # Module systems magic dance.
@@ -116,5 +114,26 @@
116
114
  else
117
115
  _super.apply(this, arguments)
118
116
 
117
+ chai.Assertion.addMethod 'call', (methodName) ->
118
+ object = flag(this, 'object')
119
+ definedActions = flag(this, 'whenActions') || []
120
+ definedActions.push
121
+ negate: flag(this, 'negate')
122
+ before: (context) ->
123
+ @originalMethod = object[methodName]
124
+ @spy = sinon.spy()
125
+ object[methodName] = @spy
126
+ object.delegateEvents?()
127
+ after: (context) ->
128
+ object[methodName] = @originalMethod
129
+ object.delegateEvents?()
130
+
131
+ context.assert @spy.callCount > 0,
132
+ @spy.printf("expected %n to have been called at least once"),
133
+ @spy.printf("expected %n to not have been called")
134
+
135
+ flag(this, 'whenActions', definedActions)
136
+
119
137
  )
120
138
 
139
+
@@ -53,11 +53,9 @@
53
53
  endValue = object()
54
54
  actualDelta = endValue - startValue
55
55
 
56
- result = (@expectedDelta is actualDelta)
57
- result = !result if negate
58
- context.assert result,
56
+ context.assert (@expectedDelta is actualDelta),
59
57
  "expected `#{formatFunction object}` to change by #{@expectedDelta}, but it changed by #{actualDelta}",
60
- "not supported"
58
+ "expected `#{formatFunction object}` not to change by #{@expectedDelta}, but it did"
61
59
  flag(context, 'negate', negate)
62
60
 
63
61
  changeToBeginAssert = (context) ->
@@ -82,10 +80,9 @@
82
80
  endValue = object()
83
81
 
84
82
  result = utils.eql(endValue, @expectedEndValue)
85
- result = !result if negate
86
83
  context.assert result,
87
84
  "expected `#{formatFunction object}` to change to #{utils.inspect @expectedEndValue}, but it changed to #{utils.inspect endValue}",
88
- "not supported"
85
+ "expected `#{formatFunction object}` not to change to #{utils.inspect @expectedEndValue}, but it did"
89
86
  flag(context, 'negate', negate)
90
87
 
91
88
  changeFromBeginAssert = (context) ->
@@ -96,10 +93,9 @@
96
93
  startValue = object()
97
94
 
98
95
  result = utils.eql(startValue, @expectedStartValue)
99
- result = !result if negate
100
96
  context.assert result,
101
- "expected `#{formatFunction object}` to change from #{utils.inspect @expectedStartValue}, but it changed from #{utils.inspect startValue}",
102
- "not supported"
97
+ "expected the change of `#{formatFunction object}` to start from #{utils.inspect @expectedStartValue}, but it started from #{utils.inspect startValue}",
98
+ "expected the change of `#{formatFunction object}` not to start from #{utils.inspect @expectedStartValue}, but it did",
103
99
  flag(context, 'negate', negate)
104
100
 
105
101
  changeFromAssert = (context) ->
@@ -181,3 +177,4 @@
181
177
 
182
178
  )
183
179
 
180
+
@@ -1,3 +1,4 @@
1
+
1
2
  sequencer = (property) ->
2
3
  value = if @sequences[property]?
3
4
  @sequences[property] += 1
@@ -49,3 +50,4 @@ window.Factory =
49
50
 
50
51
 
51
52
 
53
+
@@ -1,4 +1,3 @@
1
- #= require ./../chai-backbone
2
1
 
3
2
  describe 'Chai-Backbone', ->
4
3
 
@@ -49,3 +48,31 @@ describe 'Chai-Backbone', ->
49
48
  it 'leaves the `to` keyword working properly', ->
50
49
  expect('1').to.be.equal '1'
51
50
 
51
+ describe 'call', ->
52
+
53
+ it 'asserts if a method on provided object is called', ->
54
+ obj =
55
+ method: ->
56
+
57
+ obj.should.call('method').when ->
58
+ obj.method()
59
+
60
+ it 'raises AssertionError if method was not called', ->
61
+ obj =
62
+ method: ->
63
+ expect(->
64
+ obj.should.call('method').when ->
65
+ "noop"
66
+ ).to.throw /been called/
67
+
68
+ if Backbone? and jQuery?
69
+ it 'can check event calls of Backbone.Views', ->
70
+ viewClass = class extends Backbone.View
71
+ events:
72
+ 'click': 'eventCall'
73
+ eventCall: ->
74
+
75
+ viewInstance = new viewClass
76
+ viewInstance.should.call('eventCall').when ->
77
+ viewInstance.$el.trigger('click')
78
+