rspec-expectations 2.12.1 → 2.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/Changelog.md +31 -0
  2. data/README.md +1 -1
  3. data/features/built_in_matchers/be.feature +6 -4
  4. data/features/built_in_matchers/be_within.feature +3 -1
  5. data/features/built_in_matchers/cover.feature +2 -0
  6. data/features/built_in_matchers/end_with.feature +2 -0
  7. data/features/built_in_matchers/equality.feature +9 -15
  8. data/features/built_in_matchers/exist.feature +2 -0
  9. data/features/built_in_matchers/expect_error.feature +14 -8
  10. data/features/built_in_matchers/have.feature +11 -5
  11. data/features/built_in_matchers/include.feature +53 -0
  12. data/features/built_in_matchers/match.feature +2 -0
  13. data/features/built_in_matchers/operators.feature +17 -11
  14. data/features/built_in_matchers/predicates.feature +21 -13
  15. data/features/built_in_matchers/respond_to.feature +7 -1
  16. data/features/built_in_matchers/satisfy.feature +2 -0
  17. data/features/built_in_matchers/start_with.feature +2 -0
  18. data/features/built_in_matchers/throw_symbol.feature +6 -0
  19. data/features/built_in_matchers/types.feature +8 -6
  20. data/lib/rspec/expectations/deprecation.rb +1 -1
  21. data/lib/rspec/expectations/differ.rb +8 -8
  22. data/lib/rspec/expectations/fail_with.rb +17 -3
  23. data/lib/rspec/expectations/syntax.rb +46 -0
  24. data/lib/rspec/expectations/version.rb +1 -1
  25. data/lib/rspec/matchers/built_in/be.rb +7 -3
  26. data/lib/rspec/matchers/built_in/be_within.rb +13 -4
  27. data/lib/rspec/matchers/built_in/change.rb +2 -2
  28. data/lib/rspec/matchers/built_in/equal.rb +5 -1
  29. data/lib/rspec/matchers/built_in/exist.rb +1 -1
  30. data/lib/rspec/matchers/built_in/have.rb +8 -8
  31. data/lib/rspec/matchers/built_in/include.rb +19 -3
  32. data/lib/rspec/matchers/built_in/respond_to.rb +1 -1
  33. data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +1 -1
  34. data/lib/rspec/matchers/matcher.rb +4 -3
  35. data/lib/rspec/matchers/operator_matcher.rb +1 -1
  36. data/lib/rspec/matchers/pretty.rb +5 -1
  37. data/spec/rspec/expectations/differ_spec.rb +8 -15
  38. data/spec/rspec/expectations/expectation_target_spec.rb +18 -8
  39. data/spec/rspec/expectations/extensions/kernel_spec.rb +15 -15
  40. data/spec/rspec/expectations/fail_with_spec.rb +41 -16
  41. data/spec/rspec/expectations/handler_spec.rb +13 -13
  42. data/spec/rspec/expectations/syntax_spec.rb +70 -8
  43. data/spec/rspec/matchers/base_matcher_spec.rb +14 -12
  44. data/spec/rspec/matchers/be_close_spec.rb +1 -1
  45. data/spec/rspec/matchers/be_instance_of_spec.rb +14 -8
  46. data/spec/rspec/matchers/be_kind_of_spec.rb +12 -8
  47. data/spec/rspec/matchers/be_spec.rb +212 -148
  48. data/spec/rspec/matchers/be_within_spec.rb +91 -42
  49. data/spec/rspec/matchers/change_spec.rb +52 -38
  50. data/spec/rspec/matchers/configuration_spec.rb +19 -15
  51. data/spec/rspec/matchers/cover_spec.rb +19 -19
  52. data/spec/rspec/matchers/description_generation_spec.rb +86 -86
  53. data/spec/rspec/matchers/dsl_spec.rb +7 -7
  54. data/spec/rspec/matchers/eq_spec.rb +17 -11
  55. data/spec/rspec/matchers/eql_spec.rb +10 -10
  56. data/spec/rspec/matchers/equal_spec.rb +27 -9
  57. data/spec/rspec/matchers/exist_spec.rb +35 -21
  58. data/spec/rspec/matchers/has_spec.rb +33 -29
  59. data/spec/rspec/matchers/have_spec.rb +165 -151
  60. data/spec/rspec/matchers/include_matcher_integration_spec.rb +30 -0
  61. data/spec/rspec/matchers/include_spec.rb +282 -124
  62. data/spec/rspec/matchers/match_array_spec.rb +90 -49
  63. data/spec/rspec/matchers/match_spec.rb +21 -21
  64. data/spec/rspec/matchers/matcher_spec.rb +85 -48
  65. data/spec/rspec/matchers/matchers_spec.rb +12 -6
  66. data/spec/rspec/matchers/method_missing_spec.rb +5 -1
  67. data/spec/rspec/matchers/operator_matcher_spec.rb +216 -237
  68. data/spec/rspec/matchers/raise_error_spec.rb +132 -132
  69. data/spec/rspec/matchers/respond_to_spec.rb +109 -112
  70. data/spec/rspec/matchers/satisfy_spec.rb +16 -16
  71. data/spec/rspec/matchers/start_with_end_with_spec.rb +36 -32
  72. data/spec/rspec/matchers/throw_symbol_spec.rb +24 -24
  73. data/spec/rspec/matchers/yield_spec.rb +7 -7
  74. data/spec/spec_helper.rb +46 -19
  75. data/spec/support/in_sub_process.rb +27 -20
  76. metadata +81 -83
@@ -8,15 +8,15 @@ module RSpec
8
8
  end
9
9
 
10
10
  it "is diffable" do
11
- eq(1).should be_diffable
11
+ expect(eq(1)).to be_diffable
12
12
  end
13
13
 
14
14
  it "matches when actual == expected" do
15
- 1.should eq(1)
15
+ expect(1).to eq(1)
16
16
  end
17
-
17
+
18
18
  it "does not match when actual != expected" do
19
- 1.should_not eq(2)
19
+ expect(1).not_to eq(2)
20
20
  end
21
21
 
22
22
  it "compares by sending == to actual (not expected)" do
@@ -27,26 +27,32 @@ module RSpec
27
27
  end
28
28
  end.new
29
29
 
30
- actual.should eq :anything # to trigger the matches? method
31
- called.should be_true
30
+ expect(actual).to eq :anything # to trigger the matches? method
31
+ expect(called).to be_true
32
32
  end
33
33
 
34
34
  it "describes itself" do
35
35
  matcher = eq(1)
36
36
  matcher.matches?(1)
37
- matcher.description.should eq "eq 1"
37
+ expect(matcher.description).to eq "eq 1"
38
38
  end
39
-
39
+
40
40
  it "provides message, expected and actual on #failure_message" do
41
41
  matcher = eq("1")
42
42
  matcher.matches?(1)
43
- matcher.failure_message_for_should.should eq "\nexpected: \"1\"\n got: 1\n\n(compared using ==)\n"
43
+ expect(matcher.failure_message_for_should).to eq "\nexpected: \"1\"\n got: 1\n\n(compared using ==)\n"
44
44
  end
45
-
45
+
46
46
  it "provides message, expected and actual on #negative_failure_message" do
47
47
  matcher = eq(1)
48
48
  matcher.matches?(1)
49
- matcher.failure_message_for_should_not.should eq "\nexpected: value != 1\n got: 1\n\n(compared using ==)\n"
49
+ expect(matcher.failure_message_for_should_not).to eq "\nexpected: value != 1\n got: 1\n\n(compared using ==)\n"
50
+ end
51
+
52
+ it 'fails properly when the actual is an array of multiline strings' do
53
+ expect {
54
+ expect(["a\nb", "c\nd"]).to eq([])
55
+ }.to fail_matching("expected: []")
50
56
  end
51
57
  end
52
58
  end
@@ -8,33 +8,33 @@ module RSpec
8
8
  end
9
9
 
10
10
  it "is diffable" do
11
- eql(1).should be_diffable
11
+ expect(eql(1)).to be_diffable
12
12
  end
13
13
 
14
14
  it "matches when actual.eql?(expected)" do
15
- 1.should eql(1)
15
+ expect(1).to eql(1)
16
16
  end
17
-
17
+
18
18
  it "does not match when !actual.eql?(expected)" do
19
- 1.should_not eql(2)
19
+ expect(1).not_to eql(2)
20
20
  end
21
-
21
+
22
22
  it "describes itself" do
23
23
  matcher = eql(1)
24
24
  matcher.matches?(1)
25
- matcher.description.should == "eql 1"
25
+ expect(matcher.description).to eq "eql 1"
26
26
  end
27
-
27
+
28
28
  it "provides message, expected and actual on #failure_message" do
29
29
  matcher = eql("1")
30
30
  matcher.matches?(1)
31
- matcher.failure_message_for_should.should == "\nexpected: \"1\"\n got: 1\n\n(compared using eql?)\n"
31
+ expect(matcher.failure_message_for_should).to eq "\nexpected: \"1\"\n got: 1\n\n(compared using eql?)\n"
32
32
  end
33
-
33
+
34
34
  it "provides message, expected and actual on #negative_failure_message" do
35
35
  matcher = eql(1)
36
36
  matcher.matches?(1)
37
- matcher.failure_message_for_should_not.should == "\nexpected: value != 1\n got: 1\n\n(compared using eql?)\n"
37
+ expect(matcher.failure_message_for_should_not).to eq "\nexpected: value != 1\n got: 1\n\n(compared using eql?)\n"
38
38
  end
39
39
  end
40
40
  end
@@ -11,42 +11,60 @@ module RSpec
11
11
  end
12
12
 
13
13
  it "matches when actual.equal?(expected)" do
14
- 1.should equal(1)
14
+ expect(1).to equal(1)
15
15
  end
16
16
 
17
17
  it "does not match when !actual.equal?(expected)" do
18
- "1".should_not equal("1")
18
+ expect("1").not_to equal("1")
19
19
  end
20
20
 
21
21
  it "describes itself" do
22
22
  matcher = equal(1)
23
23
  matcher.matches?(1)
24
- matcher.description.should == "equal 1"
24
+ expect(matcher.description).to eq "equal 1"
25
25
  end
26
26
 
27
- it "provides message on #failure_message" do
27
+ it "suggests the `eq` matcher on failure" do
28
28
  expected, actual = "1", "1"
29
- matcher = equal(expected)
30
- matcher.matches?(actual)
29
+ expect {
30
+ expect(actual).to equal(expected)
31
+ }.to fail_with <<-MESSAGE
32
+
33
+ expected #{inspect_object(expected)}
34
+ got #{inspect_object(actual)}
35
+
36
+ Compared using equal?, which compares object identity,
37
+ but expected and actual are not the same object. Use
38
+ `expect(actual).to eq(expected)` if you don't care about
39
+ object identity in this example.
40
+
41
+ MESSAGE
42
+ end
31
43
 
32
- matcher.failure_message_for_should.should == <<-MESSAGE
44
+ context "when using only `should`", :uses_only_should do
45
+ it "suggests the `eq` matcher on failure" do
46
+ expected, actual = "1", "1"
47
+ lambda {
48
+ actual.should equal(expected)
49
+ }.should fail_with <<-MESSAGE
33
50
 
34
51
  expected #{inspect_object(expected)}
35
52
  got #{inspect_object(actual)}
36
53
 
37
54
  Compared using equal?, which compares object identity,
38
55
  but expected and actual are not the same object. Use
39
- 'actual.should eq(expected)' if you don't care about
56
+ `actual.should eq(expected)` if you don't care about
40
57
  object identity in this example.
41
58
 
42
59
  MESSAGE
60
+ end
43
61
  end
44
62
 
45
63
  it "provides message on #negative_failure_message" do
46
64
  expected = actual = "1"
47
65
  matcher = equal(expected)
48
66
  matcher.matches?(actual)
49
- matcher.failure_message_for_should_not.should == <<-MESSAGE
67
+ expect(matcher.failure_message_for_should_not).to eq <<-MESSAGE
50
68
 
51
69
  expected not #{inspect_object(expected)}
52
70
  got #{inspect_object(actual)}
@@ -10,11 +10,11 @@ describe "exist matcher" do
10
10
  context "when the object does not respond to #exist? or #exists?" do
11
11
  subject { mock }
12
12
 
