View sentence_spec.rb
1 2 3 4 5 6 7 8 9 10
class Sentence
def test(sen)
first = sen[0]
last = sen.split('').last
rest = sen[1..-1]
word_arr = sen.split
word_arr_len = word_arr.length
 
unless first == first.upcase then
puts "the first letter is not in uppercase"
View stack_overflow_debugger.rb
1 2 3 4 5 6 7 8 9
max_stack_frames = 500
 
TooManyStackFrames = Class.new(StandardError)
 
TracePoint.new(:call) do |tp|
if caller.size >= max_stack_frames
raise TooManyStackFrames, "Stack has exceeded #{max_stack_frames} frames"
end
end.enable
View raise_error_spec.rb
1 2 3 4 5 6 7 8 9 10
RSpec::Matchers.define :an_exception_caused_by do |cause|
match do |exception|
cause === exception.cause
end
end
 
expect {
begin
"foo".gsub # requires 2 args
rescue ArgumentError
View a_string_including.rb
1 2 3 4 5
expect {
expect(the_dbl).to have_received(:expected_method).with(:one).once
}.to raise_error(Expectations::ExpectationNotMetError,
a_string_including("expected: 1 time",
"received: 2 times"))
View Gemfile
1 2 3 4
# A sample Gemfile
source "https://rubygems.org"
 
gem 'rspec'
View order_spec.rb
1 2 3 4 5 6 7 8 9 10
require_relative '../order.rb'
 
describe Order do
def new_order(input)
order = Order.new
allow(order).to receive(:input).and_return(input)
order
end
 
it 'gets dish number as input' do
View transaction_spec.rb
1 2 3 4 5 6 7 8 9 10
class SomeEntity
def run
transaction do
mark_non_eligible
make_invoice_batch
send_batch_email
end
end
 
def transaction
View why_expect_arg_vs_block.md

Haven't dived into RSpec but is it not possible for expect to accept either args or blocks, and process them internally?

First off, expect does accept either an arg or a block already. But if it's a block (or a proc/lambda arg), expect does not call the block automatically -- it just passes the block to the matcher and allows the matcher to call it if it wants. This is necessary because some matchers (the block matchers like raise_error, change, etc) must wrap the block in some extra logic to work properly because they deal in side effects, not expression return values.

View Gemfile-3-1
1 2 3 4
source "https://rubygems.org"
 
gem 'rspec-core', "~> 3.1.7"
gem 'allocation_stats'
Something went wrong with that request. Please try again.