spec/punchblock/translator/asterisk/call_spec.rb in punchblock-1.6.1 vs spec/punchblock/translator/asterisk/call_spec.rb in punchblock-1.7.0
- old
+ new
@@ -5,11 +5,11 @@
module Punchblock
module Translator
class Asterisk
describe Call do
let(:channel) { 'SIP/foo' }
- let(:translator) { stub_everything 'Translator::Asterisk' }
+ let(:translator) { stub('Translator::Asterisk').as_null_object }
let(:agi_env) do
{
:agi_request => 'async',
:agi_channel => 'SIP/1234-00000000',
:agi_language => 'en',
@@ -86,11 +86,11 @@
it 'sends an offer to the translator' do
expected_offer = Punchblock::Event::Offer.new :target_call_id => subject.id,
:to => '1000',
:from => 'Jane Smith <SIP/5678>',
:headers => sip_headers
- translator.expects(:handle_pb_event).with expected_offer
+ translator.should_receive(:handle_pb_event).with expected_offer
subject.send_offer
end
it 'should make the call identify as inbound' do
subject.send_offer
@@ -102,19 +102,19 @@
describe '#send_progress' do
context "with a call that is already answered" do
it 'should not send the EXEC Progress command' do
- subject.wrapped_object.expects(:'answered?').returns true
- subject.wrapped_object.expects(:send_agi_action).with("EXEC Progress").never
+ subject.wrapped_object.should_receive(:'answered?').and_return true
+ subject.wrapped_object.should_receive(:send_agi_action).with("EXEC Progress").never
subject.send_progress
end
end
context "with an unanswered call" do
before do
- subject.wrapped_object.expects(:'answered?').returns(false).at_least_once
+ subject.wrapped_object.should_receive(:'answered?').at_least(:once).and_return(false)
end
context "with a call that is outbound" do
let(:dial_command) { Command::Dial.new }
@@ -122,27 +122,27 @@
dial_command.request!
subject.dial dial_command
end
it 'should not send the EXEC Progress command' do
- subject.wrapped_object.expects(:send_agi_action).with("EXEC Progress").never
+ subject.wrapped_object.should_receive(:send_agi_action).with("EXEC Progress").never
subject.send_progress
end
end
context "with a call that is inbound" do
before do
subject.send_offer
end
it 'should send the EXEC Progress command to a call that is inbound and not answered' do
- subject.wrapped_object.expects(:send_agi_action).with("EXEC Progress")
+ subject.wrapped_object.should_receive(:send_agi_action).with("EXEC Progress")
subject.send_progress
end
it 'should send the EXEC Progress command only once if called twice' do
- subject.wrapped_object.expects(:send_agi_action).with("EXEC Progress").once
+ subject.wrapped_object.should_receive(:send_agi_action).with("EXEC Progress").once
subject.send_progress
subject.send_progress
end
end
end
@@ -168,11 +168,11 @@
:channel => 'SIP/1234',
:callerid => 'sip:foo@bar.com',
:variable => "punchblock_call_id=#{subject.id}"
}).tap { |a| a.request! }
- translator.expects(:execute_global_command!).once.with expected_action
+ translator.should_receive(:execute_global_command!).once.with expected_action
subject.dial dial_command
end
context 'with a name and channel in the to field' do
let(:to) { 'Jane Smith <SIP/5678>' }
@@ -186,11 +186,11 @@
:channel => 'SIP/5678',
:callerid => 'sip:foo@bar.com',
:variable => "punchblock_call_id=#{subject.id}"
}).tap { |a| a.request! }
- translator.expects(:execute_global_command!).once.with expected_action
+ translator.should_receive(:execute_global_command!).once.with expected_action
subject.dial dial_command
end
end
context 'with a timeout specified' do
@@ -208,11 +208,11 @@
:callerid => 'sip:foo@bar.com',
:variable => "punchblock_call_id=#{subject.id}",
:timeout => 10000
}).tap { |a| a.request! }
- translator.expects(:execute_global_command!).once.with expected_action
+ translator.should_receive(:execute_global_command!).once.with expected_action
subject.dial dial_command
end
end
context 'with headers specified' do
@@ -229,11 +229,11 @@
:channel => 'SIP/1234',
:callerid => 'sip:foo@bar.com',
:variable => "punchblock_call_id=#{subject.id},SIPADDHEADER51=\"X-foo: bar\",SIPADDHEADER52=\"X-doo: dah\""
}).tap { |a| a.request! }
- translator.expects(:execute_global_command!).once.with expected_action
+ translator.should_receive(:execute_global_command!).once.with expected_action
subject.dial dial_command
end
end
it 'sends the call ID as a response to the Dial' do
@@ -251,11 +251,11 @@
it 'causes accepting the call to be a null operation' do
subject.dial dial_command
accept_command = Command::Accept.new
accept_command.request!
- subject.wrapped_object.expects(:send_agi_action).never
+ subject.wrapped_object.should_receive(:send_agi_action).never
subject.execute_command accept_command
accept_command.response(0.5).should be true
end
end
@@ -274,19 +274,19 @@
let(:cause) { '16' }
let(:cause_txt) { 'Normal Clearing' }
it "should cause the actor to be terminated" do
- translator.expects(:handle_pb_event).twice
+ translator.should_receive(:handle_pb_event).twice
subject.process_ami_event ami_event
sleep 5.5
subject.should_not be_alive
end
it "de-registers the call from the translator" do
- translator.stubs :handle_pb_event
- translator.expects(:deregister_call).once.with(subject)
+ translator.stub :handle_pb_event
+ translator.should_receive(:deregister_call).once.with(subject)
subject.process_ami_event ami_event
end
it "should cause all components to send complete events before sending end event" do
comp_command = Punchblock::Component::Input.new :grammar => {:value => '<grammar/>'}, :mode => :dtmf
@@ -294,24 +294,24 @@
component = subject.execute_command comp_command
comp_command.response(0.1).should be_a Ref
expected_complete_event = Punchblock::Event::Complete.new :target_call_id => subject.id, :component_id => component.id
expected_complete_event.reason = Punchblock::Event::Complete::Hangup.new
expected_end_event = Punchblock::Event::End.new :reason => :hangup, :target_call_id => subject.id
- end_sequence = sequence 'end events'
- translator.expects(:handle_pb_event).with(expected_complete_event).once.in_sequence(end_sequence)
- translator.expects(:handle_pb_event).with(expected_end_event).once.in_sequence(end_sequence)
+
+ translator.should_receive(:handle_pb_event).with(expected_complete_event).once.ordered
+ translator.should_receive(:handle_pb_event).with(expected_end_event).once.ordered
subject.process_ami_event ami_event
end
context "with an undefined cause" do
let(:cause) { '0' }
let(:cause_txt) { 'Undefined' }
it 'should send an end (hangup) event to the translator' do
expected_end_event = Punchblock::Event::End.new :reason => :hangup,
:target_call_id => subject.id
- translator.expects(:handle_pb_event).with expected_end_event
+ translator.should_receive(:handle_pb_event).with expected_end_event
subject.process_ami_event ami_event
end
end
context "with a normal clearing cause" do
@@ -319,11 +319,11 @@
let(:cause_txt) { 'Normal Clearing' }
it 'should send an end (hangup) event to the translator' do
expected_end_event = Punchblock::Event::End.new :reason => :hangup,
:target_call_id => subject.id
- translator.expects(:handle_pb_event).with expected_end_event
+ translator.should_receive(:handle_pb_event).with expected_end_event
subject.process_ami_event ami_event
end
end
context "with a user busy cause" do
@@ -331,11 +331,11 @@
let(:cause_txt) { 'User Busy' }
it 'should send an end (busy) event to the translator' do
expected_end_event = Punchblock::Event::End.new :reason => :busy,
:target_call_id => subject.id
- translator.expects(:handle_pb_event).with expected_end_event
+ translator.should_receive(:handle_pb_event).with expected_end_event
subject.process_ami_event ami_event
end
end
{
@@ -347,11 +347,11 @@
let(:cause_txt) { cause_txt }
it 'should send an end (timeout) event to the translator' do
expected_end_event = Punchblock::Event::End.new :reason => :timeout,
:target_call_id => subject.id
- translator.expects(:handle_pb_event).with expected_end_event
+ translator.should_receive(:handle_pb_event).with expected_end_event
subject.process_ami_event ami_event
end
end
end
@@ -365,11 +365,11 @@
let(:cause_txt) { cause_txt }
it 'should send an end (reject) event to the translator' do
expected_end_event = Punchblock::Event::End.new :reason => :reject,
:target_call_id => subject.id
- translator.expects(:handle_pb_event).with expected_end_event
+ translator.should_receive(:handle_pb_event).with expected_end_event
subject.process_ami_event ami_event
end
end
end
@@ -417,11 +417,11 @@
let(:cause_txt) { cause_txt }
it 'should send an end (error) event to the translator' do
expected_end_event = Punchblock::Event::End.new :reason => :error,
:target_call_id => subject.id
- translator.expects(:handle_pb_event).with expected_end_event
+ translator.should_receive(:handle_pb_event).with expected_end_event
subject.process_ami_event ami_event
end
end
end
end
@@ -445,11 +445,11 @@
before do
subject.register_component component
end
it 'should send the event to the component' do
- component.expects(:handle_ami_event).once.with ami_event
+ component.should_receive(:handle_ami_event).once.with ami_event
subject.process_ami_event ami_event
end
end
context 'with a Newstate event' do
@@ -472,11 +472,11 @@
let(:channel_state_desc) { 'Ringing' }
it 'should send a ringing event' do
expected_ringing = Punchblock::Event::Ringing.new
expected_ringing.target_call_id = subject.id
- translator.expects(:handle_pb_event).with expected_ringing
+ translator.should_receive(:handle_pb_event).with expected_ringing
subject.process_ami_event ami_event
end
it '#answered? should return false' do
subject.process_ami_event ami_event
@@ -489,11 +489,11 @@
let(:channel_state_desc) { 'Up' }
it 'should send a ringing event' do
expected_answered = Punchblock::Event::Answered.new
expected_answered.target_call_id = subject.id
- translator.expects(:handle_pb_event).with expected_answered
+ translator.should_receive(:handle_pb_event).with expected_answered
subject.process_ami_event ami_event
end
it '#answered? should be true' do
subject.process_ami_event ami_event
@@ -521,21 +521,21 @@
context 'sucessful' do
let(:response) { 'Success' }
let(:uniqueid) { '<null>' }
it 'should not send an end event' do
- translator.expects(:handle_pb_event).once.with is_a(Punchblock::Event::Asterisk::AMI::Event)
+ translator.should_receive(:handle_pb_event).once.with an_instance_of(Punchblock::Event::Asterisk::AMI::Event)
subject.process_ami_event ami_event
end
end
context 'failed after being connected' do
let(:response) { 'Failure' }
let(:uniqueid) { '1235' }
it 'should not send an end event' do
- translator.expects(:handle_pb_event).once.with is_a(Punchblock::Event::Asterisk::AMI::Event)
+ translator.should_receive(:handle_pb_event).once.with an_instance_of(Punchblock::Event::Asterisk::AMI::Event)
subject.process_ami_event ami_event
end
end
context 'failed without ever having connected' do
@@ -543,11 +543,11 @@
let(:uniqueid) { '<null>' }
it 'should send an error end event' do
expected_end_event = Punchblock::Event::End.new :reason => :error,
:target_call_id => subject.id
- translator.expects(:handle_pb_event).with expected_end_event
+ translator.should_receive(:handle_pb_event).with expected_end_event
subject.process_ami_event ami_event
end
end
end
@@ -563,11 +563,11 @@
end
let(:response) { mock 'Response' }
it 'should execute the handler' do
- response.expects(:call).once.with ami_event
+ response.should_receive(:call).once.with ami_event
subject.register_handler :ami, :name => 'DTMF' do |event|
response.call event
end
subject.process_ami_event ami_event
end
@@ -635,12 +635,12 @@
end
end
before do
translator.register_call other_call
- translator.expects(:call_for_channel).with(other_channel).returns(other_call)
- other_call.expects(:id).returns other_call_id
+ translator.should_receive(:call_for_channel).with(other_channel).and_return(other_call)
+ other_call.should_receive(:id).and_return other_call_id
end
context "of state 'Link'" do
let(:state) { 'Link' }
@@ -650,16 +650,16 @@
joined.call_id = other_call_id
end
end
it 'sends the Joined event when the call is the first channel' do
- translator.expects(:handle_pb_event).with expected_joined
+ translator.should_receive(:handle_pb_event).with expected_joined
subject.process_ami_event ami_event
end
it 'sends the Joined event when the call is the second channel' do
- translator.expects(:handle_pb_event).with expected_joined
+ translator.should_receive(:handle_pb_event).with expected_joined
subject.process_ami_event switched_ami_event
end
end
context "of state 'Unlink'" do
@@ -671,16 +671,16 @@
joined.call_id = other_call_id
end
end
it 'sends the Unjoined event when the call is the first channel' do
- translator.expects(:handle_pb_event).with expected_unjoined
+ translator.should_receive(:handle_pb_event).with expected_unjoined
subject.process_ami_event ami_event
end
it 'sends the Unjoined event when the call is the second channel' do
- translator.expects(:handle_pb_event).with expected_unjoined
+ translator.should_receive(:handle_pb_event).with expected_unjoined
subject.process_ami_event switched_ami_event
end
end
end
@@ -715,28 +715,28 @@
end
end
before do
translator.register_call other_call
- translator.expects(:call_for_channel).with(other_channel).returns(other_call)
- other_call.expects(:id).returns other_call_id
+ translator.should_receive(:call_for_channel).with(other_channel).and_return(other_call)
+ other_call.should_receive(:id).and_return other_call_id
end
let :expected_unjoined do
Punchblock::Event::Unjoined.new.tap do |joined|
joined.target_call_id = subject.id
joined.call_id = other_call_id
end
end
it 'sends the Unjoined event when the call is the first channel' do
- translator.expects(:handle_pb_event).with expected_unjoined
+ translator.should_receive(:handle_pb_event).with expected_unjoined
subject.process_ami_event ami_event
end
it 'sends the Unjoined event when the call is the second channel' do
- translator.expects(:handle_pb_event).with expected_unjoined
+ translator.should_receive(:handle_pb_event).with expected_unjoined
subject.process_ami_event switched_ami_event
end
end
let :ami_event do
@@ -760,11 +760,11 @@
:'cause-txt' => "Unknown"},
:target_call_id => subject.id
end
it 'sends the AMI event to the connection as a PB event' do
- translator.expects(:handle_pb_event).with expected_pb_event
+ translator.should_receive(:handle_pb_event).with expected_pb_event
subject.process_ami_event ami_event
end
end
@@ -860,13 +860,13 @@
end
let(:mock_action) { mock 'Component::Asterisk::AGI::Command', :id => 'foo' }
it 'should create an AGI command component actor and execute it asynchronously' do
- mock_action.expects(:internal=).never
- Component::Asterisk::AGICommand.expects(:new_link).once.with(command, subject).returns mock_action
- mock_action.expects(:execute!).once
+ mock_action.should_receive(:internal=).never
+ Component::Asterisk::AGICommand.should_receive(:new_link).once.with(command, subject).and_return mock_action
+ mock_action.should_receive(:execute!).once
subject.execute_command command
end
end
context 'with an Output component' do
@@ -875,13 +875,13 @@
end
let(:mock_action) { mock 'Component::Asterisk::Output', :id => 'foo' }
it 'should create an Output component and execute it asynchronously' do
- Component::Output.expects(:new_link).once.with(command, subject).returns mock_action
- mock_action.expects(:internal=).never
- mock_action.expects(:execute!).once
+ Component::Output.should_receive(:new_link).once.with(command, subject).and_return mock_action
+ mock_action.should_receive(:internal=).never
+ mock_action.should_receive(:execute!).once
subject.execute_command command
end
end
context 'with an Input component' do
@@ -890,13 +890,13 @@
end
let(:mock_action) { mock 'Component::Asterisk::Input', :id => 'foo' }
it 'should create an Input component and execute it asynchronously' do
- Component::Input.expects(:new_link).once.with(command, subject).returns mock_action
- mock_action.expects(:internal=).never
- mock_action.expects(:execute!).once
+ Component::Input.should_receive(:new_link).once.with(command, subject).and_return mock_action
+ mock_action.should_receive(:internal=).never
+ mock_action.should_receive(:execute!).once
subject.execute_command command
end
end
context 'with a Record component' do
@@ -905,13 +905,13 @@
end
let(:mock_action) { mock 'Component::Asterisk::Record', :id => 'foo' }
it 'should create a Record component and execute it asynchronously' do
- Component::Record.expects(:new_link).once.with(command, subject).returns mock_action
- mock_action.expects(:internal=).never
- mock_action.expects(:execute!).once
+ Component::Record.should_receive(:new_link).once.with(command, subject).and_return mock_action
+ mock_action.should_receive(:internal=).never
+ mock_action.should_receive(:execute!).once
subject.execute_command command
end
end
context 'with a component command' do
@@ -927,11 +927,11 @@
context "for a known component ID" do
before { subject.register_component mock_component }
it 'should send the command to the component for execution' do
- mock_component.expects(:execute_command).once
+ mock_component.should_receive(:execute_command).once
subject.execute_command command
end
end
context "for a component which began executing but crashed" do
@@ -961,11 +961,11 @@
component.wrapped_object.define_singleton_method(:oops) do
raise 'Woops, I died'
end
- translator.expects(:handle_pb_event).once.with expected_event
+ translator.should_receive(:handle_pb_event).once.with expected_event
lambda { component.oops }.should raise_error(/Woops, I died/)
sleep 0.1
component.should_not be_alive
subject.component_with_id(comp_id).should be_nil
@@ -996,30 +996,30 @@
end
context "with a join command" do
let(:other_call_id) { "abc123" }
let(:other_channel) { 'SIP/bar' }
- let(:other_translator) { stub_everything 'Translator::Asterisk' }
+ let(:other_translator) { stub('Translator::Asterisk').as_null_object }
let :other_call do
Call.new other_channel, other_translator
end
let :command do
Punchblock::Command::Join.new :call_id => other_call_id
end
it "executes the proper dialplan Bridge application" do
- translator.expects(:call_with_id).with(other_call_id).returns(other_call)
+ translator.should_receive(:call_with_id).with(other_call_id).and_return(other_call)
subject.execute_command command
agi_command = subject.wrapped_object.instance_variable_get(:'@current_agi_command')
agi_command.name.should be == "EXEC Bridge"
agi_command.params_array.should be == [other_channel]
end
it "adds the join to the @pending_joins hash" do
- translator.expects(:call_with_id).with(other_call_id).returns(other_call)
+ translator.should_receive(:call_with_id).with(other_call_id).and_return(other_call)
subject.execute_command command
subject.pending_joins[other_channel].should be command
end
end
@@ -1034,11 +1034,11 @@
let :command do
Punchblock::Command::Unjoin.new :call_id => other_call_id
end
it "executes the unjoin through redirection" do
- translator.expects(:call_with_id).with(other_call_id).returns(nil)
+ translator.should_receive(:call_with_id).with(other_call_id).and_return(nil)
subject.execute_command command
ami_action = subject.wrapped_object.instance_variable_get(:'@current_ami_action')
ami_action.name.should be == "redirect"
ami_action.headers['Channel'].should be == channel
ami_action.headers['Exten'].should be == Punchblock::Translator::Asterisk::REDIRECT_EXTENSION
@@ -1048,11 +1048,11 @@
ami_action << RubyAMI::Response.new
command.response(1).should be_true
end
it "executes the unjoin through redirection, on the subject call and the other call" do
- translator.expects(:call_with_id).with(other_call_id).returns(other_call)
+ translator.should_receive(:call_with_id).with(other_call_id).and_return(other_call)
subject.execute_command command
ami_action = subject.wrapped_object.instance_variable_get(:'@current_ami_action')
ami_action.name.should be == "redirect"
ami_action.headers['Channel'].should be == channel
ami_action.headers['Exten'].should be == Punchblock::Translator::Asterisk::REDIRECT_EXTENSION
@@ -1064,11 +1064,11 @@
ami_action.headers['ExtraPriority'].should be == Punchblock::Translator::Asterisk::REDIRECT_PRIORITY
ami_action.headers['ExtraContext'].should be == Punchblock::Translator::Asterisk::REDIRECT_CONTEXT
end
it "handles redirect errors" do
- translator.expects(:call_with_id).with(other_call_id).returns(nil)
+ translator.should_receive(:call_with_id).with(other_call_id).and_return(nil)
subject.execute_command command
ami_action = subject.wrapped_object.instance_variable_get(:'@current_ami_action')
ami_action.name.should be == "redirect"
ami_action.headers['Channel'].should be == channel
ami_action.headers['Exten'].should be == Punchblock::Translator::Asterisk::REDIRECT_EXTENSION
@@ -1084,21 +1084,21 @@
end#execute_command
describe '#send_agi_action' do
it 'should send an appropriate AsyncAGI AMI action' do
pending
- subject.wrapped_object.expects(:send_ami_action).once.with('AGI', 'Command' => 'FOO', 'Channel' => subject.channel)
+ subject.wrapped_object.should_receive(:send_ami_action).once.with('AGI', 'Command' => 'FOO', 'Channel' => subject.channel)
subject.send_agi_action 'FOO'
end
end
describe '#send_ami_action' do
let(:component_id) { Punchblock.new_uuid }
before { stub_uuids component_id }
it 'should send the action to the AMI client' do
action = RubyAMI::Action.new 'foo', :foo => :bar
- translator.expects(:send_ami_action).once.with action
+ translator.should_receive(:send_ami_action).once.with action
subject.send_ami_action 'foo', :foo => :bar
end
end
describe '#redirect_back' do