Skip to content

Instantly share code, notes, and snippets.

@TomV
Created March 6, 2010 00:09
Show Gist options
  • Save TomV/323338 to your computer and use it in GitHub Desktop.
Save TomV/323338 to your computer and use it in GitHub Desktop.
# Basic Rspec question:
# How do I 'catch' that an Alert was sent to verify my test?
require 'spec'
class MyTask
def problem
a = Alert.new
a.send_alert
end
end
class Alert
def send_alert(message='problem with system')
puts "Warning Will Robinson!: #{message}"
end
end
describe 'MyTask' do
it 'should send an alert when there is a problem' do
task = MyTask.new
task.problem
Alert.should_receive(:new)
end
end
__END__
OUTPUT:
MyTask
Warning Will Robinson!: problem with system
- should send an alert when there is a problem (FAILED - 1)
1)
Spec::Mocks::MockExpectationError in 'MyTask should send an alert when there is a problem'
<Alert (class)> expected :new with (any args) once, but received it 0 times
./spec/test_alert.rb:20:
Finished in 0.07813 seconds
1 example, 1 failure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment