Skip to content

Commit

Permalink
refactor specs for ChangePassword (#544)
Browse files Browse the repository at this point in the history
This makes it so that we can pass input and output to ChangePassword in
order to test against them, and prevent them from outputting in the
tests.
  • Loading branch information
mockdeep committed Feb 27, 2021
1 parent 34cff79 commit 8d27b72
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
14 changes: 8 additions & 6 deletions app/tasks/change_password.rb
Expand Up @@ -3,13 +3,15 @@
require_relative "../commands/users/change_user_password"

class ChangePassword
def initialize(command = ChangeUserPassword.new)
def initialize(command = ChangeUserPassword.new, output: $stdout, input: $stdin)
@command = command
@output = output
@input = input
end

def change_password
while (password = ask_password) != ask_confirmation
puts "The confirmation doesn't match the password. Please try again."
@output.puts "The confirmation doesn't match the password. Please try again."
end
@command.change_user_password(password)
end
Expand All @@ -25,9 +27,9 @@ def ask_confirmation
end

def ask_hidden(question)
print(question)
input = $stdin.noecho(&:gets).chomp
puts
input
@output.print(question)
user_input = $stdin.noecho { @input.gets }.chomp
@output.puts
user_input
end
end
23 changes: 9 additions & 14 deletions spec/tasks/change_password_spec.rb
Expand Up @@ -3,30 +3,25 @@
app_require "tasks/change_password"

describe ChangePassword do
let(:command) { double("command") }
let(:new_password) { "new-pw" }

let(:task) { ChangePassword.new(command) }
let(:command) { instance_double(ChangeUserPassword) }

describe "#change_password" do
it "invokes command with confirmed password" do
expect(task).to receive(:ask_hidden).twice
.and_return(new_password, new_password)
output = StringIO.new
input = StringIO.new("new-pw\nnew-pw\n")
task = ChangePassword.new(command, output: output, input: input)

expect(command)
.to receive(:change_user_password)
.with(new_password)
expect(command).to receive(:change_user_password).with("new-pw")

task.change_password
end

it "repeats until a matching confirmation" do
expect(task).to receive(:ask_hidden).exactly(2).times
.and_return(new_password, "", new_password, new_password)
output = StringIO.new
input = StringIO.new("woops\nnope\nnew-pw\nnew-pw\n")
task = ChangePassword.new(command, output: output, input: input)

expect(command)
.to receive(:change_user_password)
.with(new_password)
expect(command).to receive(:change_user_password).with("new-pw")

task.change_password
end
Expand Down

0 comments on commit 8d27b72

Please sign in to comment.