How to use initialize method of Mock Package

Best Active_mocker_ruby code snippet using Mock.initialize

initialize_spec.rb

Source:initialize_spec.rb Github

copy

Full Screen

1require_relative '../../../spec_helper'2require_relative 'spec_helper'3describe "Net::FTP#initialize" do4 before :each do5 @ftp = Net::FTP.allocate6 @ftp.stub!(:connect)7 @port_args = []8 ruby_version_is "2.5" do9 @port_args << 2110 end11 end12 it "is private" do13 Net::FTP.should have_private_instance_method(:initialize)14 end15 it "sets self into binary mode" do16 @ftp.binary.should be_nil17 @ftp.send(:initialize)18 @ftp.binary.should be_true19 end20 it "sets self into active mode" do21 @ftp.passive.should be_nil22 @ftp.send(:initialize)23 @ftp.passive.should be_false24 end25 it "sets self into non-debug mode" do26 @ftp.debug_mode.should be_nil27 @ftp.send(:initialize)28 @ftp.debug_mode.should be_false29 end30 it "sets self to not resume file uploads/downloads" do31 @ftp.resume.should be_nil32 @ftp.send(:initialize)33 @ftp.resume.should be_false34 end35 describe "when passed no arguments" do36 it "does not try to connect" do37 @ftp.should_not_receive(:connect)38 @ftp.send(:initialize)39 end40 end41 describe "when passed host" do42 it "tries to connect to the passed host" do43 @ftp.should_receive(:connect).with("localhost", *@port_args)44 @ftp.send(:initialize, "localhost")45 end46 end47 describe "when passed host, user" do48 it "tries to connect to the passed host" do49 @ftp.should_receive(:connect).with("localhost", *@port_args)50 @ftp.send(:initialize, "localhost")51 end52 it "tries to login with the passed username" do53 @ftp.should_receive(:login).with("rubyspec", nil, nil)54 @ftp.send(:initialize, "localhost", "rubyspec")55 end56 end57 describe "when passed host, user, password" do58 it "tries to connect to the passed host" do59 @ftp.should_receive(:connect).with("localhost", *@port_args)60 @ftp.send(:initialize, "localhost")61 end62 it "tries to login with the passed username and password" do63 @ftp.should_receive(:login).with("rubyspec", "rocks", nil)64 @ftp.send(:initialize, "localhost", "rubyspec", "rocks")65 end66 end67 describe "when passed host, user" do68 it "tries to connect to the passed host" do69 @ftp.should_receive(:connect).with("localhost", *@port_args)70 @ftp.send(:initialize, "localhost")71 end72 it "tries to login with the passed username, password and account" do73 @ftp.should_receive(:login).with("rubyspec", "rocks", "account")74 @ftp.send(:initialize, "localhost", "rubyspec", "rocks", "account")75 end76 end77 ruby_version_is '2.4' do78 before :each do79 @ftp.stub!(:login)80 end81 describe 'when the host' do82 describe 'is set' do83 describe 'and port option' do84 describe 'is set' do85 it 'tries to connect to the host on the specified port' do86 options = mock('ftp initialize options')87 options.should_receive(:to_hash).and_return({ port: 8080 })88 @ftp.should_receive(:connect).with('localhost', 8080)89 @ftp.send(:initialize, 'localhost', options)90 end91 end92 describe 'is not set' do93 it 'tries to connect to the host without a port' do94 @ftp.should_receive(:connect).with("localhost", *@port_args)95 @ftp.send(:initialize, 'localhost')96 end97 end98 end99 describe 'when the username option' do100 describe 'is set' do101 describe 'and the password option' do102 describe 'is set' do103 describe 'and the account option' do104 describe 'is set' do105 it 'tries to log in with the supplied parameters' do106 options = mock('ftp initialize options')107 options.should_receive(:to_hash).and_return({ username: 'a', password: 'topsecret', account: 'b' })108 @ftp.should_receive(:login).with('a', 'topsecret', 'b')109 @ftp.send(:initialize, 'localhost', options)110 end111 end112 describe 'is unset' do113 it 'tries to log in with the supplied parameters' do114 options = mock('ftp initialize options')115 options.should_receive(:to_hash).and_return({ username: 'a', password: 'topsecret' })116 @ftp.should_receive(:login).with('a', 'topsecret', nil)117 @ftp.send(:initialize, 'localhost', options)118 end119 end120 end121 end122 describe 'is unset' do123 describe 'and the account option' do124 describe 'is set' do125 it 'tries to log in with the supplied parameters' do126 options = mock('ftp initialize options')127 options.should_receive(:to_hash).and_return({ username: 'a', account: 'b' })128 @ftp.should_receive(:login).with('a', nil, 'b')129 @ftp.send(:initialize, 'localhost', options)130 end131 end132 describe 'is unset' do133 it 'tries to log in with the supplied parameters' do134 options = mock('ftp initialize options')135 options.should_receive(:to_hash).and_return({ username: 'a'})136 @ftp.should_receive(:login).with('a', nil, nil)137 @ftp.send(:initialize, 'localhost', options)138 end139 end140 end141 end142 end143 end144 describe 'is not set' do145 it 'does not try to log in' do146 options = mock('ftp initialize options')147 options.should_receive(:to_hash).and_return({})148 @ftp.should_not_receive(:login)149 @ftp.send(:initialize, 'localhost', options)150 end151 end152 end153 end154 describe 'is unset' do155 it 'does not try to connect' do156 @ftp.should_not_receive(:connect)157 @ftp.send(:initialize)158 end159 it 'does not try to log in' do160 @ftp.should_not_receive(:login)161 @ftp.send(:initialize)162 end163 end164 end165 describe 'when the passive option' do166 describe 'is set' do167 describe 'to true' do168 it 'sets passive to true' do169 options = mock('ftp initialize options')170 options.should_receive(:to_hash).and_return({ passive: true })171 @ftp.send(:initialize, nil, options)172 @ftp.passive.should == true173 end174 end175 describe 'to false' do176 it 'sets passive to false' do177 options = mock('ftp initialize options')178 options.should_receive(:to_hash).and_return({ passive: false })179 @ftp.send(:initialize, nil, options)180 @ftp.passive.should == false181 end182 end183 end184 describe 'is unset' do185 it 'sets passive to false' do186 @ftp.send(:initialize)187 @ftp.passive.should == false188 end189 end190 end191 describe 'when the debug_mode option' do192 describe 'is set' do193 describe 'to true' do194 it 'sets debug_mode to true' do195 options = mock('ftp initialize options')196 options.should_receive(:to_hash).and_return({ debug_mode: true })197 @ftp.send(:initialize, nil, options)198 @ftp.debug_mode.should == true199 end200 end201 describe 'to false' do202 it 'sets debug_mode to false' do203 options = mock('ftp initialize options')204 options.should_receive(:to_hash).and_return({ debug_mode: false })205 @ftp.send(:initialize, nil, options)206 @ftp.debug_mode.should == false207 end208 end209 end210 describe 'is unset' do211 it 'sets debug_mode to false' do212 @ftp.send(:initialize)213 @ftp.debug_mode.should == false214 end215 end216 end217 describe 'when the open_timeout option' do218 describe 'is set' do219 it 'sets open_timeout to the specified value' do220 options = mock('ftp initialize options')221 options.should_receive(:to_hash).and_return({ open_timeout: 42 })222 @ftp.send(:initialize, nil, options)223 @ftp.open_timeout.should == 42224 end225 end226 describe 'is not set' do227 it 'sets open_timeout to nil' do228 @ftp.send(:initialize)229 @ftp.open_timeout.should == nil230 end231 end232 end233 describe 'when the read_timeout option' do234 describe 'is set' do235 it 'sets read_timeout to the specified value' do236 options = mock('ftp initialize options')237 options.should_receive(:to_hash).and_return({ read_timeout: 100 })238 @ftp.send(:initialize, nil, options)239 @ftp.read_timeout.should == 100240 end241 end242 describe 'is not set' do243 it 'sets read_timeout to the default value' do244 @ftp.send(:initialize)245 @ftp.read_timeout.should == 60246 end247 end248 end249 describe 'when the ssl_handshake_timeout option' do250 describe 'is set' do251 it 'sets ssl_handshake_timeout to the specified value' do252 options = mock('ftp initialize options')253 options.should_receive(:to_hash).and_return({ ssl_handshake_timeout: 23 })254 @ftp.send(:initialize, nil, options)255 @ftp.ssl_handshake_timeout.should == 23256 end257 end258 describe 'is not set' do259 it 'sets ssl_handshake_timeout to nil' do260 @ftp.send(:initialize)261 @ftp.ssl_handshake_timeout.should == nil262 end263 end264 end265 describe 'when the ssl option' do266 describe 'is set' do267 describe "and the ssl option's value is true" do268 it 'initializes ssl_context to a blank SSLContext object' do269 options = mock('ftp initialize options')270 options.should_receive(:to_hash).and_return({ ssl: true })271 ssl_context = OpenSSL::SSL::SSLContext.allocate272 ssl_context.stub!(:set_params)273 OpenSSL::SSL::SSLContext.should_receive(:new).and_return(ssl_context)274 ssl_context.should_receive(:set_params).with({})275 @ftp.send(:initialize, nil, options)276 @ftp.instance_variable_get(:@ssl_context).should == ssl_context277 end278 end279 describe "and the ssl option's value is a hash" do280 it 'initializes ssl_context to a configured SSLContext object' do281 options = mock('ftp initialize options')282 options.should_receive(:to_hash).and_return({ ssl: {key: 'value'} })283 ssl_context = OpenSSL::SSL::SSLContext.allocate284 ssl_context.stub!(:set_params)285 OpenSSL::SSL::SSLContext.should_receive(:new).and_return(ssl_context)286 ssl_context.should_receive(:set_params).with({key: 'value'})287 @ftp.send(:initialize, nil, options)288 @ftp.instance_variable_get(:@ssl_context).should == ssl_context289 end290 end291 describe 'and private_data_connection' do292 describe 'is set' do293 it 'sets private_data_connection to that value' do294 options = mock('ftp initialize options')295 options.should_receive(:to_hash).and_return({ ssl: true, private_data_connection: 'true' })296 @ftp.send(:initialize, nil, options)297 @ftp.instance_variable_get(:@private_data_connection).should == 'true'298 end299 end300 describe 'is not set' do301 it 'sets private_data_connection to nil' do302 options = mock('ftp initialize options')303 options.should_receive(:to_hash).and_return({ ssl: true })304 @ftp.send(:initialize, nil, options)305 @ftp.instance_variable_get(:@private_data_connection).should == true306 end307 end308 end309 end310 describe 'is not set' do311 it 'sets ssl_context to nil' do312 options = mock('ftp initialize options')313 options.should_receive(:to_hash).and_return({})314 @ftp.send(:initialize, nil, options)315 @ftp.instance_variable_get(:@ssl_context).should == nil316 end317 describe 'private_data_connection' do318 describe 'is set' do319 it 'raises an ArgumentError' do320 options = mock('ftp initialize options')321 options.should_receive(:to_hash).and_return({ private_data_connection: true })322 -> {323 @ftp.send(:initialize, nil, options)324 }.should raise_error(ArgumentError, /private_data_connection can be set to true only when ssl is enabled/)325 end326 end327 describe 'is not set' do328 it 'sets private_data_connection to false' do329 options = mock('ftp initialize options')330 options.should_receive(:to_hash).and_return({})331 @ftp.send(:initialize, nil, options)332 @ftp.instance_variable_get(:@private_data_connection).should == false333 end334 end335 end336 end337 end338 end339end...

Full Screen

Full Screen

initializer_spec.rb

Source:initializer_spec.rb Github

copy

Full Screen

...14 config.reconnect_attempts = 1.0/0.015 config.reconnect_timer = 516 end17 end18 def initialize_rayo(options = {})19 reset_default_config20 allow(described_class).to receive(:connect)21 Adhearsion.config.core do |config|22 config.type = options[:type] if options.has_key?(:type)23 config.username = options[:username] if options.has_key?(:username)24 config.password = options[:password] if options.has_key?(:password)25 config.host = options[:host] if options.has_key?(:host)26 config.port = options[:port] if options.has_key?(:port)27 config.certs_directory = options[:certs_directory] if options.has_key?(:certs_directory)28 config.root_domain = options[:root_domain] if options.has_key?(:root_domain)29 config.connection_timeout = options[:connection_timeout] if options.has_key?(:connection_timeout)30 config.reconnect_attempts = options[:reconnect_attempts] if options.has_key?(:reconnect_attempts)31 config.reconnect_timer = options[:reconnect_timer] if options.has_key?(:reconnect_timer)32 end33 described_class.init34 Adhearsion.config.core35 end36 let(:call_id) { rand.to_s }37 let(:offer) { Adhearsion::Event::Offer.new :target_call_id => call_id }38 let(:mock_call) { Adhearsion::Call.new }39 let(:mock_client) { double 'Client' }40 before do41 allow(mock_call).to receive_messages :id => call_id42 mock_client.as_null_object43 allow(mock_client).to receive_messages :event_handler= => true44 Adhearsion::Events.refresh!45 allow(Adhearsion::Process).to receive_messages :fqdn => 'hostname'46 allow(::Process).to receive_messages :pid => 123447 end48 describe "starts the client with the default values" do49 subject { initialize_rayo }50 it "should set properly the username value" do51 expect(subject.username).to eq('usera@127.0.0.1')52 end53 it "should set properly the password value" do54 expect(subject.password).to eq('1')55 end56 it "should set properly the host value" do57 expect(subject.host).to be_nil58 end59 it "should set properly the port value" do60 expect(subject.port).to be_nil61 end62 it "should set properly the certs_directory value" do63 expect(subject.certs_directory).to be_nil64 end65 it "should set properly the root_domain value" do66 expect(subject.root_domain).to be_nil67 end68 it "should properly set the reconnect_attempts value" do69 expect(subject.reconnect_attempts).to eq(1.0/0.0)70 end71 it "should properly set the reconnect_timer value" do72 expect(subject.reconnect_timer).to eq(5)73 end74 end75 it "starts the client with the correct resource" do76 username = "usera@127.0.0.1/hostname-1234"77 expect(Adhearsion::Rayo::Connection::XMPP).to receive(:new).once.with(hash_including :username => username).and_return mock_client78 initialize_rayo79 end80 context "when the fqdn is not available" do81 it "should use the local hostname instead" do82 allow(Adhearsion::Process).to receive(:fqdn).and_raise SocketError83 allow(Socket).to receive(:gethostname).and_return 'local_hostname'84 username = "usera@127.0.0.1/local_hostname-1234"85 expect(Adhearsion::Rayo::Connection::XMPP).to receive(:new).once.with(hash_including :username => username).and_return mock_client86 initialize_rayo87 end88 end89 it "starts the client with any overridden settings" do90 expect(Adhearsion::Rayo::Connection::XMPP).to receive(:new).once.with(username: 'userb@127.0.0.1/foo', password: '123', host: 'foo.bar.com', port: 200, certs: '/foo/bar', connection_timeout: 20, root_domain: 'foo.com').and_return mock_client91 initialize_rayo username: 'userb@127.0.0.1/foo', password: '123', host: 'foo.bar.com', port: 200, certs_directory: '/foo/bar', connection_timeout: 20, root_domain: 'foo.com'92 end93 describe "#connect" do94 it 'should block until the connection is established' do95 reset_default_config96 mock_connection = double :mock_connection97 expect(mock_connection).to receive(:register_event_handler).once98 expect(Adhearsion::Rayo::Client).to receive(:new).once.and_return mock_connection99 expect(mock_connection).to receive(:run).once100 t = Thread.new { described_class.init; described_class.run }101 t.join 5102 expect(t.status).to eq("sleep")103 Adhearsion::Events.trigger_immediately :rayo, Adhearsion::Rayo::Connection::Connected.new104 t.join105 end106 end107 describe '#connect_to_server' do108 before :each do109 Adhearsion::Process.reset110 described_class.config = reset_default_config111 described_class.config.reconnect_attempts = 1112 expect(Adhearsion::Logging.get_logger(described_class)).to receive(:fatal).at_most(:once)113 allow(described_class).to receive(:client).and_return mock_client114 end115 after :each do116 Adhearsion::Process.reset117 end118 it 'should reset the Adhearsion process state to "booting"' do119 Adhearsion::Process.booted120 expect(Adhearsion::Process.state_name).to eq(:running)121 allow(mock_client).to receive(:run).and_raise Adhearsion::Rayo::DisconnectedError122 expect(Adhearsion::Process).to receive(:reset).at_least(:once)123 described_class.connect_to_server124 end125 it 'should retry the connection the specified number of times' do126 described_class.config.reconnect_attempts = 3127 allow(mock_client).to receive(:run).and_raise Adhearsion::Rayo::DisconnectedError128 described_class.connect_to_server129 expect(described_class.attempts).to eq(3)130 end131 it 'should preserve a Adhearsion::ProtocolError exception and give up' do132 allow(mock_client).to receive(:run).and_raise Adhearsion::ProtocolError133 expect { described_class.connect_to_server }.to raise_error Adhearsion::ProtocolError134 end135 it 'should not attempt to reconnect if Adhearsion is shutting down' do136 skip 'Strange interaction with specs restarting Celluloid; TODO re-consider along with migration to actors for Process/connection management'137 Adhearsion::Process.booted138 Adhearsion::Process.shutdown139 allow(mock_client).to receive(:run).and_raise Adhearsion::Rayo::DisconnectedError140 expect { described_class.connect_to_server }.not_to raise_error141 end142 end143 describe 'using Asterisk' do144 let(:overrides) { {:username => 'test', :password => '123', :host => 'foo.bar.com', :port => 200, :certs => nil, :connection_timeout => 20, :root_domain => 'foo.com'} }145 it 'should start an Asterisk PB connection' do146 expect(Adhearsion::Rayo::Connection::Asterisk).to receive(:new).once.with(overrides).and_return mock_client147 initialize_rayo overrides.merge(:type => :asterisk)148 end149 end150 it 'should place events into the event handler' do151 event = nil152 latch = CountDownLatch.new 1153 Adhearsion::Events.register_handler :rayo do |offer|154 event = offer155 latch.countdown!156 end157 initialize_rayo158 described_class.client.handle_event offer159 expect(latch.wait(2)).to be_truthy160 expect(event).to be(offer)161 end162 describe "dispatching an offer" do163 before do164 initialize_rayo165 end166 it "should create and route the call" do167 expect(Adhearsion::Call).to receive(:new).once.and_return mock_call168 expect(mock_call).to receive_message_chain("async.route")169 Adhearsion::Rayo::Initializer.dispatch_offer offer170 end171 end172 describe "dispatching a component event" do173 let(:component) { double 'ComponentNode' }174 let(:mock_event) { double 'Event' }175 before { allow(mock_event).to receive_messages target_call_id: call_id, source: component }176 before do177 initialize_rayo178 end179 it "should place the event in the call's inbox" do180 expect(component).to receive(:trigger_event_handler).once.with mock_event181 Adhearsion::Events.trigger_immediately :rayo, mock_event182 end183 end184 describe "dispatching a call event" do185 let(:mock_event) { double 'Event' }186 before { allow(mock_event).to receive_messages target_call_id: call_id }187 describe "with an active call" do188 before do189 initialize_rayo190 Adhearsion.active_calls << mock_call191 end192 it "should forward the event to the call actor" do193 events = []194 mock_call.register_event_handler do |event|195 events << event196 end197 described_class.dispatch_call_event mock_event198 sleep 0.5199 expect(events).to eql([mock_event])200 end201 it "should not block on the call handling the event" do202 mock_call.register_event_handler do |event|203 sleep 5...

