1
1
# frozen_string_literal: true
2
2
3
3
require "rails_helper"
4
- require_relative "../../lib/upcoming_rides"
5
-
6
- # Create a class to include the UpcomingRides module for testing
7
- class UpcomingRidesForTesting
8
- include UpcomingRides
9
- end
10
4
11
5
RSpec . describe UpcomingRides do
12
6
include described_class
@@ -16,6 +10,7 @@ class UpcomingRidesForTesting
16
10
instance_double ( Ride , start_address : Faker ::Address . street_address ,
17
11
destination_address : Faker ::Address . street_address )
18
12
end
13
+
19
14
let ( :headers ) do
20
15
{
21
16
"Accept" => "*/*" ,
@@ -40,11 +35,11 @@ class UpcomingRidesForTesting
40
35
end
41
36
42
37
describe '#sort_upcoming_rides' do
43
- xit 'sorts the upcoming rides by the score in descending order' do
38
+ it 'sorts the upcoming rides by the score in descending order' do
44
39
# Create some sample rides with scores
45
- ride1 = instance_double ( Ride , score : 5 )
46
- ride2 = instance_double ( Ride , score : 7 )
47
- ride3 = instance_double ( Ride , score : 3 )
40
+ ride1 = { score : 5 }
41
+ ride2 = { score : 7 }
42
+ ride3 = { score : 3 }
48
43
49
44
# Expect the sort_upcoming_rides method to return the rides sorted by score
50
45
expect ( sort_upcoming_rides ( [ ride1 , ride2 , ride3 ] ) ) . to eq ( [ ride2 , ride1 , ride3 ] )
@@ -108,18 +103,18 @@ class UpcomingRidesForTesting
108
103
109
104
describe '#add_ride_attributes' do
110
105
it 'returns a hash with ride attributes and additional attributes' do
111
- # Stub the data method to return sample data
112
- allow ( self ) . to receive ( :data ) . and_return ( {
113
- home_address : '123 Main St' ,
114
- start_address : '456 Elm St' ,
115
- commute_distance : 5.0 ,
116
- commute_duration : 10.0 ,
117
- destination_address : '789 Oak St' ,
118
- ride_distance : 8.0 ,
119
- ride_duration : 15.0 ,
120
- ride_earnings : 20.0 ,
121
- score : 0.5
122
- } )
106
+ # Stub the get_data method to return sample data
107
+ allow ( self ) . to receive ( :get_data ) . and_return ( {
108
+ home_address : '123 Main St' ,
109
+ start_address : '456 Elm St' ,
110
+ commute_distance : 5.0 ,
111
+ commute_duration : 10.0 ,
112
+ destination_address : '789 Oak St' ,
113
+ ride_distance : 8.0 ,
114
+ ride_duration : 15.0 ,
115
+ ride_earnings : 20.0 ,
116
+ score : 0.5
117
+ } )
123
118
124
119
# Stub the ride attributes to be merged
125
120
allow ( ride ) . to receive ( :attributes ) . and_return ( { id : 1 , name : 'Ride 1' } )
@@ -163,7 +158,7 @@ class UpcomingRidesForTesting
163
158
end
164
159
end
165
160
166
- describe '#data ' do
161
+ describe '#get_data ' do
167
162
it 'returns a hash with data including commute distance, duration, ride earnings, and score' do
168
163
home_address = instance_double ( Address , id : Faker ::Internet . uuid ,
169
164
address : "1588 E Thompson Blvd" )
@@ -172,6 +167,9 @@ class UpcomingRidesForTesting
172
167
destination_address = instance_double ( Address , id : Faker ::Internet . uuid ,
173
168
address : "2112 E Thompson Blvd" )
174
169
170
+ # expect_any_instance_of(GoogleDirectionsApiClient).to receive(:get_directions).with(home_address.address, start_address.address).and_return([5.0, 10.0])
171
+ # expect_any_instance_of(GoogleDirectionsApiClient).to receive(:get_directions).with(start_address.address, destination_address.address).and_return([5.0, 10.0])
172
+
175
173
stub_request ( :get , "https://maps.googleapis.com/maps/api/directions/json?origin=1588 E Thompson Blvd&destination=2112 E Thompson Blvd&key=#{ Rails . application . credentials . google_api_key } " )
176
174
. with (
177
175
headers : {
@@ -232,20 +230,18 @@ class UpcomingRidesForTesting
232
230
"status" : "OK"
233
231
}' , headers : { } )
234
232
235
- puts data ( home_address , start_address , destination_address ) . inspect
236
-
237
- # Expect the data method to return a hash with the correct data
238
- expect ( data ( home_address , start_address , destination_address ) ) . to eq ( {
239
- home_address : home_address ,
240
- start_address : start_address ,
241
- commute_distance : 5.0 ,
242
- commute_duration : 10.0 ,
243
- destination_address : destination_address ,
244
- ride_distance : 5.0 ,
245
- ride_duration : 10.0 ,
246
- ride_earnings : 12.0 ,
247
- score : 0.6
248
- } )
233
+ # Expect the get_data method to return a hash with the correct data
234
+ expect ( get_data ( home_address , start_address , destination_address ) ) . to eq ( {
235
+ home_address : home_address ,
236
+ start_address : start_address ,
237
+ commute_distance : 5.0 ,
238
+ commute_duration : 10.0 ,
239
+ destination_address : destination_address ,
240
+ ride_distance : 5.0 ,
241
+ ride_duration : 10.0 ,
242
+ ride_earnings : 12.0 ,
243
+ score : 0.6
244
+ } )
249
245
end
250
246
end
251
247
end
0 commit comments