0

Yea, I know that this question is silly, newbee and simple, but I still can't figure it out. I've created a class (in app/minions/ directory) to parse auth hashes from 3rd-party services (like google, twitter, etc.). It looks like this.

class AuthHash

  def initialize(hash)
    @hash = hash
    @provider = hash[:provider]
    @uid = hash[:uid]
    create_user_hash
  end

 def create_user_hash
   @user_hash = send("parse_hash_from_" << @hash[:provider], @hash)
 end

 def credentials
    {provider: @provider, uid: @uid}
 end

 def user_hash
    @user_hash
 end

 private

   # parse_hash_from_* methods here

end

I've added that directory to the autoload path, so I can use it in my controllers. Now I want to write some tests for it.

I'm using RSpec with FactoryGirl for testing. So I started by adding a factory to spec/factories/ called auth_hashes.rb but I can't define a hash as a field in a factory. So I moved the declaration to the spec/minions/auth_hash_spec.rb.

require 'spec_helper'

describe AuthHash do
  before_each do
    auth_hash = AuthHash.new({:provider=>"google_oauth2",:uid=>"123456789",:info=>{:name=>"JohnDoe",:email=>"john@company_name.com",:first_name=>"John",:last_name=>"Doe",:image=>"https://lh3.googleusercontent.com/url/photo.jpg"},:credentials=>{:token=>"token",:refresh_token=>"another_token",:expires_at=>1354920555,:expires=>true},:extra=>{:raw_info=>{:id=>"123456789",:email=>"user@domain.example.com",:verified_email=>true,:name=>"JohnDoe",:given_name=>"John",:family_name=>"Doe",:link=>"https://plus.google.com/123456789",:picture=>"https://lh3.googleusercontent.com/url/photo.jpg",:gender=>"male",:birthday=>"0000-06-25",:locale=>"en",:hd=>"company_name.com"}}})
  end
end

But still it does not seem to work.

I know this should be alot simpler then I'm trying to do, but I can't figure it out.

Almaron
  • 4,127
  • 6
  • 26
  • 48

1 Answers1

1

Add something like this to that new spec (spec/minions/auth_hash_spec.rb) file at the top:

require Rails.root.to_s + '/app/minions/myhash.rb'

And then write your tests.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Natus Drew
  • 1,876
  • 1
  • 21
  • 23
  • Can't I just add `require 'auth_hash'` since it's in the autoload path? – Almaron Sep 26 '13 at 16:06
  • Unless you setup Rails to autoload your new directory, it's not going to autoload it. Refer to this. http://stackoverflow.com/questions/4073856/rails-3-autoload – Natus Drew Sep 26 '13 at 16:32
  • Did you read my comment and the question? I did it to autoload. So, yes, just simple require seems working. But I though there should be a way to move the prototype declarations from the test to some kind of a factory. – Almaron Sep 26 '13 at 16:47
  • I just tested if everything is autoloaded in /app by creating a class and trying to instantiate it. I had to require it to instantiate the class. So everything is not autoloaded under /app in Rails 3.2.13. Are you using a previous version of Rails? – Natus Drew Sep 26 '13 at 18:09