Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring

  • Loading branch information...
commit d9cfa187b89b14d8a672950506142e2daac68f94 1 parent 49e86ad
@arailsdemo authored
View
10 lib/vmc/cli.rb
@@ -2,7 +2,17 @@
module VMC
class Cli < Thor
+ protected
+
+ def client
+ @client ||= VMC::Client.new
+ end
+
+ def config
+ @config ||= VMC::Cli::Config.new
+ end
end
end
+require 'vmc/cli/config'
require 'vmc/cli/methods/user'
View
7 lib/vmc/cli/config.rb
@@ -0,0 +1,7 @@
+module VMC
+ class Cli
+ class Config
+
+ end
+ end
+end
View
7 lib/vmc/cli/methods/user.rb
@@ -8,13 +8,10 @@ class Cli
def login
email = options[:email] || get_option(:email)
password = options[:password] || get_option(:password)
- say "Attempting to login."
+ say "Attempting to login.", :yellow
- client = VMC::Client.new
token = client.login(email, password)
- say "Login successful."
-
- config = VMC::Cli::Config.new
+ say "Login successful.", :green
config.update(:tokens, token)
end
View
12 spec/spec_helper.rb
@@ -31,4 +31,16 @@ def capture(stream)
end
result
+end
+
+def mock_client(stubs={})
+ client = double(VMC::Client, stubs)
+ VMC::Client.stub(:new) { client }
+ client
+end
+
+def mock_config(stubs={})
+ config = double(VMC::Cli::Config, stubs)
+ VMC::Cli::Config.stub(:new) { config }
+ config
end
View
23 spec/vmc/cli/methods/user_spec.rb
@@ -2,10 +2,17 @@
describe VMC::Cli do
describe "#login" do
+ def stub_login_and_update_setting
+ mock_client.should_receive(:login).
+ with('foo@bar.com', 'sekret') { 'token' }
+ mock_config.should_receive(:update).with(:tokens, 'token')
+ end
+
context "when credentials are not provided" do
let(:login) { VMC::Cli.start(["login"]) }
it "asks for an email and password" do
+ stub_login_and_update_setting
$stdin.should_receive(:gets).and_return('foo@bar.com', 'sekret')
results = capture(:stdout) { login }
results.should =~ /Please enter your email:/
@@ -36,21 +43,15 @@
end
it "does not ask for email or password" do
- $stdin.should_not_receive(:gets)
- results = capture(:stdout) { login }
- results.should =~ /Attempting to login./
+ stub_login_and_update_setting
+ $stdin.should_not_receive(:gets)
+ results = capture(:stdout) { login }
+ results.should =~ /Attempting to login./
end
context "and login will succeed" do
it "saves the token via Config and displays a success message" do
- client = double(VMC::Client)
- client.should_receive(:login).with('foo@bar.com', 'sekret') { 'token' }
- VMC::Client.stub(:new) { client }
-
- config = double(VMC::Cli::Config)
- config.should_receive(:update).with(:tokens, 'token')
- VMC::Cli::Config.stub(:new) { config }
-
+ stub_login_and_update_setting
results = capture(:stdout) { login }
results.should =~ /Login successful./
end
Please sign in to comment.
Something went wrong with that request. Please try again.