Skip to content

Commit 53a6aeb

Browse files
committed
[rb] fix stubbing / mocking issues in specs
1 parent b3a689b commit 53a6aeb

26 files changed

+249
-161
lines changed

rb/.rubocop.yml

-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ RSpec/MessageSpies:
136136
RSpec/MultipleExpectations:
137137
Enabled: false
138138

139-
RSpec/StubbedMock:
140-
Enabled: false
141-
142139
RSpec/MultipleMemoizedHelpers:
143140
Enabled: false
144141

rb/spec/integration/selenium/webdriver/chrome/profile_spec.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,26 @@ module Chrome
2828
it 'adds an extension' do
2929
ext_path = '/some/path.crx'
3030

31-
expect(File).to receive(:file?).with(ext_path).and_return true
31+
allow(File).to receive(:file?).with(ext_path).and_return true
32+
3233
expect(profile.add_extension(ext_path)).to eq([ext_path])
3334
end
3435

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

3940
profile.add_extension(ext_path)
4041

4142
ext_file = instance_double('file')
42-
expect(File).to receive(:open).with(ext_path, 'rb').and_yield ext_file
43-
expect(ext_file).to receive(:read).and_return 'test'
43+
allow(File).to receive(:open).with(ext_path, 'rb').and_yield ext_file
44+
allow(ext_file).to receive(:read).and_return 'test'
4445

45-
expect(profile).to receive(:layout_on_disk).and_return 'ignored'
46+
allow(profile).to receive(:layout_on_disk).and_return 'ignored'
4647

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

5153
it "raises an error if the extension doesn't exist" do

rb/spec/integration/selenium/webdriver/edge/profile_spec.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,25 @@ module Edge
2828
it 'adds an extension' do
2929
ext_path = '/some/path.crx'
3030

31-
expect(File).to receive(:file?).with(ext_path).and_return true
31+
allow(File).to receive(:file?).with(ext_path).and_return true
3232
expect(profile.add_extension(ext_path)).to eq([ext_path])
3333
end
3434

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

3939
profile.add_extension(ext_path)
4040

4141
ext_file = instance_double('file')
42-
expect(File).to receive(:open).with(ext_path, 'rb').and_yield ext_file
43-
expect(ext_file).to receive(:read).and_return 'test'
42+
allow(File).to receive(:open).with(ext_path, 'rb').and_yield ext_file
43+
allow(ext_file).to receive(:read).and_return 'test'
4444

45-
expect(profile).to receive(:layout_on_disk).and_return 'ignored'
45+
allow(profile).to receive(:layout_on_disk).and_return 'ignored'
4646

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

5152
it "raises an error if the extension doesn't exist" do

rb/spec/unit/selenium/server_spec.rb

+41-19
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,23 @@ module Selenium
3232
end
3333

3434
it 'uses the given jar file and port' do
35-
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
36-
37-
expect(ChildProcess).to receive(:build)
35+
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
36+
allow(ChildProcess).to receive(:build)
3837
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
3938
.and_return(mock_process)
4039

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

4443
server.start
44+
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
45+
expect(ChildProcess).to have_received(:build)
46+
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
4547
end
4648

4749
it 'waits for the server process by default' do
48-
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
49-
50-
expect(ChildProcess).to receive(:build)
50+
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
51+
allow(ChildProcess).to receive(:build)
5152
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '4444')
5253
.and_return(mock_process)
5354

@@ -56,12 +57,14 @@ module Selenium
5657

5758
expect(mock_process).to receive(:wait)
5859
server.start
60+
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
61+
expect(ChildProcess).to have_received(:build)
62+
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '4444')
5963
end
6064

6165
it 'adds additional args' do
62-
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
63-
64-
expect(ChildProcess).to receive(:build)
66+
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
67+
allow(ChildProcess).to receive(:build)
6568
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '4444', 'foo', 'bar')
6669
.and_return(mock_process)
6770

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

7376
server.start
77+
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
78+
expect(ChildProcess).to have_received(:build)
79+
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone',
80+
'--port', '4444', 'foo', 'bar')
7481
end
7582

