Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Created November 27, 2012 07:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rewinfrey/4152978 to your computer and use it in GitHub Desktop.
Save rewinfrey/4152978 to your computer and use it in GitHub Desktop.
RSpec expectations
Index:
1. should() #basic expectation
2. should_not() #basic negation of an expectation (RSpec does not support using !=)
3. include(item) #called on an enumerable object, this returns true or false if the object is found in the enumerable collection
4. respond_to(:message) #determines if a particular message (as a symbol) is defined for an object
5. raise_error(type, message) #checks if a particular error was raised, accepts zero, one or two parameters
6. == #value equality
7. eql #self and other have the same values, and type
8. equal #self and other have the same object id
9. =~ #matches against a regular expression
10. match(/regex/) #matches against a regular expression
11. be_close(value, range of tolerance) #determines if value is within the acceptable range (useful for floating point calculations)
12. expect {
given condition
}.to change{then condition}.by(difference in change)
13. throw_symbol() # accepts zero, one or two arguments (like raise_error())
14. be_predicate() # equates predicate to a predicate method ex. empty? => be_empty()
15. be_a_predicate || be_an_predicate #equates to a predicate just as does rule 14
16. be_true() # return values should be evaluated as true or false (it doesn't mean the return value is a boolean)
17. be_false() # return values should be evalauted as true or false (it doesn't mean the return value is a boolean) only time Ruby evaluates to false is `false` and nil
18. have_key() # equates to a Hash.has_key?(:item) method (like be_predicate()) - works on any method beginning with has_
19. have().collection_item_to_check # used on collections, collection_item_to_check is the attribute of the collection to check size or length with, if this method isn't found
# RSpec ignores the collection_item_to_check and evalutates the length or size of the target object, assuming it has a length or size method defined on it
20. have() # used on Strings
21. collection precision
have_exactly(number)
have_at_least(number)
have_at_most(number)
22. operators
<
>
<=
>=
23. subject of an example is the object being described
24. should_receive(:method_name).with(arg).and_return(arg)
Examples:
1. a.should == b
2. a.should_not == b
3. prime_numbers.should_not include(8)
4. list.should respond_to(:length)
5. lambda { Object.new.explode! }.should raise_error(NameError)
5. account = Account.new 50, :dollars
expect {
account.withdraw 75, :dollars
}.to raise_error(InsufficientFundsError, /attempted to withdraw 75 dollars from an account with 50 dollars/) #this line and following 3 lines demonstrate all possible ways to invoke raise_error()
}.to raise_error(InsufficientFundsError)
}.to raise_error("attempted to withdraw 75 dollars from an account with 50 dollars")
}.to raise_error(/attempted to withdraw 75 dolalrs from an account with 50 dollars/)
6. a.should == b
7. a.should eql(b) #self and other have the same values, and type
8. a.should equal(b) #self and other have the same object id
9. result.should =~ /Total Due: \$37\.42/m
10. result.should match(/this expression/)
11. result.should be_close(5.25, 0.005) #result passes as long as the value is withint .005 of 5.25
12. expect {
seller.accept Offer.new(250_000)
}.to change{agent.commission}.by(7_500)
# useful for testing against database transactions
12. expect {
User.create!(role: "admin")
}.to change( User.admins.count ).by(1)
13. course = Course.new(seats: 20)
20.times { course.register Student.new }
lambda {
course.register Student.new
}.should throw_symbol(:course_full)
14. array.empty?.should == true
should be expressed as:
array.should be_empty
16. true.should be_true
0.should be_true
"this".should be_true
17. false.should be_false
nil.should be_false
18. request_parameters.has_key?(:id).should == true
# can be rewritten as:
request_paramaters.should have_key(:id)
19. collection.should have(37).items #items is syntactic sugar
20. "this string".should have(11).characters #characters is syntactic sugar for strings
21. day.should have_exactly(24).hours
dozen_bagles.should have_at_least(12).bagels
internet.should have_at_most(2037).awesome_social_networking_apps
22. result.should be < 7
result.should be > 7
result.should be <= 7
result.should be >= 7
23. describe Person do
subject { Person.new(birthdate: 19.years.ago) }
specify { subject.should be_eligible_to_vote }
OR
it { should be_eligible_to_vote }
OR
it "should be eligible to vote" do
subject.should be_eligible_to_vote
end
end
24. describe Person do
subject { Person.new }
specify { subject.should_receive(:birthday).with(19).and_return(19) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment