Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Modernize the test suite #45

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'adhearsion'
require 'flexmock'
require 'voicemail'

Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
Expand All @@ -8,7 +7,6 @@
config.color = true
config.tty = true

config.mock_framework = :flexmock
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
14 changes: 7 additions & 7 deletions spec/support/voicemail_controller_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module VoicemailControllerSpecHelper
def self.included(test_case)
test_case.let(:from) { "sip:[email protected]" }
test_case.let(:call) { flexmock 'Call', from: from }
test_case.let(:call) { Adhearsion::Call.new from: from, active?: true }
test_case.let(:config) { Voicemail::Plugin.config }
test_case.let(:greeting) { nil }
test_case.let(:mailbox) do
Expand All @@ -13,27 +13,27 @@ def self.included(test_case)
email_address: '[email protected]'
}
end
test_case.let(:storage_instance) { flexmock 'StorageInstance' }
test_case.let(:storage_instance) { double 'StorageInstance' }
test_case.let(:metadata) do
{ :mailbox => 100, :storage => storage_instance }
end

test_case.subject(:controller) { flexmock test_case.described_class.new(call, metadata) }
test_case.subject(:controller) { test_case.described_class.new(call, metadata) }

test_case.before(:each) do
storage_instance.should_receive(:get_mailbox).with(metadata[:mailbox]).and_return(mailbox)
expect(storage_instance).to receive(:get_mailbox).with(metadata[:mailbox]).and_return(mailbox)
end
end

def should_play(*args)
subject.should_receive(:play).once.tap { |exp| exp.with(*args) if args.count > 0 }
expect(subject).to receive(:play).once.tap { |exp| exp.with(*args) if args.count > 0 }
end

def should_ask(*args)
subject.should_receive(:ask).with(*args).once
expect(subject).to receive(:ask).with(*args).once
end

def should_invoke(*args)
subject.should_receive(:invoke).once.with(*args)
expect(subject).to receive(:invoke).once.with(*args)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
end

describe '#play_message' do
include FlexMock::ArgumentTypes
before do
['delete', 'replay', 'skip'].each do |prompt|
subject.should_receive(:t).with("voicemail.messages.menu.#{prompt}").and_return prompt
Expand Down
6 changes: 3 additions & 3 deletions spec/voicemail/call_controllers/voicemail_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
end

context 'with an existing mailbox' do
let(:ask_result) { flexmock 'Result', status: :noinput, response: nil }
let(:ask_result) { Adhearsion::CallController::Input::Result.new status: :noinput, utterance: nil }

def default_output_expectations
subject.should_receive(:t).with('voicemail.default_greeting').and_return 'Hiyas!'
Expand Down Expand Up @@ -77,7 +77,7 @@ def default_output_expectations

context 'when the greeting message is interrupted' do
context 'with the correct digit' do
let(:ask_result) { flexmock 'Result', status: :match, response: '#' }
let(:ask_result) { Adhearsion::CallController::Input::Result.new status: :match, utterance: '#' }
it 'should pass control to the AuthenticationController' do
subject.should_receive(:t).with('voicemail.default_greeting').and_return 'Hiyas!'
should_ask('Hiyas!', limit: 1).and_return ask_result
Expand All @@ -86,7 +86,7 @@ def default_output_expectations
end
end
context 'with an incorrect digit' do
let(:ask_result) { flexmock 'Result', status: :match, response: '1' }
let(:ask_result) { Adhearsion::CallController::Input::Result.new status: :match, utterance: '1' }
it 'executes the normal output, recording and hangup' do
default_output_expectations
subject.should_receive(:hangup).once
Expand Down
49 changes: 24 additions & 25 deletions spec/voicemail/intro_message_creator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'spec_helper'

describe Voicemail::IntroMessageCreator do
include FlexMock::ArgumentTypes

context 'included in a CallController' do
let(:config) { Voicemail::Plugin.config }
Expand All @@ -11,77 +10,77 @@
received: Time.local(2012, 5, 1, 9, 0, 0)
}
end
let(:call) { flexmock 'Call', from: 'sip:[email protected]' }
let(:call) { double Adhearsion::Call, from: 'sip:[email protected]' }
let(:metadata) { Hash.new }
let(:call_controller_class) do
class MyCallController < Adhearsion::CallController
include Voicemail::IntroMessageCreator
end
end

