spec/adhearsion/call_spec.rb in adhearsion-2.3.0 vs spec/adhearsion/call_spec.rb in adhearsion-2.3.1

- old
+ new

@@ -8,11 +8,11 @@ end end module Adhearsion describe Call do - let(:mock_client) { flexmock('Client').tap(&:should_ignore_missing) } + let(:mock_client) { mock('Client').as_null_object } let(:call_id) { rand } let(:headers) { nil } let(:to) { 'sip:you@there.com' } let(:from) { 'sip:me@here.com' } @@ -24,11 +24,11 @@ end subject { Adhearsion::Call.new offer } before do - flexmock(offer).should_receive(:client).and_return(mock_client) + offer.stub(:client).and_return(mock_client) end after do Adhearsion.active_calls.clear end @@ -95,20 +95,20 @@ lambda { Adhearsion::Call.new }.should_not raise_error end end it 'allows the registration of event handlers which are called when messages are delivered' do - event = flexmock 'Event' + event = mock 'Event' event.should_receive(:foo?).and_return true - response = flexmock 'Response' + response = mock 'Response' response.should_receive(:call).once subject.register_event_handler(:foo?) { response.call } subject << event end describe "event handlers" do - let(:response) { flexmock 'Response' } + let(:response) { mock 'Response' } describe "for joined events" do context "joined to another call" do let :event do Punchblock::Event::Joined.new :call_id => 'foobar' @@ -176,11 +176,12 @@ subject << event end it "should trigger any on_unjoined callbacks set for the matching call" do response.should_receive(:call).once.with(event) - call = flexmock Call.new, :id => 'foobar' + call = Call.new + call.stub :id => 'foobar' subject.on_unjoined(call) { |event| response.call event } subject << event end it "should not trigger on_unjoined callbacks for other call IDs" do @@ -225,11 +226,12 @@ subject << event end it "should not trigger any on_unjoined callbacks set for the matching call" do response.should_receive(:call).never - call = flexmock Call.new, :id => 'foobar' + call = Call.new + call.stub :id => 'foobar' subject.on_unjoined(call) { |event| response.call event } subject << event end end end @@ -269,12 +271,14 @@ end end context "peer registry" do let(:other_call_id) { 'foobar' } - let(:other_call) { flexmock Call.new, :id => other_call_id } + let(:other_call) { Call.new } + before { other_call.stub :id => other_call_id } + let :joined_event do Punchblock::Event::Joined.new :call_id => other_call_id end let :unjoined_event do @@ -327,11 +331,11 @@ subject << end_event subject.end_reason.should be == :hangup end it "should instruct the command registry to terminate" do - flexmock(subject.commands).should_receive(:terminate).once + subject.commands.should_receive(:terminate).once subject << end_event end it "removes itself from the active calls" do size_before = Adhearsion.active_calls.size @@ -399,22 +403,21 @@ subject.tagged_with?(:authorized).should be true end end describe "#write_command" do - let(:mock_command) { flexmock('Command') } + let(:mock_command) { mock('Command') } it "should asynchronously write the command to the Punchblock connection" do - mock_client = flexmock('Client') - flexmock(subject.wrapped_object).should_receive(:client).once.and_return mock_client + subject.wrapped_object.should_receive(:client).once.and_return mock_client mock_client.should_receive(:execute_command).once.with(mock_command, :call_id => subject.id, :async => true).and_return true subject.write_command mock_command end describe "with a hungup call" do before do - flexmock(subject.wrapped_object).should_receive(:active?).and_return(false) + subject.wrapped_object.should_receive(:active?).and_return(false) end it "should raise a Hangup exception" do lambda { subject.write_command mock_command }.should raise_error(Call::Hangup) end @@ -432,16 +435,16 @@ describe '#write_and_await_response' do let(:message) { Punchblock::Command::Accept.new } let(:response) { :foo } before do - flexmock(message).should_receive(:execute!).and_return true + message.should_receive(:execute!).and_return true message.response = response end it "writes a command to the call" do - flexmock(subject.wrapped_object).should_receive(:write_command).once.with(message) + subject.wrapped_object.should_receive(:write_command).once.with(message) subject.write_and_await_response message end it "adds the command to the registry" do subject.write_and_await_response message @@ -469,19 +472,19 @@ describe "with an error response" do let(:new_exception) { Punchblock::ProtocolError } let(:response) { new_exception.new } it "raises the error" do - flexmock(Events).should_receive(:trigger).never + Events.should_receive(:trigger).never lambda { subject.write_and_await_response message }.should raise_error new_exception end context "where the name is :item_not_found" do let(:response) { new_exception.new.setup :item_not_found } it "should raise a Hangup exception" do - flexmock(Events).should_receive(:trigger).never + Events.should_receive(:trigger).never lambda { subject.write_and_await_response message }.should raise_error Call::Hangup end end end @@ -497,14 +500,18 @@ end end end describe "basic control commands" do - include FlexMock::ArgumentTypes - - def expect_message_waiting_for_response(message) - flexmock(subject.wrapped_object).should_receive(:write_and_await_response).once.with(message).and_return(message) + def expect_message_waiting_for_response(message = nil, fail = false, &block) + expectation = subject.wrapped_object.should_receive(:write_and_await_response, &block).once + expectation = expectation.with message if message + if fail + expectation.and_raise fail + else + expectation.and_return message + end end describe '#accept' do describe "with no headers" do it 'should send an Accept message' do @@ -562,38 +569,42 @@ end end describe "with no headers" do it 'should send a Reject message' do - expect_message_waiting_for_response on { |c| c.is_a?(Punchblock::Command::Reject) && c.headers_hash == {} } + expect_message_waiting_for_response do |c| + c.is_a?(Punchblock::Command::Reject) && c.headers_hash == {} + end subject.reject end end describe "with headers set" do it 'should send a Hangup message with the correct headers' do headers = {:foo => 'bar'} - expect_message_waiting_for_response on { |c| c.is_a?(Punchblock::Command::Reject) && c.headers_hash == headers } + expect_message_waiting_for_response do |c| + c.is_a?(Punchblock::Command::Reject) && c.headers_hash == headers + end subject.reject nil, headers end end it "should immediately fire the :call_rejected event giving the call and the reason" do - expect_message_waiting_for_response Punchblock::Command::Reject - flexmock(Adhearsion::Events).should_receive(:trigger_immediately).once.with(:call_rejected, :call => subject, :reason => :decline) + expect_message_waiting_for_response kind_of(Punchblock::Command::Reject) + Adhearsion::Events.should_receive(:trigger_immediately).once.with(:call_rejected, :call => subject, :reason => :decline) subject.reject :decline end end describe "#hangup" do describe "if the call is not active" do before do - flexmock(subject.wrapped_object).should_receive(:active?).and_return false + subject.wrapped_object.should_receive(:active?).and_return false end it "should do nothing and return false" do - flexmock(subject).should_receive(:write_and_await_response).never + subject.should_receive(:write_and_await_response).never subject.hangup.should be false end end describe "if the call is active" do @@ -627,12 +638,14 @@ end end context "with a call" do let(:call_id) { rand.to_s } - let(:target) { flexmock Call.new, :id => call_id } + let(:target) { described_class.new } + before { target.stub id: call_id } + it "should send a join command joining to the provided call ID" do expect_join_with_options :call_id => call_id subject.join target end @@ -712,12 +725,14 @@ end end context "with a call" do let(:call_id) { rand.to_s } - let(:target) { flexmock Call.new, :id => call_id } + let(:target) { described_class.new } + before { target.stub id: call_id } + it "should send an unjoin command unjoining from the provided call ID" do expect_unjoin_with_options :call_id => call_id subject.unjoin target end end @@ -776,25 +791,25 @@ end end describe "#execute_controller" do let(:latch) { CountDownLatch.new 1 } - let(:mock_controller) { flexmock CallController.new(subject) } + let(:mock_controller) { CallController.new(subject) } before do - flexmock subject.wrapped_object, :write_and_await_response => true + subject.wrapped_object.stub :write_and_await_response => true end it "should call #bg_exec on the controller instance" do mock_controller.should_receive(:exec).once subject.execute_controller mock_controller, lambda { |call| latch.countdown! } latch.wait(3).should be_true end it "should use the passed block as a controller if none is specified" do mock_controller.should_receive(:exec).once - flexmock(CallController).should_receive(:new).once.and_return mock_controller + CallController.should_receive(:new).once.and_return mock_controller subject.execute_controller nil, lambda { |call| latch.countdown! } do foo end latch.wait(3).should be_true end @@ -814,11 +829,10 @@ latch.wait(3).should be true Adhearsion::Events.clear_handlers :exception end it "should execute a callback after the controller executes" do - flexmock(CallController).should_receive(:exec) foo = nil subject.execute_controller mock_controller, lambda { |call| foo = call; latch.countdown! } latch.wait(3).should be_true foo.should be subject end @@ -830,11 +844,11 @@ subject.controllers.should include :foo end end context "with two controllers registered" do - let(:controller1) { flexmock 'CallController1' } - let(:controller2) { flexmock 'CallController2' } + let(:controller1) { mock 'CallController1' } + let(:controller2) { mock 'CallController2' } before { subject.controllers << controller1 << controller2 } describe "#pause_controllers" do it "should pause each of the registered controllers" do