7683
it 'adds additional JAVA options args' do
77-
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
78-
79-
expect(ChildProcess).to receive(:build)
84+
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
85+
allow(ChildProcess).to receive(:build)
8086
.with('java',
8187
'-Dwebdriver.chrome.driver=/bin/chromedriver',
8288
'-jar', 'selenium_server_deploy.jar',
@@ -93,6 +99,15 @@ module Selenium
9399
server << '-Dwebdriver.chrome.driver=/bin/chromedriver'
94100

95101
server.start
102+
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
103+
expect(ChildProcess).to have_received(:build)
104+
.with('java',
105+
'-Dwebdriver.chrome.driver=/bin/chromedriver',
106+
'-jar', 'selenium_server_deploy.jar',
107+
'standalone',
108+
'--port', '4444',
109+
'foo',
110+
'bar')
96111
end
97112

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

120-
expect(Selenium::Server).to receive(:download).with(required_version).and_return(expected_download_file_name)
121-
expect(Selenium::Server).to receive(:new).with(expected_download_file_name, expected_options).and_return(fake_server)
135+
allow(Selenium::Server).to receive(:download).with(required_version).and_return(expected_download_file_name)
136+
allow(Selenium::Server).to receive(:new).with(expected_download_file_name, expected_options).and_return(fake_server)
137+
122138
server = Selenium::Server.get required_version, expected_options
123139
expect(server).to eq(fake_server)
140+
expect(Selenium::Server).to have_received(:download).with(required_version)
141+
expect(Selenium::Server).to have_received(:new).with(expected_download_file_name, expected_options)
124142
end
125143

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

140-
expect(File).to receive(:exist?).with(expected_download_file_name).and_return true
158+
allow(File).to receive(:exist?).with(expected_download_file_name).and_return true
141159

142160
Selenium::Server.download required_version
161+
expect(File).to have_received(:exist?).with(expected_download_file_name)
143162
end
144163

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

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

167186
begin
168187
actual_download_file_name = Selenium::Server.download(:latest)
169188
expect(actual_download_file_name).to eq(expected_download_file_name)
170189
expect(File).to exist(expected_download_file_name)
190+
expect(Selenium::Server).to have_received(:latest)
171191
ensure
172192
FileUtils.rm_rf expected_download_file_name
173193
end
174194
end
175195

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

179199
poller = instance_double('SocketPoller')
180-
expect(poller).to receive(:connected?).and_return(false)
200+
allow(poller).to receive(:connected?).and_return(false)
181201

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

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

188209
it 'sets options after instantiation' do
189-
expect(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
210+
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
190211
server = Selenium::Server.new('selenium_server_deploy.jar')
191212
expect(server.port).to eq(4444)
192213
expect(server.timeout).to eq(30)
@@ -204,6 +225,7 @@ module Selenium
204225
expect(server.background).to be_truthy
205226
expect(server.log).to eq('/tmp/server.log')
206227
end
228+
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
207229
end
208230
end
209231
end # Selenium

rb/spec/unit/selenium/webdriver/chrome/profile_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,28 @@ module Chrome
4343
end
4444

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

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

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

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

5857
mock_io = StringIO.new
5958
expect(FileUtils).to receive(:mkdir_p).with('/tmp/some/path/Default')
60-
expect(File).to receive(:open).with('/tmp/some/path/Default/Preferences', 'w').and_yield(mock_io)
59+
allow(File).to receive(:open).with('/tmp/some/path/Default/Preferences', 'w').and_yield(mock_io)
6160

6261
model_profile.layout_on_disk
6362

6463
result = JSON.parse(mock_io.string)
6564

6665
expect(result['autofill']['enabled']).to eq(false)
6766
expect(result['some']['other']['pref']).to eq(123)
67+
expect(File).to have_received(:read).with('/some/path/Default/Preferences')
6868
end
6969
end
7070
end # Chrome

rb/spec/unit/selenium/webdriver/chrome/service_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ module WebDriver
128128
end
129129

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

133133
driver.new
134+
expect(Service).to have_received(:new).with(hash_excluding(url: anything))
134135
end
135136

136137
it 'accepts :driver_path but throws deprecation notice' do

rb/spec/unit/selenium/webdriver/common/action_builder_spec.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ module WebDriver
5454

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

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

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

7172
it 'should add a key input' do
72-
expect(Interactions::KeyInput).to receive(:new).with('keyboard').and_return(:device)
73+
allow(Interactions::KeyInput).to receive(:new).with('keyboard').and_return(:device)
7374
expect(builder).to receive(:add_input).with(:device)
7475

7576
expect(builder.add_key_input('keyboard')).to eq(:device)
77+
expect(Interactions::KeyInput).to have_received(:new).with('keyboard')
7678
end
7779

7880
it 'should get a device by name' do
@@ -112,8 +114,8 @@ module WebDriver
112114
end
113115

114116
it 'should call bridge#send_actions with encoded and compacted devices' do
115-
expect(mouse).to receive(:encode).and_return(nil)
116-
expect(keyboard).to receive(:encode).and_return('not_nil')
117+
allow(mouse).to receive(:encode).and_return(nil)
118+
allow(keyboard).to receive(:encode).and_return('not_nil')
117119
expect(bridge).to receive(:send_actions).with(['not_nil'])
118120
allow(builder).to receive(:clear_all_actions)
119121

rb/spec/unit/selenium/webdriver/common/interactions/input_device_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ def type
6565

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

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

7374
it 'should add a pause action' do

rb/spec/unit/selenium/webdriver/common/interactions/key_actions_spec.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ module WebDriver
3232

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

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

4243
it 'should get the first key_input when no device name is supplied' do
43-
expect(builder).to receive(:get_device).with(nil)
44-
expect(builder).to receive(:key_inputs).and_return([keyboard])
44+
allow(builder).to receive(:get_device).with(nil)
45+
allow(builder).to receive(:key_inputs).and_return([keyboard])
4546
allow(keyboard).to receive(:create_key_down)
4647
allow(builder).to receive(:tick)
4748

rb/spec/unit/selenium/webdriver/common/interactions/key_input_spec.rb

+14-5
Original file line numberDiff line numberDiff line change
@@ -62,36 +62,45 @@ module Interactions
6262

6363
it 'should contain an actions key with an array of actions' do
6464
allow(key_input).to receive(:no_actions?).and_return(false)
65-
expect(key_input.actions).to receive(:map).and_return([1, 2, 3])
65+
allow(key_input.actions).to receive(:map).and_return([1, 2, 3])
66+
6667
expect(key_input.encode).to include(actions: [1, 2, 3])
6768
end
6869
end # when encoding
6970

7071
context 'when creating a key_down action' do
7172
it 'should create a TypingInteraction with the :down direction and key' do
72-
expect(KeyInput::TypingInteraction).to receive(:new).with(key_input, :down, key)
73+
allow(KeyInput::TypingInteraction).to receive(:new).with(key_input, :down, key)
7374
allow(key_input).to receive(:add_action)
75+
7476
key_input.create_key_down(key)
77+
expect(KeyInput::TypingInteraction).to have_received(:new).with(key_input, :down, key)
7578
end
7679

7780
it 'should add the action to the list of actions' do
7881
allow(KeyInput::TypingInteraction).to receive(:new).and_return(:action)
79-
expect(key_input).to receive(:add_action).with(:action)
82+
allow(key_input).to receive(:add_action).with(:action)
83+
8084
key_input.create_key_down(key)
85+
expect(key_input).to have_received(:add_action).with(:action)
8186
end
8287
end # when creating a key_down action
8388

8489
context 'when creating a key_up action' do
8590
it 'should create a TypingInteraction with the :up direction and key' do
86-
expect(KeyInput::TypingInteraction).to receive(:new).with(key_input, :up, key)
91+
allow(KeyInput::TypingInteraction).to receive(:new).with(key_input, :up, key)
8792
allow(key_input).to receive(:add_action)
93+
8894
key_input.create_key_up(key)
95+
expect(KeyInput::TypingInteraction).to have_received(:new).with(key_input, :up, key)
8996
end
9097

9198
it 'should add the action to the list of actions' do
9299
allow(KeyInput::TypingInteraction).to receive(:new).and_return(:action)
93-
expect(key_input).to receive(:add_action).with(:action)
100+
allow(key_input).to receive(:add_action).with(:action)
101+
94102
key_input.create_key_up(key)
103+
expect(key_input).to have_received(:add_action).with(:action)
95104
end
96105
end # when creating a key_up action
97106

0 commit comments

Comments
 (0)