Skip to content

Commit

Permalink
Spec Vehicle. Add a method missing for vehicle properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
timdorr committed Dec 15, 2014
1 parent 7f0c828 commit f273116
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/tesla_api/vehicle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ def initialize(api, email, id, vehicle)
@vehicle = vehicle
end

# Properties

def [](key)
vehicle[key]
end

def method_missing(name)
if vehicle.keys.include?(name.to_s)
vehicle[name.to_s]
else
raise NoMethodError.new("Vehicle does not have property `#{name}`", name)
end
end

# State

def mobile_enabled
Expand Down
89 changes: 89 additions & 0 deletions spec/cassettes/vehicle.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions spec/lib/tesla_api/vehicle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'spec_helper'

RSpec.describe TeslaApi::Vehicle do
let(:tesla_api) { TeslaApi::Client.new(ENV["TESLA_EMAIL"], ENV["TESLA_PASS"], ENV["TESLA_CLIENT_ID"], ENV["TESLA_CLIENT_SECRET"]) }

subject(:vehicle) { tesla_api.vehicles.first }

describe "#[]", vcr: { cassette_name: "vehicle" } do
it "contains the vehicle name" do
expect(vehicle["display_name"]).to eq("Nikola")
end
end

describe "#method_missing", vcr: {cassette_name: "vehicle"} do
it "doesn't provide dynamic methods for nonexistent properties" do
expect{vehicle.gas_level}.to raise_exception(NoMethodError)
end

it "provides a dynamic method for the display_name" do
expect(vehicle.display_name).to eq("Nikola")
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require 'tesla_api'

Dotenv.load

Coveralls::Output.silent = true
Coveralls.wear!

VCR.configure do |c|
Expand Down

0 comments on commit f273116

Please sign in to comment.