Skip to content

Commit

Permalink
[rb] fix stubbing / mocking issues in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jan 13, 2021
1 parent b3a689b commit 53a6aeb
Show file tree
Hide file tree
Showing 26 changed files with 249 additions and 161 deletions.
3 changes: 0 additions & 3 deletions rb/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ RSpec/MessageSpies:
RSpec/MultipleExpectations:
Enabled: false

RSpec/StubbedMock:
Enabled: false

RSpec/MultipleMemoizedHelpers:
Enabled: false

Expand Down
12 changes: 7 additions & 5 deletions rb/spec/integration/selenium/webdriver/chrome/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,26 @@ module Chrome
it 'adds an extension' do
ext_path = '/some/path.crx'

expect(File).to receive(:file?).with(ext_path).and_return true
allow(File).to receive(:file?).with(ext_path).and_return true

expect(profile.add_extension(ext_path)).to eq([ext_path])
end

it 'reads an extension as binary data' do
ext_path = '/some/path.crx'
expect(File).to receive(:file?).with(ext_path).and_return true
allow(File).to receive(:file?).with(ext_path).and_return true

profile.add_extension(ext_path)

ext_file = instance_double('file')
expect(File).to receive(:open).with(ext_path, 'rb').and_yield ext_file
expect(ext_file).to receive(:read).and_return 'test'
allow(File).to receive(:open).with(ext_path, 'rb').and_yield ext_file
allow(ext_file).to receive(:read).and_return 'test'

expect(profile).to receive(:layout_on_disk).and_return 'ignored'
allow(profile).to receive(:layout_on_disk).and_return 'ignored'

expect(profile.as_json).to eq('directory' => 'ignored',
'extensions' => [Base64.strict_encode64('test')])
expect(ext_file).to have_received(:read)
end

it "raises an error if the extension doesn't exist" do
Expand Down
11 changes: 6 additions & 5 deletions rb/spec/integration/selenium/webdriver/edge/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,25 @@ module Edge
it 'adds an extension' do
ext_path = '/some/path.crx'

expect(File).to receive(:file?).with(ext_path).and_return true
allow(File).to receive(:file?).with(ext_path).and_return true
expect(profile.add_extension(ext_path)).to eq([ext_path])
end

it 'reads an extension as binary data' do
ext_path = '/some/path.crx'
expect(File).to receive(:file?).with(ext_path).and_return true
allow(File).to receive(:file?).with(ext_path).and_return true

profile.add_extension(ext_path)

ext_file = instance_double('file')
expect(File).to receive(:open).with(ext_path, 'rb').and_yield ext_file
expect(ext_file).to receive(:read).and_return 'test'
allow(File).to receive(:open).with(ext_path, 'rb').and_yield ext_file
allow(ext_file).to receive(:read).and_return 'test'

expect(profile).to receive(:layout_on_disk).and_return 'ignored'
allow(profile).to receive(:layout_on_disk).and_return 'ignored'

expect(profile.as_json).to eq('directory' => 'ignored',
'extensions' => [Base64.strict_encode64('test')])
expect(ext_file).to have_received(:read)
end

it "raises an error if the extension doesn't exist" do
Expand Down
60 changes: 41 additions & 19 deletions rb/spec/unit/selenium/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,23 @@ module Selenium
end

it 'uses the given jar file and port' do
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)

expect(ChildProcess).to receive(:build)
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(ChildProcess).to receive(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
.and_return(mock_process)

server = Selenium::Server.new('selenium_server_deploy.jar', port: 1234, background: true)
allow(server).to receive(:socket).and_return(mock_poller)

server.start
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
expect(ChildProcess).to have_received(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
end

it 'waits for the server process by default' do
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)

expect(ChildProcess).to receive(:build)
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(ChildProcess).to receive(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '4444')
.and_return(mock_process)

Expand All @@ -56,12 +57,14 @@ module Selenium

expect(mock_process).to receive(:wait)
server.start
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
expect(ChildProcess).to have_received(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '4444')
end

it 'adds additional args' do
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)

expect(ChildProcess).to receive(:build)
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(ChildProcess).to receive(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '4444', 'foo', 'bar')
.and_return(mock_process)

Expand All @@ -71,12 +74,15 @@ module Selenium
server << %w[foo bar]

server.start
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
expect(ChildProcess).to have_received(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone',
'--port', '4444', 'foo', 'bar')
end

it 'adds additional JAVA options args' do
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)

expect(ChildProcess).to receive(:build)
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(ChildProcess).to receive(:build)
.with('java',
'-Dwebdriver.chrome.driver=/bin/chromedriver',
'-jar', 'selenium_server_deploy.jar',
Expand All @@ -93,6 +99,15 @@ module Selenium
server << '-Dwebdriver.chrome.driver=/bin/chromedriver'

server.start
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
expect(ChildProcess).to have_received(:build)
.with('java',
'-Dwebdriver.chrome.driver=/bin/chromedriver',
'-jar', 'selenium_server_deploy.jar',
'standalone',
'--port', '4444',
'foo',
'bar')
end

it 'downloads the specified version from the selenium site' do
Expand All @@ -117,10 +132,13 @@ module Selenium
expected_options = {port: 5555}
fake_server = Object.new

expect(Selenium::Server).to receive(:download).with(required_version).and_return(expected_download_file_name)
expect(Selenium::Server).to receive(:new).with(expected_download_file_name, expected_options).and_return(fake_server)
allow(Selenium::Server).to receive(:download).with(required_version).and_return(expected_download_file_name)
allow(Selenium::Server).to receive(:new).with(expected_download_file_name, expected_options).and_return(fake_server)

server = Selenium::Server.get required_version, expected_options
expect(server).to eq(fake_server)
expect(Selenium::Server).to have_received(:download).with(required_version)
expect(Selenium::Server).to have_received(:new).with(expected_download_file_name, expected_options)
end

it 'automatically repairs http_proxy settings that do not start with http://' do
Expand All @@ -137,9 +155,10 @@ module Selenium
required_version = '10.2.0'
expected_download_file_name = "selenium-server-standalone-#{required_version}.jar"

expect(File).to receive(:exist?).with(expected_download_file_name).and_return true
allow(File).to receive(:exist?).with(expected_download_file_name).and_return true

Selenium::Server.download required_version
expect(File).to have_received(:exist?).with(expected_download_file_name)
end

it 'should know what the latest version available is' do
Expand All @@ -160,33 +179,35 @@ module Selenium
minor_version = '2.42'
expected_download_file_name = "selenium-server-standalone-#{required_version}.jar"

expect(Selenium::Server).to receive(:latest).and_return required_version
allow(Selenium::Server).to receive(:latest).and_return required_version
stub_request(:get, "http://selenium-release.storage.googleapis.com/#{minor_version}/#{expected_download_file_name}")
.to_return(body: 'this is pretending to be a jar file for testing purposes')

begin
actual_download_file_name = Selenium::Server.download(:latest)
expect(actual_download_file_name).to eq(expected_download_file_name)
expect(File).to exist(expected_download_file_name)
expect(Selenium::Server).to have_received(:latest)
ensure
FileUtils.rm_rf expected_download_file_name
end
end

it 'raises Selenium::Server::Error if the server is not launched within the timeout' do
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)

poller = instance_double('SocketPoller')
expect(poller).to receive(:connected?).and_return(false)
allow(poller).to receive(:connected?).and_return(false)

server = Selenium::Server.new('selenium_server_deploy.jar', background: true)
allow(server).to receive(:socket).and_return(poller)

expect { server.start }.to raise_error(Selenium::Server::Error)
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
end

it 'sets options after instantiation' do
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
server = Selenium::Server.new('selenium_server_deploy.jar')
expect(server.port).to eq(4444)
expect(server.timeout).to eq(30)
Expand All @@ -204,6 +225,7 @@ module Selenium
expect(server.background).to be_truthy
expect(server.log).to eq('/tmp/server.log')
end
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
end
end
end # Selenium
10 changes: 5 additions & 5 deletions rb/spec/unit/selenium/webdriver/chrome/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ module Chrome
end

it 'reads existing prefs' do
expect(File).to receive(:read).with('/some/path/Default/Preferences')
.and_return('{"autofill": {"enabled": false}}')
allow(File).to receive(:read).and_return('{"autofill": {"enabled": false}}')

expect(model_profile['autofill.enabled']).to eq(false)
expect(File).to have_received(:read).with('/some/path/Default/Preferences')
end

it 'writes out prefs' do
expect(File).to receive(:read).with('/some/path/Default/Preferences')
.and_return('{"autofill": {"enabled": false}}')
allow(File).to receive(:read).and_return('{"autofill": {"enabled": false}}')

model_profile['some.other.pref'] = 123

mock_io = StringIO.new
expect(FileUtils).to receive(:mkdir_p).with('/tmp/some/path/Default')
expect(File).to receive(:open).with('/tmp/some/path/Default/Preferences', 'w').and_yield(mock_io)
allow(File).to receive(:open).with('/tmp/some/path/Default/Preferences', 'w').and_yield(mock_io)

model_profile.layout_on_disk

result = JSON.parse(mock_io.string)

expect(result['autofill']['enabled']).to eq(false)
expect(result['some']['other']['pref']).to eq(123)
expect(File).to have_received(:read).with('/some/path/Default/Preferences')
end
end
end # Chrome
Expand Down
3 changes: 2 additions & 1 deletion rb/spec/unit/selenium/webdriver/chrome/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ module WebDriver
end

it 'is created when :url is not provided' do
expect(Service).to receive(:new).and_return(service)
allow(Service).to receive(:new).and_return(service)

driver.new
expect(Service).to have_received(:new).with(hash_excluding(url: anything))
end

it 'accepts :driver_path but throws deprecation notice' do
Expand Down
10 changes: 6 additions & 4 deletions rb/spec/unit/selenium/webdriver/common/action_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ module WebDriver

context 'when adding a pointer input' do
it 'should add a PointerInput' do
expect(Interactions::PointerInput).to receive(:new).with(:touch, name: 'touch').and_return(:device)
allow(Interactions::PointerInput).to receive(:new).with(:touch, name: 'touch').and_return(:device)
expect(builder).to receive(:add_input).with(:device)

expect(builder.add_pointer_input(:touch, 'touch')).to eq(:device)
expect(Interactions::PointerInput).to have_received(:new).with(:touch, name: 'touch')
end

it 'should not assign the pointer input as primary if not primary' do
Expand All @@ -69,10 +70,11 @@ module WebDriver
end # when adding a pointer input

it 'should add a key input' do
expect(Interactions::KeyInput).to receive(:new).with('keyboard').and_return(:device)
allow(Interactions::KeyInput).to receive(:new).with('keyboard').and_return(:device)
expect(builder).to receive(:add_input).with(:device)

expect(builder.add_key_input('keyboard')).to eq(:device)
expect(Interactions::KeyInput).to have_received(:new).with('keyboard')
end

it 'should get a device by name' do
Expand Down Expand Up @@ -112,8 +114,8 @@ module WebDriver
end

it 'should call bridge#send_actions with encoded and compacted devices' do
expect(mouse).to receive(:encode).and_return(nil)
expect(keyboard).to receive(:encode).and_return('not_nil')
allow(mouse).to receive(:encode).and_return(nil)
allow(keyboard).to receive(:encode).and_return('not_nil')
expect(bridge).to receive(:send_actions).with(['not_nil'])
allow(builder).to receive(:clear_all_actions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ def type

context 'when creating a pause' do
it 'should create a pause action' do
expect(Pause).to receive(:new).with(device, 5).and_return(action)
allow(Pause).to receive(:new).with(device, 5).and_return(action)

expect { device.create_pause(5) }.to change(device, :actions).from([]).to([action])
expect(Pause).to have_received(:new).with(device, 5)
end

it 'should add a pause action' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ module WebDriver

context 'when performing a key action' do
it 'should get the device if device name is supplied' do
expect(builder).to receive(:get_device).with('name').and_return(keyboard)
allow(builder).to receive(:get_device).with('name').and_return(keyboard)
allow(keyboard).to receive(:create_key_down)
allow(builder).to receive(:tick)

builder.send('key_action', key, action: :create_key_down, device: 'name')
expect(builder).to have_received(:get_device).with('name')
end

it 'should get the first key_input when no device name is supplied' do
expect(builder).to receive(:get_device).with(nil)
expect(builder).to receive(:key_inputs).and_return([keyboard])
allow(builder).to receive(:get_device).with(nil)
allow(builder).to receive(:key_inputs).and_return([keyboard])
allow(keyboard).to receive(:create_key_down)
allow(builder).to receive(:tick)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,45 @@ module Interactions

it 'should contain an actions key with an array of actions' do
allow(key_input).to receive(:no_actions?).and_return(false)
expect(key_input.actions).to receive(:map).and_return([1, 2, 3])
allow(key_input.actions).to receive(:map).and_return([1, 2, 3])

expect(key_input.encode).to include(actions: [1, 2, 3])
end
end # when encoding

context 'when creating a key_down action' do
it 'should create a TypingInteraction with the :down direction and key' do
expect(KeyInput::TypingInteraction).to receive(:new).with(key_input, :down, key)
allow(KeyInput::TypingInteraction).to receive(:new).with(key_input, :down, key)
allow(key_input).to receive(:add_action)

key_input.create_key_down(key)
expect(KeyInput::TypingInteraction).to have_received(:new).with(key_input, :down, key)
end

it 'should add the action to the list of actions' do
allow(KeyInput::TypingInteraction).to receive(:new).and_return(:action)
expect(key_input).to receive(:add_action).with(:action)
allow(key_input).to receive(:add_action).with(:action)

key_input.create_key_down(key)
expect(key_input).to have_received(:add_action).with(:action)
end
end # when creating a key_down action

context 'when creating a key_up action' do
it 'should create a TypingInteraction with the :up direction and key' do
expect(KeyInput::TypingInteraction).to receive(:new).with(key_input, :up, key)
allow(KeyInput::TypingInteraction).to receive(:new).with(key_input, :up, key)
allow(key_input).to receive(:add_action)

key_input.create_key_up(key)
expect(KeyInput::TypingInteraction).to have_received(:new).with(key_input, :up, key)
end

it 'should add the action to the list of actions' do
allow(KeyInput::TypingInteraction).to receive(:new).and_return(:action)
expect(key_input).to receive(:add_action).with(:action)
allow(key_input).to receive(:add_action).with(:action)

key_input.create_key_up(key)
expect(key_input).to have_received(:add_action).with(:action)
end
end # when creating a key_up action

Expand Down
Loading

0 comments on commit 53a6aeb

Please sign in to comment.