Almaron
Almaron

Reputation: 4147

Rails: testing custom classes with RSpec

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.

Upvotes: 0

Views: 2294

Answers (1)

Natus Drew
Natus Drew

Reputation: 1896

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.

Upvotes: 1

Related Questions