Full Screen

Full Screen

listener_mocker.rb

Source:listener_mocker.rb Github

copy

Full Screen

...8 9 def self.with_socket_mocked(callback_proc)10 TCPSocket.class_eval{ ListenerMocker.with_socket_mocked_callback_proc = callback_proc }11 TCPSocket.class_eval do12 alias_method :initialize_old, :initialize13 def initialize(*args)14 initialize_old(*args) if ListenerMocker.with_socket_mocked_callback_proc.call(args)15 end16 end17 yield18 ensure19 TCPSocket.class_eval do20 if method_defined?(:initialize_old)21 alias_method :initialize, :initialize_old22 end23 end24 end25 26 class << self27 attr_accessor :mock_clients, :tracker_of_callers28 end29 30 def self.mocker_proc31 Proc.new do 32 attr_accessor :messagecbs, :connected33 ListenerMocker.mock_clients ||= {}34 ListenerMocker.tracker_of_callers ||= {}35 ...

Full Screen

Full Screen

initialize

Using AI Code Generation

copy

Full Screen

1mock = Mock.new("Bob", 20)2mock = Mock.new("Bob")3mock = Mock.new(20)4mock = Mock.new(20, "Bob")5mock = Mock.new("Bob", 20, "John")6mock = Mock.new("Bob", 20, "John", 30)7mock = Mock.new("Bob", 20, "John", 30, "Joe")8mock = Mock.new("Bob", 20, "John", 30, "Joe", 40)9mock = Mock.new("Bob", 20, "John", 30, "Joe", 40, "Jack")10mock = Mock.new("Bob", 20, "John", 30,

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Active_mocker_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful