require 'spec_helper' module Wicoris::Postman describe FaxMachine do let(:job) { double('job') } let(:fax) { FaxMachine.new(job) } before do fax.stub(:system) end describe '#run' do let(:logger) { double('logger') } it 'faxes the document' do fax.stub(:command).and_return('chunky bacon') fax.should_receive(:system).with('chunky bacon') fax.run end context 'with noop' do let(:fax) { FaxMachine.new(job, :noop => true) } it 'does not fax the job' do fax.stub(:command).and_return('chunky bacon') fax.should_not_receive(:system) fax.run end end end describe '#command' do it 'should return the command-line for sending a fax' do fax.stub(:validated_phone).and_return('042') job.stub(:letter).and_return('/tmp/foo.pdf') expect(fax.send(:command)).to eq( "lp -d Fax -o phone=042 '/tmp/foo.pdf'") end end describe '#phone' do it 'adds the dial-out prefix' do job.stub(:phone).and_return('0123456789') expect(fax.validated_phone).to eq '00123456789' end it 'removes all whitespaces and dashes from the number' do job.stub(:phone).and_return("\t012-345 6789 \n") expect(fax.validated_phone).to eq '00123456789' end it 'raises when phone is missing' do job.stub(:phone).and_return(nil) expect{ fax.validated_phone }.to raise_error ArgumentError end it 'raises when one leading zero is missing' do job.stub(:phone).and_return('1234567890') expect{ fax.validated_phone }.to raise_error ArgumentError end it 'raises when begins with more then one zero' do job.stub(:phone).and_return('00123456789') expect{ fax.validated_phone }.to raise_error ArgumentError end it 'raises when shorter then 9 digits' do job.stub(:phone).and_return('01234567') expect{ fax.validated_phone }.to raise_error ArgumentError end it 'raises when number contains invalid chars' do job.stub(:phone).and_return('01234hello') expect{ fax.validated_phone }.to raise_error ArgumentError end it 'includes the phone number in the exception' do job.stub(:phone).and_return('42') expect{ fax.validated_phone }.to raise_error /42/ end end end end