1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
require 'spec_helper'
describe MailRoom::MailboxWatcher do
let(:mailbox) {build_mailbox}
describe '#running?' do
it 'is false by default' do
watcher = MailRoom::MailboxWatcher.new(mailbox)
expect(watcher.running?).to eq(false)
end
end
describe '#run' do
let(:imap) {stub(:login => true, :select => true)}
let(:watcher) {MailRoom::MailboxWatcher.new(mailbox)}
before :each do
Net::IMAP.stubs(:new).returns(imap) # prevent connection
end
it 'loops over wait while running' do
connection = MailRoom::IMAP::Connection.new(mailbox)
MailRoom::IMAP::Connection.stubs(:new).returns(connection)
watcher.expects(:running?).twice.returns(true, false)
connection.expects(:wait).once
connection.expects(:on_new_message).once
watcher.run
watcher.watching_thread.join # wait for finishing run
end
end
describe '#quit' do
let(:imap) {stub(:login => true, :select => true)}
let(:watcher) {MailRoom::MailboxWatcher.new(mailbox)}
before :each do
Net::IMAP.stubs(:new).returns(imap) # prevent connection
end
it 'closes and waits for the connection' do
connection = MailRoom::IMAP::Connection.new(mailbox)
connection.stubs(:wait)
connection.stubs(:quit)
MailRoom::IMAP::Connection.stubs(:new).returns(connection)
watcher.run
expect(watcher.running?).to eq(true)
connection.expects(:quit)
watcher.quit
expect(watcher.running?).to eq(false)
end
end
end
|