jspec-steventux 3.3.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/History.md +763 -0
- data/Manifest +73 -0
- data/README.md +974 -0
- data/Rakefile +44 -0
- data/bin/jspec +178 -0
- data/jspec-steventux.gemspec +44 -0
- data/lib/images/bg.png +0 -0
- data/lib/images/hr.png +0 -0
- data/lib/images/loading.gif +0 -0
- data/lib/images/sprites.bg.png +0 -0
- data/lib/images/sprites.png +0 -0
- data/lib/images/vr.png +0 -0
- data/lib/jspec.css +149 -0
- data/lib/jspec.growl.js +115 -0
- data/lib/jspec.jquery.js +72 -0
- data/lib/jspec.js +1756 -0
- data/lib/jspec.shell.js +39 -0
- data/lib/jspec.timers.js +90 -0
- data/lib/jspec.xhr.js +195 -0
- data/spec/commands/example_command.rb +19 -0
- data/spec/dom.html +33 -0
- data/spec/fixtures/test.html +1 -0
- data/spec/fixtures/test.json +1 -0
- data/spec/fixtures/test.xml +5 -0
- data/spec/node.js +17 -0
- data/spec/rhino.js +23 -0
- data/spec/ruby/bin/init_spec.rb +101 -0
- data/spec/ruby/bin/install_spec.rb +142 -0
- data/spec/ruby/bin/run_spec.rb +0 -0
- data/spec/ruby/bin/shell_spec.rb +13 -0
- data/spec/ruby/bin/spec_helper.rb +8 -0
- data/spec/ruby/bin/update_spec.rb +72 -0
- data/spec/server.html +29 -0
- data/spec/server.rb +2 -0
- data/spec/support/env.js +10118 -0
- data/spec/support/jquery.js +4376 -0
- data/spec/unit/helpers.js +64 -0
- data/spec/unit/spec.fixtures.js +17 -0
- data/spec/unit/spec.grammar-less.js +34 -0
- data/spec/unit/spec.grammar.js +241 -0
- data/spec/unit/spec.jquery.js +178 -0
- data/spec/unit/spec.jquery.xhr.js +84 -0
- data/spec/unit/spec.js +187 -0
- data/spec/unit/spec.matchers.js +577 -0
- data/spec/unit/spec.modules.js +51 -0
- data/spec/unit/spec.shared-behaviors.js +80 -0
- data/spec/unit/spec.utils.js +346 -0
- data/spec/unit/spec.xhr.js +157 -0
- data/src/browsers.rb +294 -0
- data/src/helpers.rb +67 -0
- data/src/installables.rb +229 -0
- data/src/project.rb +341 -0
- data/src/routes.rb +57 -0
- data/src/server.rb +99 -0
- data/support/js.jar +0 -0
- data/templates/default/History.md +5 -0
- data/templates/default/Readme.md +29 -0
- data/templates/default/lib/yourlib.js +2 -0
- data/templates/default/spec/commands/example_command.rb +19 -0
- data/templates/default/spec/dom.html +22 -0
- data/templates/default/spec/node.js +10 -0
- data/templates/default/spec/rhino.js +10 -0
- data/templates/default/spec/server.html +18 -0
- data/templates/default/spec/server.rb +4 -0
- data/templates/default/spec/unit/spec.helper.js +0 -0
- data/templates/default/spec/unit/spec.js +8 -0
- data/templates/rails/commands/example_commands.rb +19 -0
- data/templates/rails/dom.html +22 -0
- data/templates/rails/rhino.js +10 -0
- data/templates/rails/server.html +18 -0
- data/templates/rails/server.rb +4 -0
- data/templates/rails/unit/spec.helper.js +0 -0
- data/templates/rails/unit/spec.js +8 -0
- metadata +185 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
|
2
|
+
describe 'jQuery'
|
3
|
+
describe '.ajax()'
|
4
|
+
it "should call the success function when 200"
|
5
|
+
mock_request().and_return('{ foo: "bar" }', 'application/json')
|
6
|
+
var successCalled = false
|
7
|
+
var errorCalled = false
|
8
|
+
$.ajax({
|
9
|
+
type: "POST",
|
10
|
+
url: 'foo',
|
11
|
+
dataType: 'json',
|
12
|
+
success: function() {
|
13
|
+
successCalled = true
|
14
|
+
},
|
15
|
+
error: function(xhr, status, e) {
|
16
|
+
errorCalled = true
|
17
|
+
}
|
18
|
+
})
|
19
|
+
successCalled.should.be_true
|
20
|
+
errorCalled.should.be_false
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should call the error function when 404"
|
24
|
+
mock_request().and_return('{ foo: "bar" }', 'application/json', 404)
|
25
|
+
var successCalled = false
|
26
|
+
var errorCalled = false
|
27
|
+
$.ajax({
|
28
|
+
type: "POST",
|
29
|
+
url: 'foo',
|
30
|
+
dataType: 'json',
|
31
|
+
success: function() {
|
32
|
+
successCalled = true
|
33
|
+
},
|
34
|
+
error: function() {
|
35
|
+
errorCalled = true
|
36
|
+
}
|
37
|
+
})
|
38
|
+
successCalled.should.be_false
|
39
|
+
errorCalled.should.be_true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should work with getScript()'
|
44
|
+
mock_request().and_return('var foo = "bar"', 'application/javascript', 200)
|
45
|
+
var called = false
|
46
|
+
$.getScript('foobar', function(data, textStatus){
|
47
|
+
called = true
|
48
|
+
})
|
49
|
+
called.should.be_true
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '.getJSON()'
|
53
|
+
it 'should work with mockRequest'
|
54
|
+
mockRequest().and_return('{ foo : "bar" }')
|
55
|
+
$.getJSON('foo', function(response, statusText){
|
56
|
+
response.foo.should.eql 'bar'
|
57
|
+
statusText.should.eql 'success'
|
58
|
+
})
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should work with a json fixture'
|
62
|
+
mockRequest().and_return(fixture('test.json'))
|
63
|
+
$.getJSON('foo', function(response){
|
64
|
+
response.users.tj.email.should.eql 'tj@vision-media.ca'
|
65
|
+
})
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should not invoke callback when response status is 4xx'
|
69
|
+
mockRequest().and_return('foo', 'text/plain', 404)
|
70
|
+
$.getJSON('foo', function(){
|
71
|
+
fail('callback was invoked')
|
72
|
+
})
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '.post()'
|
77
|
+
it 'should work with mockRequest'
|
78
|
+
mockRequest().and_return('<p></p>', 'text/html')
|
79
|
+
$.post('foo', function(response){
|
80
|
+
response.should.eql '<p></p>'
|
81
|
+
})
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/spec/unit/spec.js
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
|
2
|
+
describe 'Failing specs'
|
3
|
+
|
4
|
+
it 'should fail'
|
5
|
+
spec = mock_it(function(){
|
6
|
+
'test'.should.not.eql 'test'
|
7
|
+
})
|
8
|
+
spec.should.have_failure_message('expected "test" to not eql "test"')
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should fail with one faulty assertion'
|
12
|
+
spec = mock_it(function() {
|
13
|
+
'test'.should.equal 'test'
|
14
|
+
'test'.should.equal 'foo'
|
15
|
+
})
|
16
|
+
spec.should.have_failure_message('expected "test" to be "foo"')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should fail and print array with square braces'
|
20
|
+
spec = mock_it(function() {
|
21
|
+
[1,2].should.equal [1,3]
|
22
|
+
})
|
23
|
+
spec.should.have_failure_message("expected [ 1, 2 ] to be [ 1, 3 ]")
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should fail and print nested array'
|
27
|
+
spec = mock_it(function() {
|
28
|
+
[1, ['foo']].should.equal [1, ['bar', ['whatever', 1.0, { foo : 'bar', bar : { 1 : 2 } }]]]
|
29
|
+
})
|
30
|
+
spec.should.have_failure_message(/^expected \[\s*1,\s*\[\s*"foo"/)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should fail with selector for jQuery objects'
|
34
|
+
spec = mock_it(function() {
|
35
|
+
elem = { jquery : '1.3.1', selector : '.foobar' }
|
36
|
+
elem.should.eql 'foo'
|
37
|
+
})
|
38
|
+
spec.should.have_failure_message('expected selector ".foobar" to eql "foo"')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should fail with negated message'
|
42
|
+
spec = mock_it(function(){
|
43
|
+
'1'.should.not.be_true
|
44
|
+
})
|
45
|
+
spec.should.have_failure_message(/expected "1" to not be true/)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should fail with positive message'
|
49
|
+
spec = mock_it(function() {
|
50
|
+
false.should.be_true
|
51
|
+
})
|
52
|
+
spec.should.have_failure_message(/expected false to be true/)
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'throw_error'
|
56
|
+
before
|
57
|
+
// The Error.name property is not defined in IE
|
58
|
+
Error.name = "Error"
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should fail saying which error has been thrown'
|
62
|
+
spec = mock_it(function() {
|
63
|
+
-{ throw 'foo' }.should.throw_error 'bar'
|
64
|
+
})
|
65
|
+
spec.should.have_failure_message('expected exception of "bar" to be thrown, but got "foo"')
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should fail saying no error was thrown'
|
69
|
+
spec = mock_it(function() {
|
70
|
+
-{ }.should.throw_error 'foo'
|
71
|
+
})
|
72
|
+
spec.should.have_failure_message('expected exception of "foo" to be thrown, but nothing was')
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should fail saying no error matching was thrown'
|
76
|
+
spec = mock_it(function() {
|
77
|
+
-{ throw 'bar' }.should.throw_error(/foo/)
|
78
|
+
})
|
79
|
+
spec.should.have_failure_message('expected exception matching /foo/ to be thrown, but got "bar"')
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should fail saying constructors'
|
83
|
+
spec = mock_it(function() {
|
84
|
+
// IE loses scope of virtually everything once global inside nested eval()s
|
85
|
+
// Create everything we need here.
|
86
|
+
function CustomError(message) { this.message = message }
|
87
|
+
CustomError.prototype.toString = -{ return 'CustomError: oh no' }
|
88
|
+
-{ throw new CustomError('oh no') }.should.throw_error(Error)
|
89
|
+
})
|
90
|
+
spec.should.have_failure_message("expected Error to be thrown, but got CustomError: oh no")
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'should fail saying multiple arg messages'
|
94
|
+
spec = mock_it(function() {
|
95
|
+
// IE loses scope of virtually everything once global inside nested eval()s
|
96
|
+
// Create everything we need here.
|
97
|
+
function CustomError(message) { this.message = message }
|
98
|
+
CustomError.name = "CustomError"
|
99
|
+
CustomError.prototype.toString = function(){ return 'CustomError: oh no' }
|
100
|
+
-{ throw new CustomError('oh no') }.should.throw_error(CustomError, /foo/)
|
101
|
+
})
|
102
|
+
spec.should.have_failure_message("expected CustomError and exception matching /foo/ to be thrown, but got CustomError: oh no")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should fail with constructor name'
|
107
|
+
spec = mock_it(function() {
|
108
|
+
function Foo(){ this.toString = function(){ return '<Foo>' }}
|
109
|
+
Foo.name = "Foo"
|
110
|
+
foo = new Foo
|
111
|
+
foo.should.not.be_an_instance_of Foo
|
112
|
+
})
|
113
|
+
spec.should.have_failure_message("expected <Foo> to not be an instance of Foo")
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should fail with message of first failure'
|
117
|
+
spec = mock_it(function() {
|
118
|
+
true.should.be_true
|
119
|
+
'bar'.should.match(/foo/gm)
|
120
|
+
'bar'.should.include 'foo'
|
121
|
+
})
|
122
|
+
spec.should.have_failure_message('expected "bar" to match /foo/gm')
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'should fail with list'
|
126
|
+
spec = mock_it(function() {
|
127
|
+
['foo', 'bar'].should.include 'foo', 'car'
|
128
|
+
})
|
129
|
+
spec.should.have_failure_message('expected [ "foo", "bar" ] to include "foo", "car"')
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'should list all failure messages'
|
133
|
+
spec = mock_it(function() {
|
134
|
+
false.should.be_true
|
135
|
+
true.should.be_false
|
136
|
+
})
|
137
|
+
spec.should.have_failure_message('expected false to be true ')
|
138
|
+
spec.should.have_failure_message('expected true to be false ')
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'should catch exceptions throw within specs'
|
142
|
+
spec = mock_it(function() {
|
143
|
+
// IE loses scope of virtually everything once global inside nested eval()s
|
144
|
+
// Create everything we need here.
|
145
|
+
Error.prototype.toString = -{ return "Error: " + this.message }
|
146
|
+
throw new Error('Oh noes!')
|
147
|
+
})
|
148
|
+
spec.should.have_failure_message(/Error: Oh noes!/)
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'should catch exceptions without constructors'
|
152
|
+
spec = mock_it(function() {
|
153
|
+
throw 'oh noes'
|
154
|
+
})
|
155
|
+
spec.should.have_failure_message(/oh noes/)
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'should catch indirect exceptions'
|
159
|
+
spec = mock_it(function() {
|
160
|
+
iDoNotExist.neitherDoI()
|
161
|
+
})
|
162
|
+
// NOTE: Most browsers will specifically mention iDoNotExist being undefined.
|
163
|
+
// IE only reports an Error.
|
164
|
+
spec.should.have_failure_message(/Error/)
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
describe 'Contexts'
|
170
|
+
before
|
171
|
+
JSpec.context = { iLike : 'cookies' }
|
172
|
+
end
|
173
|
+
|
174
|
+
after
|
175
|
+
JSpec.context = null
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'should be replaceable'
|
179
|
+
iLike.should.equal 'cookies'
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe 'Misc'
|
184
|
+
it 'requires implementation'
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
@@ -0,0 +1,577 @@
|
|
1
|
+
|
2
|
+
describe 'Matchers'
|
3
|
+
|
4
|
+
describe 'eql'
|
5
|
+
it 'should work with strings'
|
6
|
+
'test'.should.eql 'test'
|
7
|
+
'test'.should.not.eql 'foo'
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should work with numbers'
|
11
|
+
11.should.eql 11
|
12
|
+
10.should.not.eql 11
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should work with regular expressions'
|
16
|
+
a = /foo/
|
17
|
+
b = /foo/
|
18
|
+
c = /foo/g
|
19
|
+
d = /bar/
|
20
|
+
a.should.eql a
|
21
|
+
a.should.eql b
|
22
|
+
a.should.not.eql c
|
23
|
+
a.should.not.eql d
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should work with dates'
|
27
|
+
a = new Date('May 25 1987')
|
28
|
+
b = new Date('May 25 1987')
|
29
|
+
c = new Date('May 25 1988')
|
30
|
+
a.should.eql b
|
31
|
+
a.should.not.eql c
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should work with arrays'
|
35
|
+
[1, 2].should.eql [1, 2]
|
36
|
+
[1, 2].should.not.eql [1, 3]
|
37
|
+
[1, 2, [3], { foo : 'bar' }].should.eql [1, 2, [3], { foo : 'bar' }]
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should work with objects'
|
41
|
+
{ foo : 'bar' }.should.eql { foo : 'bar' }
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should hash compare objects with different orders'
|
45
|
+
a = { one : 'two', three : 'four' }
|
46
|
+
b = { three : 'four', one : 'two' }
|
47
|
+
a.should.eql b
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should work with functions'
|
51
|
+
function foo(){}
|
52
|
+
function bar(){}
|
53
|
+
foo.should.eql foo
|
54
|
+
foo.should.not.eql bar
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should work with constructors'
|
58
|
+
Array.should.eql Array
|
59
|
+
Array.should.not.eql Object
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'equal'
|
64
|
+
it 'should perform strict comparisons'
|
65
|
+
'test'.should.equal 'test'
|
66
|
+
'1'.should.not.equal 1
|
67
|
+
true.should.be true
|
68
|
+
'1'.should.not.be true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'match'
|
73
|
+
it 'should match regular expressions'
|
74
|
+
'foobar'.should.match(/foo/)
|
75
|
+
'foobar'.should.not.match(/barfoo/)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe 'be_empty'
|
80
|
+
it 'should consider any object with zero length to be empty'
|
81
|
+
''.should.be_empty
|
82
|
+
' '.should.not.be_empty
|
83
|
+
[].should.be_empty
|
84
|
+
{ length : 0 }.should.be_empty
|
85
|
+
{}.should.be_empty
|
86
|
+
'cookies'.should.not.be_empty
|
87
|
+
[0].should.not.be_empty
|
88
|
+
{ length : 1 }.should.not.be_empty
|
89
|
+
{ foo : 'bar' }.should.not.be_empty
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'be_null'
|
94
|
+
it 'should check if a value is null'
|
95
|
+
a = 0
|
96
|
+
b = null
|
97
|
+
null.should.be_null
|
98
|
+
0.should.not.be_null
|
99
|
+
a.should.not.be_null
|
100
|
+
b.should.be_null
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe 'be_undefined'
|
105
|
+
it 'should check if a var is defined'
|
106
|
+
var rawr
|
107
|
+
rawr.should.be_undefined
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe 'have_length'
|
112
|
+
it 'should compare the length of an object'
|
113
|
+
'foo'.should.have_length 3
|
114
|
+
[1, 2].should.have_length 2
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe 'have_length_within'
|
119
|
+
it 'should check if an object has a length within the specified range'
|
120
|
+
'foo'.should.have_length_within 2..4
|
121
|
+
'f'.should.not.have_length_within 2..4
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe 'have_prop'
|
126
|
+
it 'should check if a property exists'
|
127
|
+
'foo'.should.have_prop 'length'
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should check that a property has a specific value'
|
131
|
+
'foo'.should.have_prop 'length', 3
|
132
|
+
{ length : 3 }.should.have_prop 'length', 3
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should check object hashes'
|
136
|
+
{ foo : 1..3 }.should.have_prop 'foo', 1..3
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'should fail when the property does not exist'
|
140
|
+
'foo'.should.not.have_prop 'foo'
|
141
|
+
'foo'.should.not.have_prop 'foo', 'bar'
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'should fail when it is a function'
|
145
|
+
'foo'.should.not.have_prop 'toString'
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe 'have_property'
|
150
|
+
it 'should check if a property exists'
|
151
|
+
'foo'.should.have_property 'length'
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'should check that a property has a specific value'
|
155
|
+
'foo'.should.have_property 'length', 3
|
156
|
+
{ length : '3' }.should.not.have_property 'length', 3
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'should fail when the property does not exist'
|
160
|
+
'foo'.should.not.have_property 'foo'
|
161
|
+
'foo'.should.not.have_property 'foo', 'bar'
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'should fail when it is a function'
|
165
|
+
'foo'.should.not.have_property 'toString'
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe 'respond_to'
|
170
|
+
it 'should check if an object contains a method'
|
171
|
+
'test'.should.respond_to('toString')
|
172
|
+
'test'.should.not.respond_to('rawr')
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe 'include'
|
177
|
+
it 'should check if an object includes a property'
|
178
|
+
{ hey : 'there' }.should.include 'hey'
|
179
|
+
{ hey : 'there' }.should.not.include 'foo'
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'should check if a regular expression includes a string'
|
183
|
+
(/(foo)?bar/).should.include '(foo)'
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'should check if a string contains a substring'
|
187
|
+
'if (foo) return [foo, bar]'.should.include '[foo, bar'
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'should check if a function body includes a string'
|
191
|
+
-{ return [foo, bar] }.should.include 'foo', 'bar'
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'should check if an array contains element(s)'
|
195
|
+
[1,2,3].should.include 1
|
196
|
+
[1,2,3].should.include 1, 2, 3
|
197
|
+
[1].should.not.include 0
|
198
|
+
['foo', 'bar'].should.include 'foo', 'bar'
|
199
|
+
['foo', 'bar'].should.include 'bar', 'foo'
|
200
|
+
['foo', 'bar'].should.not.include 'foo', 'rawr'
|
201
|
+
['foo', 'bar'].should.not.include 'rawr', 'foo'
|
202
|
+
end
|
203
|
+
|
204
|
+
it 'should check hashes of array elements'
|
205
|
+
[1, [2]].should.include [2]
|
206
|
+
[1, [2]].should.include [2], 1
|
207
|
+
[1, { two : 'three' }].should.include { two : 'three' }
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe 'be_a'
|
212
|
+
it 'should compare the constructor of an object'
|
213
|
+
'test'.should.be_a String
|
214
|
+
[].should.be_an Array
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
describe 'throw_error'
|
219
|
+
it 'should check if an error is thrown'
|
220
|
+
-{ throw 'error' }.should.throw_error
|
221
|
+
-{ return 'test' }.should.not.throw_error
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'should check if an error with a specific message is thrown'
|
225
|
+
-{ throw 'some foo bar' }.should.throw_error('some foo bar')
|
226
|
+
-{ throw 'some foo bar' }.should.throw_error(/foo bar/)
|
227
|
+
-{ throw 'some foo bar' }.should.not.throw_error(/rawr/)
|
228
|
+
-{ throw 'some foo bar' }.should.not.throw_error('rawr')
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'should check if an error of a specific constructor is thrown'
|
232
|
+
function CustomError() {}
|
233
|
+
CustomError.name = "CustomError"
|
234
|
+
-{ throw new Error('foo') }.should.throw_error(Error)
|
235
|
+
-{ throw new CustomError('foo') }.should.throw_error(CustomError)
|
236
|
+
-{ throw 'foo' }.should.not.throw_error CustomError
|
237
|
+
-{ throw new CustomError('foo') }.should.not.throw_error Error
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'should check if an error with a specific constructor and message is thrown'
|
241
|
+
Error.name = "Error"
|
242
|
+
function CustomError(message) { this.message = message }
|
243
|
+
CustomError.name = "CustomError"
|
244
|
+
CustomError.prototype.toString = function(){ return 'CustomError: ' + this.message }
|
245
|
+
-{ throw new CustomError('oh no!') }.should.throw_error(CustomError, 'oh no!')
|
246
|
+
-{ throw new CustomError('oh no!') }.should.not.throw_error(CustomError, 'foo bar')
|
247
|
+
-{ throw new CustomError('oh no!') }.should.throw_error(CustomError, /oh no/)
|
248
|
+
-{ throw new CustomError('oh no!') }.should.not.throw_error(CustomError, /foo bar/)
|
249
|
+
-{ throw new CustomError('oh no!') }.should.not.throw_error(Error, 'oh no!')
|
250
|
+
-{ throw new CustomError('oh no!') }.should.not.throw_error(Error, 'foo bar')
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
describe 'be_an_instance_of'
|
255
|
+
it 'should check that an object is an instance of another'
|
256
|
+
MyObject = function(){}
|
257
|
+
myInstance = new MyObject()
|
258
|
+
{}.should.be_an_instance_of Object
|
259
|
+
[].should.be_an_instance_of Array
|
260
|
+
MyObject.should.be_an_instance_of Function
|
261
|
+
myInstance.should.be_an_instance_of MyObject
|
262
|
+
myInstance.should.be_an_instance_of Object
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
describe 'be_type'
|
267
|
+
it 'should compare the type of an object via typeof'
|
268
|
+
'hey'.should.be_type 'string'
|
269
|
+
{}.should.be_type 'object'
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
describe 'be_within'
|
274
|
+
it 'should check if a number is within a range'
|
275
|
+
5.should.be_within 1..10
|
276
|
+
15.should.not.be_within 10..5
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
describe 'have'
|
281
|
+
it 'should check the length of a property'
|
282
|
+
person = { pets : ['izzy', 'niko'] }
|
283
|
+
person.should.have 2, 'pets'
|
284
|
+
person.should.not.have 3, 'pets'
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
describe 'have_at_least'
|
289
|
+
it 'should check if a object has at least n of a property'
|
290
|
+
person = { pets : ['izzy', 'niko'] }
|
291
|
+
person.should.have_at_least 1, 'pets'
|
292
|
+
person.should.have_at_least 2, 'pets'
|
293
|
+
person.should.not.have_at_least 3, 'pets'
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
describe 'have_at_most'
|
298
|
+
it 'should check if an object has at most n of a property'
|
299
|
+
person = { pets : ['izzy', 'niko'] }
|
300
|
+
person.should.have_at_most 2, 'pets'
|
301
|
+
person.should.have_at_most 3, 'pets'
|
302
|
+
person.should.not.have_at_most 1, 'pets'
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
describe 'receive'
|
307
|
+
before_each
|
308
|
+
person = { toString : function(){ return 'person' }}
|
309
|
+
personWithPets = {
|
310
|
+
toString : function(){ return 'personWithPets' },
|
311
|
+
getPets : function() { return ['izzy'] },
|
312
|
+
addPet : function(name) { return ['izzy', name] },
|
313
|
+
addPets : function(a, b) { return ['izzy', a, b] }
|
314
|
+
}
|
315
|
+
end
|
316
|
+
|
317
|
+
it 'should pass when the method is invoked'
|
318
|
+
personWithPets.should.receive('getPets')
|
319
|
+
personWithPets.getPets()
|
320
|
+
end
|
321
|
+
|
322
|
+
it 'should pass and original method should still return its result'
|
323
|
+
personWithPets.should.receive('getPets')
|
324
|
+
personWithPets.getPets().should.eql ['izzy']
|
325
|
+
end
|
326
|
+
|
327
|
+
it 'should pass when the proper value is returned'
|
328
|
+
personWithPets.should.receive('getPets').and_return(['izzy'])
|
329
|
+
personWithPets.getPets()
|
330
|
+
end
|
331
|
+
|
332
|
+
it 'should pass when invoked the expected number of times'
|
333
|
+
personWithPets.should.receive('getPets', 'twice').and_return(['izzy'])
|
334
|
+
personWithPets.getPets()
|
335
|
+
personWithPets.getPets()
|
336
|
+
end
|
337
|
+
|
338
|
+
it 'should pass when a method is invoked with specific arguments'
|
339
|
+
personWithPets.should.receive('addPet', 'once').with_args('suki')
|
340
|
+
personWithPets.addPet('suki')
|
341
|
+
end
|
342
|
+
|
343
|
+
it 'should pass with multiple arguments'
|
344
|
+
personWithPets.should.receive('addPets').with_args('suki', 'max')
|
345
|
+
personWithPets.addPets('suki', 'max')
|
346
|
+
end
|
347
|
+
|
348
|
+
it 'should pass with arguments and return value'
|
349
|
+
personWithPets.should.receive('addPet').with_args('suki').and_return(['izzy', 'suki'])
|
350
|
+
personWithPets.addPet('suki')
|
351
|
+
end
|
352
|
+
|
353
|
+
it 'should pass when argument is the correct type'
|
354
|
+
personWithPets.should.receive('addPet').with_args(an_instance_of(String))
|
355
|
+
personWithPets.addPet('suki')
|
356
|
+
end
|
357
|
+
|
358
|
+
it 'should pass when return type is correct'
|
359
|
+
personWithPets.should.receive('addPet').and_return(an_instance_of(Array))
|
360
|
+
personWithPets.addPet('suki')
|
361
|
+
end
|
362
|
+
|
363
|
+
it 'should pass when checking the type of multiple args and return types'
|
364
|
+
personWithPets.should.receive('addPets').with_args(an_instance_of(String), an_instance_of(String)).and_return(an_instance_of(Array))
|
365
|
+
personWithPets.addPets('suki', 'max')
|
366
|
+
end
|
367
|
+
|
368
|
+
it 'should pass with negation when a method is not called'
|
369
|
+
personWithPets.should.not.receive('addPets')
|
370
|
+
end
|
371
|
+
|
372
|
+
it 'should pass with negation with args'
|
373
|
+
personWithPets.should.not.receive('addPets').with_args('izzy')
|
374
|
+
personWithPets.addPets('max')
|
375
|
+
end
|
376
|
+
|
377
|
+
it 'should pass with negation with return values'
|
378
|
+
personWithPets.should.not.receive('addPets').with_args('izzy').and_return('test')
|
379
|
+
personWithPets.addPets('izzy')
|
380
|
+
end
|
381
|
+
|
382
|
+
it 'should pass with negation with times'
|
383
|
+
personWithPets.should.not.receive('addPets', 'twice')
|
384
|
+
personWithPets.addPets('izzy')
|
385
|
+
end
|
386
|
+
|
387
|
+
it 'should pass with boolean args'
|
388
|
+
foo = { bar : function(arg){ return arg }}
|
389
|
+
foo.should.receive('bar', 'twice').with_args(true)
|
390
|
+
foo.bar(true)
|
391
|
+
foo.bar(true)
|
392
|
+
end
|
393
|
+
|
394
|
+
it 'should pass with null args'
|
395
|
+
foo = { bar : function(arg){ return arg }}
|
396
|
+
foo.should.receive('bar').with_args(null)
|
397
|
+
foo.bar(null)
|
398
|
+
end
|
399
|
+
|
400
|
+
it 'should pass with boolean return value true'
|
401
|
+
foo = { bar : function(){ return true }}
|
402
|
+
foo.should.receive('bar').and_return(true)
|
403
|
+
foo.bar()
|
404
|
+
end
|
405
|
+
|
406
|
+
it 'should pass with boolean return value false'
|
407
|
+
foo = { bar : function(){ return false }}
|
408
|
+
foo.should.receive('bar').and_return(false)
|
409
|
+
foo.bar()
|
410
|
+
end
|
411
|
+
|
412
|
+
it 'should pass with null return value'
|
413
|
+
foo = { bar : function(){ return null }}
|
414
|
+
foo.should.receive('bar').and_return(null)
|
415
|
+
foo.bar()
|
416
|
+
end
|
417
|
+
|
418
|
+
it 'should fail when the method does not exist'
|
419
|
+
spec = mock_it(function(){
|
420
|
+
person.should.receive('getPets')
|
421
|
+
})
|
422
|
+
spec.should.have_failure_message('expected person.getPets() to be called once, but was not called')
|
423
|
+
end
|
424
|
+
|
425
|
+
it 'should fail when the method is never invoked'
|
426
|
+
spec = mock_it(function(){
|
427
|
+
personWithPets.should.receive('getPets')
|
428
|
+
})
|
429
|
+
spec.should.have_failure_message('expected personWithPets.getPets() to be called once, but was not called')
|
430
|
+
end
|
431
|
+
|
432
|
+
it 'should fail when improper value is returned'
|
433
|
+
spec = mock_it(function(){
|
434
|
+
personWithPets.should.receive('getPets').and_return(['niko'])
|
435
|
+
personWithPets.getPets()
|
436
|
+
})
|
437
|
+
spec.should.have_failure_message('expected personWithPets.getPets() to return [ "niko" ] but got [ "izzy" ]')
|
438
|
+
end
|
439
|
+
|
440
|
+
it 'should fail when checking the type of multiple args and return types'
|
441
|
+
spec = mock_it(function(){
|
442
|
+
personWithPets.should.receive('addPets').with_args(an_instance_of(String), an_instance_of(Array)).and_return(an_instance_of(Array))
|
443
|
+
personWithPets.addPets('suki', 'max')
|
444
|
+
})
|
445
|
+
spec.should.have_failure_message('expected personWithPets.addPets() to be called with an instance of String, an instance of Array but was called with "suki", "max"')
|
446
|
+
end
|
447
|
+
|
448
|
+
it 'should fail when not invoked the expected number of times'
|
449
|
+
spec = mock_it(function(){
|
450
|
+
personWithPets.should.receive('getPets', 'twice').and_return(['izzy'])
|
451
|
+
personWithPets.getPets()
|
452
|
+
})
|
453
|
+
spec.should.have_failure_message('expected personWithPets.getPets() to be called twice, but was called once')
|
454
|
+
end
|
455
|
+
|
456
|
+
it 'should fail when not invoked many times'
|
457
|
+
spec = mock_it(function(){
|
458
|
+
personWithPets.should.receive('getPets', 3).and_return(['izzy'])
|
459
|
+
personWithPets.getPets()
|
460
|
+
personWithPets.getPets()
|
461
|
+
})
|
462
|
+
spec.should.have_failure_message('expected personWithPets.getPets() to be called 3 times, but was called twice')
|
463
|
+
end
|
464
|
+
|
465
|
+
it 'should fail when not invoked with specific arguments'
|
466
|
+
spec = mock_it(function(){
|
467
|
+
personWithPets.should.receive('addPet', 'once').with_args('suki')
|
468
|
+
personWithPets.addPet('niko')
|
469
|
+
})
|
470
|
+
spec.should.have_failure_message('expected personWithPets.addPet() to be called with "suki" but was called with "niko"')
|
471
|
+
end
|
472
|
+
|
473
|
+
it 'should fail when expecting multiple arguments'
|
474
|
+
spec = mock_it(function(){
|
475
|
+
personWithPets.should.receive('addPets').with_args('suki', 'max')
|
476
|
+
personWithPets.addPets('suki')
|
477
|
+
})
|
478
|
+
spec.should.have_failure_message('expected personWithPets.addPets() to be called with "suki", "max" but was called with "suki"')
|
479
|
+
end
|
480
|
+
|
481
|
+
it 'should fail when argument is of the wrong type'
|
482
|
+
spec = mock_it(function(){
|
483
|
+
personWithPets.should.receive('addPet').with_args(an_instance_of(String))
|
484
|
+
personWithPets.addPet(['suki'])
|
485
|
+
})
|
486
|
+
spec.should.have_failure_message('expected personWithPets.addPet() to be called with an instance of String but was called with [ "suki" ]')
|
487
|
+
end
|
488
|
+
|
489
|
+
it 'should fail when return type is incorrect'
|
490
|
+
spec = mock_it(function(){
|
491
|
+
personWithPets.should.receive('addPet').and_return(an_instance_of(String))
|
492
|
+
personWithPets.addPet('suki')
|
493
|
+
})
|
494
|
+
spec.should.have_failure_message('expected personWithPets.addPet() to return an instance of String but got [ "izzy", "suki" ]')
|
495
|
+
end
|
496
|
+
|
497
|
+
it 'should fail with negation when a method is called'
|
498
|
+
spec = mock_it(function(){
|
499
|
+
personWithPets.should.not.receive('addPets')
|
500
|
+
personWithPets.addPets('izzy')
|
501
|
+
})
|
502
|
+
spec.should.have_failure_message('expected personWithPets.addPets() not to be called once, but was called once')
|
503
|
+
end
|
504
|
+
|
505
|
+
it 'should fail with negation with args'
|
506
|
+
spec = mock_it(function(){
|
507
|
+
personWithPets.should.not.receive('addPets').with_args('izzy')
|
508
|
+
personWithPets.addPets('izzy')
|
509
|
+
})
|
510
|
+
spec.should.have_failure_message('expected personWithPets.addPets() not to be called with "izzy" but was')
|
511
|
+
end
|
512
|
+
|
513
|
+
it 'should fail with negation with return values'
|
514
|
+
spec = mock_it(function(){
|
515
|
+
personWithPets.should.not.receive('addPets').with_args('izzy').and_return(an_instance_of(Array))
|
516
|
+
personWithPets.addPets('izzy')
|
517
|
+
})
|
518
|
+
spec.should.have_failure_message('expected personWithPets.addPets() not to return an instance of Array but it did')
|
519
|
+
end
|
520
|
+
|
521
|
+
it 'should fail with negation when called once with no times specified'
|
522
|
+
spec = mock_it(function(){
|
523
|
+
personWithPets.should.not.receive('addPets')
|
524
|
+
personWithPets.addPets('izzy')
|
525
|
+
})
|
526
|
+
spec.should.have_failure_message('expected personWithPets.addPets() not to be called once, but was called once')
|
527
|
+
end
|
528
|
+
|
529
|
+
it 'should fail with negation when called more than once with no times specified'
|
530
|
+
spec = mock_it(function(){
|
531
|
+
personWithPets.should.not.receive('addPets')
|
532
|
+
personWithPets.addPets('izzy')
|
533
|
+
personWithPets.addPets('izzy')
|
534
|
+
})
|
535
|
+
spec.should.have_failure_message('expected personWithPets.addPets() not to be called once, but was called twice')
|
536
|
+
end
|
537
|
+
|
538
|
+
it 'should fail with negation when called more than the times specified'
|
539
|
+
spec = mock_it(function(){
|
540
|
+
personWithPets.should.not.receive('addPets', 3)
|
541
|
+
personWithPets.addPets('izzy')
|
542
|
+
personWithPets.addPets('izzy')
|
543
|
+
personWithPets.addPets('izzy')
|
544
|
+
personWithPets.addPets('izzy')
|
545
|
+
})
|
546
|
+
spec.should.have_failure_message('expected personWithPets.addPets() not to be called 3 times, but was called 4 times')
|
547
|
+
end
|
548
|
+
|
549
|
+
it 'should fail with boolean args'
|
550
|
+
spec = mock_it(function(){
|
551
|
+
foo = { bar : function(arg){ return arg }}
|
552
|
+
foo.should.receive('bar').with_args(true)
|
553
|
+
foo.bar(false)
|
554
|
+
})
|
555
|
+
spec.should.have_failure_message('expected [object Object].bar() to be called with true but was called with false')
|
556
|
+
end
|
557
|
+
|
558
|
+
it 'should fail with boolean return value true'
|
559
|
+
spec = mock_it(function(){
|
560
|
+
foo = { bar : function(){ return true }}
|
561
|
+
foo.should.receive('bar').and_return(false)
|
562
|
+
foo.bar()
|
563
|
+
})
|
564
|
+
spec.should.have_failure_message('expected [object Object].bar() to return false but got true')
|
565
|
+
end
|
566
|
+
|
567
|
+
it 'should fail with boolean return value false'
|
568
|
+
spec = mock_it(function(){
|
569
|
+
foo = { bar : function(){ return false }}
|
570
|
+
foo.should.receive('bar').and_return(true)
|
571
|
+
foo.bar()
|
572
|
+
})
|
573
|
+
spec.should.have_failure_message('expected [object Object].bar() to return true but got false')
|
574
|
+
end
|
575
|
+
end
|
576
|
+
|
577
|
+
end
|