Hi,

I've been trying out rspec recently and I've also been trying to test in 
isolation more but I'm unsure how to approach specs for code that leverages 
3rd parties.

Take this over simplified example:

class UserAuthenticator
  def initialize(user)
    @user = user
  end

  def authenticate_by_password(clear_password)
    @user if BCrypt::Password.new(@user.hashed_password) == clear_password
  end
end

In the test you could use a double for the user like this (I'm assuming 
this is the correct thing to do)

it 'returns a user if password is correct' do
  user= double('User', hashed_password:"precomputed hash of 'pass'")
  expect(UserAuthenticator.new(user).authenticate_by_password('pass')).to 
eql(user)
end

Would you leave it at that in this case? Or should Bcrypt be stubbed/mocked 
as well? Or should the password check using BCrypt be in it's own 
class/method (PasswordChecker)?

I guess I'm really interested at what point you would draw the line when it 
comes to isolating specs because it seems like trying to be a purist and 
isolate all external classes/methods leads to diminishing returns at some 
point.

Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/79d4a610-f034-4c27-b018-84c8a6bbf619%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to