13
- [:should, :should_not].each do |should_method|
14
- describe "#{should_method} exist" do
13
+ [:to, :not_to].each do |expect_method|
14
+ describe "expect(...).#{expect_method} exist" do
15
15
  it "raises an error" do
16
16
  expect {
17
- subject.send(should_method, exist)
17
+ expect(subject).send(expect_method, exist)
18
18
  }.to raise_error(NoMethodError)
19
19
  end
20
20
  end
@@ -23,26 +23,40 @@ describe "exist matcher" do
23
23
 
24
24
  [:exist?, :exists?].each do |predicate|
25
25
  context "when the object responds to ##{predicate}" do
26
- describe "should exist" do
26
+ describe "expect(...).to exist" do
27
27
  it "passes if #{predicate}" do
28
- mock(predicate => true).should exist
28
+ expect(mock(predicate => true)).to exist
29
29
  end
30
30
 
31
31
  it "fails if not #{predicate}" do
32
32
  expect {
33
- mock(predicate => false).should exist
33
+ expect(mock(predicate => false)).to exist
34
34
  }.to fail_with(/expected .* to exist/)
35
35
  end
36
+
37
+ it 'works when the object overrides `send`' do
38
+ klass = Struct.new(:message) do
39
+ def send
40
+ :message_sent
41
+ end
42
+
43
+ define_method predicate do
44
+ true
45
+ end
46
+ end
47
+
48
+ expect(klass.new("msg")).to exist
49
+ end
36
50
  end
37
51
 
38
- describe "should not exist" do
52
+ describe "expect(...).not_to exist" do
39
53
  it "passes if not #{predicate}" do
40
- mock(predicate => false).should_not exist
54
+ expect(mock(predicate => false)).not_to exist
41
55
  end
42
56
 
43
57
  it "fails if #{predicate}" do
44
58
  expect {
45
- mock(predicate => true).should_not exist
59
+ expect(mock(predicate => true)).not_to exist
46
60
  }.to fail_with(/expected .* not to exist/)
47
61
  end
48
62
  end
@@ -53,16 +67,16 @@ describe "exist matcher" do
53
67
  context "when they both return falsey values" do
54
68
  subject { mock(:exist? => false, :exists? => nil) }
55
69
 
56
- describe "should_not exist" do
70
+ describe "expect(...).not_to exist" do
57
71
  it "passes" do
58
- subject.should_not exist
72
+ expect(subject).not_to exist
59
73
  end
60
74
  end
61
75
 
62
- describe "should exist" do
76
+ describe "expect(...).to exist" do
63
77
  it "fails" do
64
78
  expect {
65
- subject.should exist
79
+ expect(subject).to exist
66
80
  }.to fail_with(/expected .* to exist/)
67
81
  end
68
82
  end
@@ -71,17 +85,17 @@ describe "exist matcher" do
71
85
  context "when they both return truthy values" do
72
86
  subject { mock(:exist? => true, :exists? => "something true") }
73
87
 
74
- describe "should_not exist" do
88
+ describe "expect(...).not_to exist" do
75
89
  it "fails" do
76
90
  expect {
77
- subject.should_not exist
91
+ expect(subject).not_to exist
78
92
  }.to fail_with(/expected .* not to exist/)
79
93
  end
80
94
  end
81
95
 
82
- describe "should exist" do
96
+ describe "expect(...).to exist" do
83
97
  it "passes" do
84
- subject.should exist
98
+ expect(subject).to exist
85
99
  end
86
100
  end
87
101
  end
@@ -89,11 +103,11 @@ describe "exist matcher" do
89
103
  context "when they return values with different truthiness" do
90
104
  subject { mock(:exist? => true, :exists? => false) }
91
105
 
92
- [:should, :should_not].each do |should_method|
93
- describe "#{should_method} exist" do
106
+ [:to, :not_to].each do |expect_method|
107
+ describe "expect(...).#{expect_method} exist" do
94
108
  it "raises an error" do
95
109
  expect {
96
- subject.send(should_method, exist)
110
+ expect(subject).send(expect_method, exist)
97
111
  }.to raise_error(/#exist\? and #exists\? returned different values/)
98
112
  end
99
113
  end
@@ -105,6 +119,6 @@ describe "exist matcher" do
105
119
  object = mock
106
120
  object.should_receive(:exist?).with(:foo, :bar) { true }
107
121
 
108
- object.should exist(:foo, :bar)
122
+ expect(object).to exist(:foo, :bar)
109
123
  end
110
124
  end
@@ -1,26 +1,26 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "should have_sym(*args)" do
3
+ describe "expect(...).to have_sym(*args)" do
4
4
  it_behaves_like "an RSpec matcher", :valid_value => { :a => 1 },
5
5
  :invalid_value => {} do
6
6
  let(:matcher) { have_key(:a) }
7
7
  end
8
8
 
9
9
  it "passes if #has_sym?(*args) returns true" do
10
- {:a => "A"}.should have_key(:a)
10
+ expect({:a => "A"}).to have_key(:a)
11
11
  end
12
12
 
13
13
  it "fails if #has_sym?(*args) returns false" do
14
- lambda {
15
- {:b => "B"}.should have_key(:a)
16
- }.should fail_with("expected #has_key?(:a) to return true, got false")
14
+ expect {
15
+ expect({:b => "B"}).to have_key(:a)
16
+ }.to fail_with("expected #has_key?(:a) to return true, got false")
17
17
  end
18
18
 
19
19
  it 'does not include any args in the failure message if no args were given to the matcher' do
20
20
  o = Object.new
21
21
  def o.has_some_stuff?; false; end
22
22
  expect {
23
- o.should have_some_stuff
23
+ expect(o).to have_some_stuff
24
24
  }.to fail_with("expected #has_some_stuff? to return true, got false")
25
25
  end
26
26
 
@@ -28,7 +28,7 @@ describe "should have_sym(*args)" do
28
28
  o = Object.new
29
29
  def o.has_some_stuff?(*_); false; end
30
30
  expect {
31
- o.should have_some_stuff(:a, 7, "foo")
31
+ expect(o).to have_some_stuff(:a, 7, "foo")
32
32
  }.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return true, got false')
33
33
  end
34
34
 
@@ -37,15 +37,15 @@ describe "should have_sym(*args)" do
37
37
  def has_foo?
38
38
  end
39
39
  end
40
- lambda {
41
- klass.new.should have_foo
42
- }.should fail_with(/expected #has_foo.* to return true, got false/)
40
+ expect {
41
+ expect(klass.new).to have_foo
42
+ }.to fail_with(/expected #has_foo.* to return true, got false/)
43
43
  end
44
44
 
45
45
  it "fails if target does not respond to #has_sym?" do
46
- lambda {
47
- Object.new.should have_key(:a)
48
- }.should raise_error(NoMethodError)
46
+ expect {
47
+ expect(Object.new).to have_key(:a)
48
+ }.to raise_error(NoMethodError)
49
49
  end
50
50
 
51
51
  it "reraises an exception thrown in #has_sym?(*args)" do
@@ -53,13 +53,15 @@ describe "should have_sym(*args)" do
53
53
  def o.has_sym?(*args)
54
54
  raise "Funky exception"
55
55
  end
56
- lambda { o.should have_sym(:foo) }.should raise_error("Funky exception")
56
+ expect {
57
+ expect(o).to have_sym(:foo)
58
+ }.to raise_error("Funky exception")
57
59
  end
58
60
  end
59
61
 
60
- describe "should_not have_sym(*args)" do
62
+ describe "expect(...).not_to have_sym(*args)" do
61
63
  it "passes if #has_sym?(*args) returns false" do
62
- {:a => "A"}.should_not have_key(:b)
64
+ expect({:a => "A"}).not_to have_key(:b)
63
65
  end
64
66
 
65
67
  it "passes if #has_sym?(*args) returns nil" do
@@ -67,19 +69,19 @@ describe "should_not have_sym(*args)" do
67
69
  def has_foo?
68
70
  end
69
71
  end
70
- klass.new.should_not have_foo
72
+ expect(klass.new).not_to have_foo
71
73
  end
72
74
 
73
75
  it "fails if #has_sym?(*args) returns true" do
74
- lambda {
75
- {:a => "A"}.should_not have_key(:a)
76
- }.should fail_with("expected #has_key?(:a) to return false, got true")
76
+ expect {
77
+ expect({:a => "A"}).not_to have_key(:a)
78
+ }.to fail_with("expected #has_key?(:a) to return false, got true")
77
79
  end
78
80
 
79
81
  it "fails if target does not respond to #has_sym?" do
80
- lambda {
81
- Object.new.should have_key(:a)
82
- }.should raise_error(NoMethodError)
82
+ expect {
83
+ expect(Object.new).to have_key(:a)
84
+ }.to raise_error(NoMethodError)
83
85
  end
84
86
 
85
87
  it "reraises an exception thrown in #has_sym?(*args)" do
@@ -87,14 +89,16 @@ describe "should_not have_sym(*args)" do
87
89
  def o.has_sym?(*args)
88
90
  raise "Funky exception"
89
91
  end
90
- lambda { o.should_not have_sym(:foo) }.should raise_error("Funky exception")
92
+ expect {
93
+ expect(o).not_to have_sym(:foo)
94
+ }.to raise_error("Funky exception")
91
95
  end
92
96
 
93
97
  it 'does not include any args in the failure message if no args were given to the matcher' do
94
98
  o = Object.new
95
99
  def o.has_some_stuff?; true; end
96
100
  expect {
97
- o.should_not have_some_stuff
101
+ expect(o).not_to have_some_stuff
98
102
  }.to fail_with("expected #has_some_stuff? to return false, got true")
99
103
  end
100
104
 
@@ -102,7 +106,7 @@ describe "should_not have_sym(*args)" do
102
106
  o = Object.new
103
107
  def o.has_some_stuff?(*_); true; end
104
108
  expect {
105
- o.should_not have_some_stuff(:a, 7, "foo")
109
+ expect(o).not_to have_some_stuff(:a, 7, "foo")
106
110
  }.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return false, got true')
107
111
  end
108
112
  end
@@ -111,8 +115,8 @@ describe "has" do
111
115
  it "works when the target implements #send" do
112
116
  o = {:a => "A"}
113
117
  def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
114
- lambda {
115
- o.should have_key(:a)
116
- }.should_not raise_error
118
+ expect {
119
+ expect(o).to have_key(:a)
120
+ }.to_not raise_error
117
121
  end
118
122
  end
@@ -22,64 +22,64 @@ describe "have matcher" do
22
22
  owner
23
23
  end
24
24
 
25
- describe "should have(n).items" do
25
+ describe "expect(...).to have(n).items" do
26
26
  it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do
27
27
  let(:matcher) { have(2).items }
28
28
  end
29
29
 
30
30
  it "passes if target has a collection of items with n members" do
31
31
  owner = create_collection_owner_with(3)
32
- owner.should have(3).items_in_collection_with_length_method
33
- owner.should have(3).items_in_collection_with_size_method
34
- owner.should have(3).items_in_collection_with_count_method
32
+ expect(owner).to have(3).items_in_collection_with_length_method
33
+ expect(owner).to have(3).items_in_collection_with_size_method
34
+ expect(owner).to have(3).items_in_collection_with_count_method
35
35
  end
36
36
 
37
37
  it "converts :no to 0" do
38
38
  owner = create_collection_owner_with(0)
39
- owner.should have(:no).items_in_collection_with_length_method
40
- owner.should have(:no).items_in_collection_with_size_method
41
- owner.should have(:no).items_in_collection_with_count_method
39
+ expect(owner).to have(:no).items_in_collection_with_length_method
40
+ expect(owner).to have(:no).items_in_collection_with_size_method
41
+ expect(owner).to have(:no).items_in_collection_with_count_method
42
42
  end
43
43
 
44
44
  it "converts a String argument to Integer" do
45
45
  owner = create_collection_owner_with(3)
46
- owner.should have('3').items_in_collection_with_length_method
47
- owner.should have('3').items_in_collection_with_size_method
48
- owner.should have('3').items_in_collection_with_count_method
46
+ expect(owner).to have('3').items_in_collection_with_length_method
47
+ expect(owner).to have('3').items_in_collection_with_size_method
48
+ expect(owner).to have('3').items_in_collection_with_count_method
49
49
  end
50
50
 
51
51
  it "fails if target has a collection of items with < n members" do
52
52
  owner = create_collection_owner_with(3)
53
- lambda {
54
- owner.should have(4).items_in_collection_with_length_method
55
- }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
56
- lambda {
57
- owner.should have(4).items_in_collection_with_size_method
58
- }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
59
- lambda {
60
- owner.should have(4).items_in_collection_with_count_method
61
- }.should fail_with("expected 4 items_in_collection_with_count_method, got 3")
53
+ expect {
54
+ expect(owner).to have(4).items_in_collection_with_length_method
55
+ }.to fail_with("expected 4 items_in_collection_with_length_method, got 3")
56
+ expect {
57
+ expect(owner).to have(4).items_in_collection_with_size_method
58
+ }.to fail_with("expected 4 items_in_collection_with_size_method, got 3")
59
+ expect {
60
+ expect(owner).to have(4).items_in_collection_with_count_method
61
+ }.to fail_with("expected 4 items_in_collection_with_count_method, got 3")
62
62
  end
63
63
 
64
64
  it "fails if target has a collection of items with > n members" do
65
65
  owner = create_collection_owner_with(3)
66
- lambda {
67
- owner.should have(2).items_in_collection_with_length_method
68
- }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
69
- lambda {
70
- owner.should have(2).items_in_collection_with_size_method
71
- }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
72
- lambda {
73
- owner.should have(2).items_in_collection_with_count_method
74
- }.should fail_with("expected 2 items_in_collection_with_count_method, got 3")
66
+ expect {
67
+ expect(owner).to have(2).items_in_collection_with_length_method
68
+ }.to fail_with("expected 2 items_in_collection_with_length_method, got 3")
69
+ expect {
70
+ expect(owner).to have(2).items_in_collection_with_size_method
71
+ }.to fail_with("expected 2 items_in_collection_with_size_method, got 3")
72
+ expect {
73
+ expect(owner).to have(2).items_in_collection_with_count_method
74
+ }.to fail_with("expected 2 items_in_collection_with_count_method, got 3")
75
75
  end
76
76
  end
77
77
 
78
- describe 'should have(1).item when ActiveSupport::Inflector is defined' do
78
+ describe 'expect(...).to have(1).item when ActiveSupport::Inflector is defined' do
79
79
 
80
80
  it 'pluralizes the collection name' do
81
81
  owner = create_collection_owner_with(1)
82
- owner.should have(1).item
82
+ expect(owner).to have(1).item
83
83
  end
84
84
 
85
85
  context "when ActiveSupport::Inflector is partially loaded without its inflectors" do
@@ -87,134 +87,136 @@ describe "have matcher" do
87
87
  it "does not pluralize the collection name" do
88
88
  stub_const("ActiveSupport::Inflector", Module.new)
89
89
  owner = create_collection_owner_with(1)
90
- expect { owner.should have(1).item }.to raise_error(NoMethodError)
90
+ expect {
91
+ expect(owner).to have(1).item
92
+ }.to raise_error(NoMethodError)
91
93
  end
92
94
 
93
95
  end
94
96
  end
95
97
 
96
- describe 'should have(1).item when Inflector is defined' do
98
+ describe 'expect(...).to have(1).item when Inflector is defined' do
97
99
  before { stub_const("Inflector", inflector) }
98
100
 
99
101
  it 'pluralizes the collection name' do
100
102
  owner = create_collection_owner_with(1)
101
- owner.should have(1).item
103
+ expect(owner).to have(1).item
102
104
  end
103
105
  end
104
106
 
105
- describe "should have(n).items where result responds to items but returns something other than a collection" do
107
+ describe "expect(...).to have(n).items where result responds to items but returns something other than a collection" do
106
108
  it "provides a meaningful error" do
107
109
  owner = Class.new do
108
110
  def items
109
111
  Object.new
110
112
  end
111
113
  end.new
112
- lambda do
113
- owner.should have(3).items
114
- end.should raise_error("expected items to be a collection but it does not respond to #length, #size or #count")
114
+ expect do
115
+ expect(owner).to have(3).items
116
+ end.to raise_error("expected items to be a collection but it does not respond to #length, #size or #count")
115
117
  end
116
118
  end
117
119
 
118
- describe "should_not have(n).items" do
120
+ describe "expect(...).not_to have(n).items" do
119
121
 
120
122
  it "passes if target has a collection of items with < n members" do
121
123
  owner = create_collection_owner_with(3)
122
- owner.should_not have(4).items_in_collection_with_length_method
123
- owner.should_not have(4).items_in_collection_with_size_method
124
- owner.should_not have(4).items_in_collection_with_count_method
124
+ expect(owner).not_to have(4).items_in_collection_with_length_method
125
+ expect(owner).not_to have(4).items_in_collection_with_size_method
126
+ expect(owner).not_to have(4).items_in_collection_with_count_method
125
127
  end
126
128
 
127
129
  it "passes if target has a collection of items with > n members" do
128
130
  owner = create_collection_owner_with(3)
129
- owner.should_not have(2).items_in_collection_with_length_method
130
- owner.should_not have(2).items_in_collection_with_size_method
131
- owner.should_not have(2).items_in_collection_with_count_method
131
+ expect(owner).not_to have(2).items_in_collection_with_length_method
132
+ expect(owner).not_to have(2).items_in_collection_with_size_method
133
+ expect(owner).not_to have(2).items_in_collection_with_count_method
132
134
  end
133
135
 
134
136
  it "fails if target has a collection of items with n members" do
135
137
  owner = create_collection_owner_with(3)
136
- lambda {
137
- owner.should_not have(3).items_in_collection_with_length_method
138
- }.should fail_with("expected target not to have 3 items_in_collection_with_length_method, got 3")
139
- lambda {
140
- owner.should_not have(3).items_in_collection_with_size_method
141
- }.should fail_with("expected target not to have 3 items_in_collection_with_size_method, got 3")
142
- lambda {
143
- owner.should_not have(3).items_in_collection_with_count_method
144
- }.should fail_with("expected target not to have 3 items_in_collection_with_count_method, got 3")
138
+ expect {
139
+ expect(owner).not_to have(3).items_in_collection_with_length_method
140
+ }.to fail_with("expected target not to have 3 items_in_collection_with_length_method, got 3")
141
+ expect {
142
+ expect(owner).not_to have(3).items_in_collection_with_size_method
143
+ }.to fail_with("expected target not to have 3 items_in_collection_with_size_method, got 3")
144
+ expect {
145
+ expect(owner).not_to have(3).items_in_collection_with_count_method
146
+ }.to fail_with("expected target not to have 3 items_in_collection_with_count_method, got 3")
145
147
  end
146
148
  end
147
149
 
148
- describe "should have_exactly(n).items" do
150
+ describe "expect(...).to have_exactly(n).items" do
149
151
 
150
152
  it "passes if target has a collection of items with n members" do
151
153
  owner = create_collection_owner_with(3)
152
- owner.should have_exactly(3).items_in_collection_with_length_method
153
- owner.should have_exactly(3).items_in_collection_with_size_method
154
- owner.should have_exactly(3).items_in_collection_with_count_method
154
+ expect(owner).to have_exactly(3).items_in_collection_with_length_method
155
+ expect(owner).to have_exactly(3).items_in_collection_with_size_method
156
+ expect(owner).to have_exactly(3).items_in_collection_with_count_method
155
157
  end
156
158
 
157
159
  it "converts :no to 0" do
158
160
  owner = create_collection_owner_with(0)
159
- owner.should have_exactly(:no).items_in_collection_with_length_method
160
- owner.should have_exactly(:no).items_in_collection_with_size_method
161
- owner.should have_exactly(:no).items_in_collection_with_count_method
161
+ expect(owner).to have_exactly(:no).items_in_collection_with_length_method
162
+ expect(owner).to have_exactly(:no).items_in_collection_with_size_method
163
+ expect(owner).to have_exactly(:no).items_in_collection_with_count_method
162
164
  end
163
165
 
164
166
  it "fails if target has a collection of items with < n members" do
165
167
  owner = create_collection_owner_with(3)
166
- lambda {
167
- owner.should have_exactly(4).items_in_collection_with_length_method
168
- }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
169
- lambda {
170
- owner.should have_exactly(4).items_in_collection_with_size_method
171
- }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
172
- lambda {
173
- owner.should have_exactly(4).items_in_collection_with_count_method
174
- }.should fail_with("expected 4 items_in_collection_with_count_method, got 3")
168
+ expect {
169
+ expect(owner).to have_exactly(4).items_in_collection_with_length_method
170
+ }.to fail_with("expected 4 items_in_collection_with_length_method, got 3")
171
+ expect {
172
+ expect(owner).to have_exactly(4).items_in_collection_with_size_method
173
+ }.to fail_with("expected 4 items_in_collection_with_size_method, got 3")
174
+ expect {
175
+ expect(owner).to have_exactly(4).items_in_collection_with_count_method
176
+ }.to fail_with("expected 4 items_in_collection_with_count_method, got 3")
175
177
  end
176
178
 
177
179
  it "fails if target has a collection of items with > n members" do
178
180
  owner = create_collection_owner_with(3)
179
- lambda {
180
- owner.should have_exactly(2).items_in_collection_with_length_method
181
- }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
182
- lambda {
183
- owner.should have_exactly(2).items_in_collection_with_size_method
184
- }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
185
- lambda {
186
- owner.should have_exactly(2).items_in_collection_with_count_method
187
- }.should fail_with("expected 2 items_in_collection_with_count_method, got 3")
181
+ expect {
182
+ expect(owner).to have_exactly(2).items_in_collection_with_length_method
183
+ }.to fail_with("expected 2 items_in_collection_with_length_method, got 3")
184
+ expect {
185
+ expect(owner).to have_exactly(2).items_in_collection_with_size_method
186
+ }.to fail_with("expected 2 items_in_collection_with_size_method, got 3")
187
+ expect {
188
+ expect(owner).to have_exactly(2).items_in_collection_with_count_method
189
+ }.to fail_with("expected 2 items_in_collection_with_count_method, got 3")
188
190
  end
189
191
  end
190
192
 
191
- describe "should have_at_least(n).items" do
193
+ describe "expect(...).to have_at_least(n).items" do
192
194
 
193
195
  it "passes if target has a collection of items with n members" do
194
196
  owner = create_collection_owner_with(3)
195
- owner.should have_at_least(3).items_in_collection_with_length_method
196
- owner.should have_at_least(3).items_in_collection_with_size_method
197
- owner.should have_at_least(3).items_in_collection_with_count_method
197
+ expect(owner).to have_at_least(3).items_in_collection_with_length_method
198
+ expect(owner).to have_at_least(3).items_in_collection_with_size_method
199
+ expect(owner).to have_at_least(3).items_in_collection_with_count_method
198
200
  end
199
201
 
200
202
  it "passes if target has a collection of items with > n members" do
201
203
  owner = create_collection_owner_with(3)
202
- owner.should have_at_least(2).items_in_collection_with_length_method
203
- owner.should have_at_least(2).items_in_collection_with_size_method
204
- owner.should have_at_least(2).items_in_collection_with_count_method
204
+ expect(owner).to have_at_least(2).items_in_collection_with_length_method
205
+ expect(owner).to have_at_least(2).items_in_collection_with_size_method
206
+ expect(owner).to have_at_least(2).items_in_collection_with_count_method
205
207
  end
206
208
 
207
209
  it "fails if target has a collection of items with < n members" do
208
210
  owner = create_collection_owner_with(3)
209
- lambda {
210
- owner.should have_at_least(4).items_in_collection_with_length_method
211
- }.should fail_with("expected at least 4 items_in_collection_with_length_method, got 3")
212
- lambda {
213
- owner.should have_at_least(4).items_in_collection_with_size_method
214
- }.should fail_with("expected at least 4 items_in_collection_with_size_method, got 3")
215
- lambda {
216
- owner.should have_at_least(4).items_in_collection_with_count_method
217
- }.should fail_with("expected at least 4 items_in_collection_with_count_method, got 3")
211
+ expect {
212
+ expect(owner).to have_at_least(4).items_in_collection_with_length_method
213
+ }.to fail_with("expected at least 4 items_in_collection_with_length_method, got 3")
214
+ expect {
215
+ expect(owner).to have_at_least(4).items_in_collection_with_size_method
216
+ }.to fail_with("expected at least 4 items_in_collection_with_size_method, got 3")
217
+ expect {
218
+ expect(owner).to have_at_least(4).items_in_collection_with_count_method
219
+ }.to fail_with("expected at least 4 items_in_collection_with_count_method, got 3")
218
220
  end
219
221
 
220
222
  it "provides educational negative failure messages" do
@@ -230,57 +232,57 @@ describe "have matcher" do
230
232
  count_matcher.matches?(owner)
231
233
 
232
234
  #then
233
- length_matcher.failure_message_for_should_not.should eq <<-EOF
235
+ expect(length_matcher.failure_message_for_should_not).to eq <<-EOF
234
236
  Isn't life confusing enough?
235
237
  Instead of having to figure out the meaning of this:
236
- should_not have_at_least(3).items_in_collection_with_length_method
238
+ expect(actual).not_to have_at_least(3).items_in_collection_with_length_method
237
239
  We recommend that you use this instead:
238
- should have_at_most(2).items_in_collection_with_length_method
240
+ expect(actual).to have_at_most(2).items_in_collection_with_length_method
239
241
  EOF
240
242
 
241
- size_matcher.failure_message_for_should_not.should eq <<-EOF
243
+ expect(size_matcher.failure_message_for_should_not).to eq <<-EOF
242
244
  Isn't life confusing enough?
243
245
  Instead of having to figure out the meaning of this:
244
- should_not have_at_least(3).items_in_collection_with_size_method
246
+ expect(actual).not_to have_at_least(3).items_in_collection_with_size_method
245
247
  We recommend that you use this instead:
246
- should have_at_most(2).items_in_collection_with_size_method
248
+ expect(actual).to have_at_most(2).items_in_collection_with_size_method
247
249
  EOF
248
- count_matcher.failure_message_for_should_not.should eq <<-EOF
250
+ expect(count_matcher.failure_message_for_should_not).to eq <<-EOF
249
251
  Isn't life confusing enough?
250
252
  Instead of having to figure out the meaning of this:
251
- should_not have_at_least(3).items_in_collection_with_count_method
253
+ expect(actual).not_to have_at_least(3).items_in_collection_with_count_method
252
254
  We recommend that you use this instead:
253
- should have_at_most(2).items_in_collection_with_count_method
255
+ expect(actual).to have_at_most(2).items_in_collection_with_count_method
254
256
  EOF
255
257
  end
256
258
  end
257
259
 
258
- describe "should have_at_most(n).items" do
260
+ describe "expect(...).to have_at_most(n).items" do
259
261
  it "passes if target has a collection of items with n members" do
260
262
  owner = create_collection_owner_with(3)
261
- owner.should have_at_most(3).items_in_collection_with_length_method
262
- owner.should have_at_most(3).items_in_collection_with_size_method
263
- owner.should have_at_most(3).items_in_collection_with_count_method
263
+ expect(owner).to have_at_most(3).items_in_collection_with_length_method
264
+ expect(owner).to have_at_most(3).items_in_collection_with_size_method
265
+ expect(owner).to have_at_most(3).items_in_collection_with_count_method
264
266
  end
265
267
 
266
268
  it "fails if target has a collection of items with > n members" do
267
269
  owner = create_collection_owner_with(3)
268
- lambda {
269
- owner.should have_at_most(2).items_in_collection_with_length_method
270
- }.should fail_with("expected at most 2 items_in_collection_with_length_method, got 3")
271
- lambda {
272
- owner.should have_at_most(2).items_in_collection_with_size_method
273
- }.should fail_with("expected at most 2 items_in_collection_with_size_method, got 3")
274
- lambda {
275
- owner.should have_at_most(2).items_in_collection_with_count_method
276
- }.should fail_with("expected at most 2 items_in_collection_with_count_method, got 3")
270
+ expect {
271
+ expect(owner).to have_at_most(2).items_in_collection_with_length_method
272
+ }.to fail_with("expected at most 2 items_in_collection_with_length_method, got 3")
273
+ expect {
274
+ expect(owner).to have_at_most(2).items_in_collection_with_size_method
275
+ }.to fail_with("expected at most 2 items_in_collection_with_size_method, got 3")
276
+ expect {
277
+ expect(owner).to have_at_most(2).items_in_collection_with_count_method
278
+ }.to fail_with("expected at most 2 items_in_collection_with_count_method, got 3")
277
279
  end
278
280
 
279
281
  it "passes if target has a collection of items with < n members" do
280
282
  owner = create_collection_owner_with(3)
281
- owner.should have_at_most(4).items_in_collection_with_length_method
282
- owner.should have_at_most(4).items_in_collection_with_size_method
283
- owner.should have_at_most(4).items_in_collection_with_count_method
283
+ expect(owner).to have_at_most(4).items_in_collection_with_length_method
284
+ expect(owner).to have_at_most(4).items_in_collection_with_size_method
285
+ expect(owner).to have_at_most(4).items_in_collection_with_count_method
284
286
  end
285
287
 
286
288
  it "provides educational negative failure messages" do
@@ -296,28 +298,28 @@ EOF
296
298
  count_matcher.matches?(owner)
297
299
 
298
300
  #then
299
- length_matcher.failure_message_for_should_not.should eq <<-EOF
301
+ expect(length_matcher.failure_message_for_should_not).to eq <<-EOF
300
302
  Isn't life confusing enough?
301
303
  Instead of having to figure out the meaning of this:
302
- should_not have_at_most(3).items_in_collection_with_length_method
304
+ expect(actual).not_to have_at_most(3).items_in_collection_with_length_method
303
305
  We recommend that you use this instead:
304
- should have_at_least(4).items_in_collection_with_length_method
306
+ expect(actual).to have_at_least(4).items_in_collection_with_length_method
305
307
  EOF
306
308
 
307
- size_matcher.failure_message_for_should_not.should eq <<-EOF
309
+ expect(size_matcher.failure_message_for_should_not).to eq <<-EOF
308
310
  Isn't life confusing enough?
309
311
  Instead of having to figure out the meaning of this:
310
- should_not have_at_most(3).items_in_collection_with_size_method
312
+ expect(actual).not_to have_at_most(3).items_in_collection_with_size_method
311
313
  We recommend that you use this instead:
312
- should have_at_least(4).items_in_collection_with_size_method
314
+ expect(actual).to have_at_least(4).items_in_collection_with_size_method
313
315
  EOF
314
316
 
315
- count_matcher.failure_message_for_should_not.should eq <<-EOF
317
+ expect(count_matcher.failure_message_for_should_not).to eq <<-EOF
316
318
  Isn't life confusing enough?
317
319
  Instead of having to figure out the meaning of this:
318
- should_not have_at_most(3).items_in_collection_with_count_method
320
+ expect(actual).not_to have_at_most(3).items_in_collection_with_count_method
319
321
  We recommend that you use this instead:
320
- should have_at_least(4).items_in_collection_with_count_method
322
+ expect(actual).to have_at_least(4).items_in_collection_with_count_method
321
323
  EOF
322
324
  end
323
325
  end
@@ -326,40 +328,48 @@ EOF
326
328
  it "passes args to target" do
327
329
  target = mock("target")
328
330
  target.should_receive(:items).with("arg1","arg2").and_return([1,2,3])
329
- target.should have(3).items("arg1","arg2")
331
+ expect(target).to have(3).items("arg1","arg2")
330
332
  end
331
333
 
332
334
  it "passes block to target" do
333
335
  target = mock("target")
334
336
  block = lambda { 5 }
335
337
  target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
336
- target.should have(3).items("arg1","arg2", block)
338
+ expect(target).to have(3).items("arg1","arg2", block)
337
339
  end
338
340
  end
339
341
 
340
342
  describe "have(n).items where target IS a collection" do
341
343
  it "references the number of items IN the collection" do
342
- [1,2,3].should have(3).items
344
+ expect([1,2,3]).to have(3).items
343
345
  end
344
346
 
345
347
  it "fails when the number of items IN the collection is not as expected" do
346
- lambda { [1,2,3].should have(7).items }.should fail_with("expected 7 items, got 3")
348
+ expect {
349
+ expect([1,2,3]).to have(7).items
350
+ }.to fail_with("expected 7 items, got 3")
347
351
  end
348
352
  end
349
353
 
350
354
  describe "have(n).characters where target IS a String" do
351
355
  it "passes if the length is correct" do
352
- "this string".should have(11).characters
356
+ expect("this string").to have(11).characters
353
357
  end
354
358
 
355
359
  it "fails if the length is incorrect" do
356
- lambda { "this string".should have(12).characters }.should fail_with("expected 12 characters, got 11")
360
+ expect {
361
+ expect("this string").to have(12).characters
362
+ }.to fail_with("expected 12 characters, got 11")
357
363
  end
358
364
  end
359
365
 
360
366
  describe "have(n).things on an object which is not a collection nor contains one" do
361
367
  it "fails" do
362
- lambda { Object.new.should have(2).things }.should raise_error(NoMethodError) {|e| e.name.should eq :things }
368
+ expect {
369
+ expect(Object.new).to have(2).things
370
+ }.to raise_error(NoMethodError) { |e|
371
+ expect(e.name).to eq :things
372
+ }
363
373
  end
364
374
  end
365
375
 
@@ -367,35 +377,39 @@ EOF
367
377
  before(:each) do
368
378
  @collection = Object.new
369
379
  def @collection.floozles; [1,2] end
380
+ def @collection.send; :sent; end
370
381
  end
371
382
 
372
383
  it "works in the straightforward case" do
373
- lambda {
374
- @collection.should have(2).floozles
375
- }.should_not raise_error
384
+ expect(@collection).to have(2).floozles
376
385
  end
377
386
 
378
387
  it "works when doing automatic pluralization" do
379
- lambda {
380
- @collection.should have_at_least(1).floozle
381
- }.should_not raise_error
388
+ expect(@collection).to have_at_least(1).floozle
382
389
  end
383
390
 
384
391
  it "blows up when the owner doesn't respond to that method" do
385
- lambda {
386
- @collection.should have(99).problems
387
- }.should raise_error(NoMethodError, /problems/)
392
+ expect {
393
+ expect(@collection).to have(99).problems
394
+ }.to raise_error(NoMethodError, /problems/)
395
+ end
396
+
397
+ it 'works when #send is defined directly on an array' do
398
+ array = [1, 2]
399
+ def array.send; :sent; end
400
+
401
+ expect(array).to have(2).items
388
402
  end
389
403
  end
390
404
 
391
405
  describe RSpec::Matchers::BuiltIn::Have do
392
406
  it "has method_missing as private" do
393
- described_class.private_instance_methods.should include_method(:method_missing)
407
+ expect(described_class.private_instance_methods).to include_method(:method_missing)
394
408
  end
395
409
 
396
410
  it "does not respond_to? method_missing (because it's private)" do
397
411
  formatter = described_class.new(0, StringIO.new)
398
- formatter.should_not respond_to(:method_missing)
412
+ expect(formatter).not_to respond_to(:method_missing)
399
413
  end
400
414
 
401
415
  describe "respond_to?" do
@@ -406,25 +420,25 @@ EOF
406
420
  end
407
421
 
408
422
  it "is true for a method which Have defines" do
409
- @have.should respond_to(@a_method_which_have_defines)
423
+ expect(@have).to respond_to(@a_method_which_have_defines)
410
424
  end
411
425
 
412
426
  it "is true for a method that it's superclass (Object) defines" do
413
- @have.should respond_to(@a_method_which_object_defines)
427
+ expect(@have).to respond_to(@a_method_which_object_defines)
414
428
  end
415
429
 
416
430
  it "is false for a method which neither Object nor nor Have defines" do
417
- @have.should_not respond_to(:foo_bar_baz)
431
+ expect(@have).not_to respond_to(:foo_bar_baz)
418
432
  end
419
433
 
420
434
  it "is false if the owner doesn't respond to the method" do
421
435
  have = described_class.new(99)
422
- have.should_not respond_to(:problems)
436
+ expect(have).not_to respond_to(:problems)
423
437
  end
424
438
 
425
439
  it "is true if the owner responds to the method" do
426
440
  have = described_class.new(:a_symbol)
427
- have.should respond_to(:to_sym)
441
+ expect(have).to respond_to(:to_sym)
428
442
  end
429
443
  end
430
444
  end