subject { flexmock call_controller_class.new call, metadata }
subject { call_controller_class.new call, metadata }

describe '#intro_message' do
context 'in :i18n_string mode' do
before do
config.numeric_method = :i18n_string
flexmock(I18n).should_receive('localize').with(message[:received]).and_return '9pm'
subject.should_receive(:t).with('voicemail.messages.message_received_on_x', received_on: '9pm').and_return 'Message received at 9pm'
expect(I18n).to receive('localize').with(message[:received]).and_return '9pm'
expect(subject).to receive(:t).with('voicemail.messages.message_received_on_x', received_on: '9pm').and_return 'Message received at 9pm'
end

it 'returns the translation' do
subject.should_receive(:t).with('voicemail.messages.message_received_from_x', from: message[:from]).and_return 'Message received from 1234'
subject.intro_message(message).should == ['Message received at 9pm', 'Message received from 1234']
expect(subject).to receive(:t).with('voicemail.messages.message_received_from_x', from: message[:from]).and_return 'Message received from 1234'
expect(subject.intro_message(message)).to eq ['Message received at 9pm', 'Message received from 1234']
end

it 'handles an unknown caller' do
unk_caller_msg = message.dup
unk_caller_msg[:from] = ''
subject.should_receive(:t).with('voicemail.unknown_caller').and_return 'an unknown caller'
subject.should_receive(:t).with('voicemail.messages.message_received_from_x', from: 'an unknown caller').and_return 'Message received from an unknown caller'
subject.intro_message(unk_caller_msg).should == ['Message received at 9pm', 'Message received from an unknown caller']
expect(subject).to receive(:t).with('voicemail.unknown_caller').and_return 'an unknown caller'
expect(subject).to receive(:t).with('voicemail.messages.message_received_from_x', from: 'an unknown caller').and_return 'Message received from an unknown caller'
expect(subject.intro_message(unk_caller_msg)).to eq ['Message received at 9pm', 'Message received from an unknown caller']
end

it 'handles stringified keys' do
message = { 'from' => '1234', 'received' => Time.local(2012, 5, 1, 9, 0, 0) }
subject.should_receive(:t).with('voicemail.messages.message_received_from_x', from: message['from']).and_return 'Message received from 1234'
subject.intro_message(message).should == ['Message received at 9pm', 'Message received from 1234']
expect(subject).to receive(:t).with('voicemail.messages.message_received_from_x', from: message['from']).and_return 'Message received from 1234'
expect(subject.intro_message(message)).to eq ['Message received at 9pm', 'Message received from 1234']
end
end

context 'in :ahn_say mode' do
let!(:ahn_config) { flexmock(Adhearsion.config, punchblock: OpenStruct.new, ahnsay: OpenStruct.new(sounds_dir: '/')) }

before do
Adhearsion.config.ahnsay.sounds_dir = '/'
config.numeric_method = :ahn_say
config.datetime_format = 'hmp'
end

it 'returns a nice array full of ahn_say sound files' do
subject.should_receive(:t).with('voicemail.messages.message_received_on').and_return 'Message received on '
subject.should_receive(:t).with('from').and_return ' from '
subject.intro_message(message).should == [
expect(subject).to receive(:t).with('voicemail.messages.message_received_on').and_return 'Message received on '
expect(subject).to receive(:t).with('from').and_return ' from '
expect(subject.intro_message(message)).to eq [
'Message received on ',
['/9.ul', '/oclock.ul', '/a-m.ul'],
['file:///9.ul', 'file:///oclock.ul', 'file:///a-m.ul'],
' from ',
['/1.ul', '/2.ul', '/3.ul', '/4.ul']
['file:///1.ul', 'file:///2.ul', 'file:///3.ul', 'file:///4.ul']
]
end
end

context 'in :play_numeric mode' do
let(:formatter) { flexmock Adhearsion::CallController::Output::Formatter }
let(:formatter) { double Adhearsion::CallController::Output::Formatter }
before do
flexmock Adhearsion::CallController::Output::Formatter, new: formatter
config.numeric_method = :play_numeric
end

it 'returns speech and ssml in an array' do
formatter.should_receive(:ssml_for_time).with(Time.local(2012, 5, 1, 9, 0, 0), any).and_return :time
formatter.should_receive(:ssml_for_characters).with('1234').and_return :number
subject.should_receive(:t).with('voicemail.messages.message_received_on').and_return 'Message received on '
subject.should_receive(:t).with('from').and_return ' from '
subject.intro_message(message).should == ['Message received on ', :time, ' from ', :number]
pending "Get rid of these mocks"
expect(formatter).to receive(:ssml_for_time).with(Time.local(2012, 5, 1, 9, 0, 0), any_args).and_return :time
expect(formatter).to receive(:ssml_for_characters).with('1234').and_return :number
expect(subject).to receive(:t).with('voicemail.messages.message_received_on').and_return 'Message received on '
expect(subject).to receive(:t).with('from').and_return ' from '
expect(subject.intro_message(message)).to eq ['Message received on ', :time, ' from ', :number]
end
end
end
Expand Down
27 changes: 13 additions & 14 deletions spec/voicemail/storage_pstore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module Voicemail
store[:saved][100] = [message_2, message_3]
store[:mailboxes][100] = mailbox
end
flexmock storage
end
end

Expand All @@ -48,52 +47,52 @@ module Voicemail
end

describe "#save_recording" do
let(:recording_object) { flexmock 'recording_object', uri: "file://somewav.wav" }
let(:recording_object) { double 'recording_object', uri: "file://somewav.wav" }

it "saves the recording" do
storage.save_recording(100, :new, "foo", recording_object)
storage.store.transaction do |store|
store[:new][100].last[:uri].should == "file://somewav.wav"
store[:new][100].last[:from].should == "foo"
store[:new][100].last[:id].should_not be_nil
expect(store[:new][100].last[:uri]).to eq "file://somewav.wav"
expect(store[:new][100].last[:from]).to eq "foo"
expect(store[:new][100].last[:id]).to_not be_nil
end
end
end

describe "#count_messages" do
it "returns the new message count" do
storage.count_messages(100, :new).should == 1
expect(storage.count_messages(100, :new)).to eq 1
end

it "returns the saved message count" do
storage.count_messages(100, :saved).should == 2
expect(storage.count_messages(100, :saved)).to eq 2
end
end

describe '#get_messages' do
it 'returns all new messages' do
storage.get_messages(100, :new).should == [{ id: :foo }]
expect(storage.get_messages(100, :new)).to eq [{ id: :foo }]
end

it 'returns all saved messages' do
storage.get_messages(100, :saved).should == [{ id: :bar }, { id: :biz }]
expect(storage.get_messages(100, :saved)).to eq [{ id: :bar }, { id: :biz }]
end
end

describe "#change_message_type" do
it "changes the message type to :saved" do
storage.change_message_type 100, :foo, :new, :saved
storage.store.transaction do |store|
store[:new][100].should == []
store[:saved][100].should == [message_2, message_3, message_1]
expect(store[:new][100]).to eq []
expect(store[:saved][100]).to eq [message_2, message_3, message_1]
end
end

it "changes the message type to :new" do
storage.change_message_type 100, :bar, :saved, :new
storage.store.transaction do |store|
store[:new][100].should == [message_1, message_2]
store[:saved][100].should == [message_3]
expect(store[:new][100]).to eq [message_1, message_2]
expect(store[:saved][100]).to eq [message_3]
end
end
end
Expand All @@ -108,7 +107,7 @@ module Voicemail
it "deletes the greeting message" do
storage.delete_greeting_from_mailbox 100
storage.store.transaction do |store|
store[:mailboxes][100][:greeting].should be_nil
expect(store[:mailboxes][100][:greeting]).to be_nil
end
end
end
Expand Down
1 change: 0 additions & 1 deletion voicemail.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Gem::Specification.new do |s|
s.add_development_dependency %q<yard>, ["~> 0.6.0"]
s.add_development_dependency %q<rake>, [">= 0"]
s.add_development_dependency %q<guard-rspec>
s.add_development_dependency %q<flexmock>
s.add_development_dependency %q<test-unit>
s.add_development_dependency %q<ahnsay>
end