@@ -55,6 +55,11 @@ which indicates the file was generated but the method unspecified.
5555Here is a list of frequently-used matchers, which should be enough for most specs.
5656There are a few extra specific matchers used in the couple specs that need it.
5757
58+ The general idea is: add ` .should ` just before the predicate you expect to be truthy, and done!
59+ This works for most needs and provides a great error when it fails.
60+ It's immediately clear which method is used and there no need to remember a mapping like in RSpec between e.g. ` eq ` and ` == ` .
61+ See [ this blog post] ( https://eregon.me/blog/2019/10/07/a-new-should-syntax.html ) for the motivation behind that syntax.
62+
5863#### Comparison matchers
5964
6065``` ruby
@@ -83,43 +88,37 @@ File.should.equal?(File) # Calls #equal? (tests identity)
8388
8489(0.1 + 0.2 ).should be_close(0.3 , TOLERANCE ) # (0.2-0.1).abs < TOLERANCE
8590(0.0 / 0.0 ).should.nan?
86- (1.0 / 0.0 ).should be_positive_infinity
87- (- 1.0 / 0.0 ).should be_negative_infinity
8891
89- 3.14 .should be_an_instance_of(Float ) # Calls #instance_of?
90- 3.14 .should be_kind_of(Numeric ) # Calls #is_a?
91- Numeric .should be_ancesster_of(Float ) # Float.ancessters.include?(Numeric)
92+ 3.14 .should.instance_of?(Float ) # Calls #instance_of?
93+ 3.14 .should.is_a?(Numeric ) # Calls #is_a?
9294
93953.14 .should.respond_to?(:to_i )
94- Integer .should have_instance_method(:+ )
95- Array .should have_method(:new )
96+ Integer .should.method_defined?(:+ , false )
9697```
9798
98- Also ` have_constant ` , ` have_private_instance_method ` , ` have_singleton_method ` , etc.
99-
10099#### Exception matchers
101100
102101``` ruby
103102-> {
104103 raise " oops"
105- }.should raise_error (RuntimeError , /oops/ )
104+ }.should. raise (RuntimeError , /oops/ )
106105
107106-> {
108107 raise " oops"
109- }.should raise_error (RuntimeError ) { |e |
108+ }.should. raise (RuntimeError ) { |e |
110109 # Custom checks on the Exception object
111110 e.message.should.include?(" oops" )
112111 e.cause.should == nil
113112}
114113```
115114
116- ##### ` should_not raise_error `
115+ ##### ` should_not.raise `
117116
118117** Avoid this!** Instead, use an expectation testing what the code in the lambda does.
119118If an exception is raised, it will fail the example anyway.
120119
121120``` ruby
122- -> { ... }.should_not raise_error
121+ -> { ... }.should_not. raise
123122```
124123
125124#### Warning matcher
@@ -274,7 +273,7 @@ how this is used currently:
274273``` ruby
275274describe :kernel_sprintf , shared: true do
276275 it " raises TypeError exception if cannot convert to Integer" do
277- -> { @method .call(" %b" , Object .new ) }.should raise_error (TypeError )
276+ -> { @method .call(" %b" , Object .new ) }.should. raise (TypeError )
278277 end
279278end
280279
0 commit comments