We can use an implicit subject, meaning we don't need to specify the subject (and we revert back to #it):
subject { Customer.new(:name => "Frank Reynolds") }
it { should be_valid }
We can also use an implicit subject based on the object we are describing:
describe Customer do
it { should_not be_valid }
end
ref: http://blog.firsthand.ca/2010/09/other-rspec-methods-subject-specify-and.html
Collection membership
actual.should include(expected)
actual.should start_with(expected)
actual.should end_with(expected)
Examples
[1,2,3].should include(1)
[1,2,3].should include(1, 2)
[1,2,3].should start_with(1)
[1,2,3].should start_with(1,2)
[1,2,3].should end_with(3)
[1,2,3].should end_with(2,3)
{:a => 'b'}.should include(:a => 'b')
"this string".should include("is str")
"this string".should start_with("this")
"this string".should end_with("ring")
actual.should be_an_instance_of(expected)
actual.should be_a_kind_of(expected)
ref: http://rubydoc.info/gems/rspec-expectations/frames
expect { ... }.to raise_error expect { ... }.to raise_error(ErrorClass) expect { ... }.to raise_error("message") expect { ... }.to raise_error(ErrorClass, "message")
expect { ... }.to throw_symbol expect { ... }.to throw_symbol(:symbol) expect { ... }.to throw_symbol(:symbol, 'value')
expect { |b| 5.tap(&b) }.to yield_control # passes regardless of yielded args
expect { |b| yield_if_true(true, &b) }.to yield_with_no_args # passes only if no args are yielded
expect { |b| 5.tap(&b) }.to yield_with_args(5) expect { |b| 5.tap(&b) }.to yield_with_args(Fixnum) expect { |b| "a string".tap(&b) }.to yield_with_args(/str/)
expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3) expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [:b, 2])
Via http://stufftohelpyouout.blogspot.com.au/2010/11/time-equivalence-or-closeness-in-rspec.html (Nov2010)
If you have two times in RSpec that you want to make sure are equal, you could use:
kid.sleepy_at.should eq(bedtime)
But, that only checks to make sure that the time to the nearest second is equal. To be more exact than a rounded second use:
kid.sleepy_at.to_f.should eq(bedtime.to_f)
If you want to ensure that a time is just close to the expected time, use be_close.
For example, if sleepy_at should be within a half second of bedtime use:
kid.sleepy_at.to_f.should be_close(bedtime.to_f, 0.5)
Converting time to float with to_f doesn't look pretty when it fails, but if you don't use it, and use:
kid.sleepy_at.should be_close(bedtime, 0.5)
then it is comparing seconds, and you might get an error like:
Failure/Error: kid.sleepy_at.should be_close(bedtime, 0.5) expected Thu Nov 04 20:50:21 UTC 2010 +/- (< 0.5), got 2010-11-04 20:50:22 UTC This is due to the second being rounded off. To make sure they are within a rounded second of each other, use:
kid.sleepy_at.should be_close(bedtime, 1)