0
是的,我知道這個問題很愚蠢,新手和簡單,但我仍然無法弄清楚。 我創建了一個類(in app/minions/
目錄)來解析來自第三方服務(如谷歌,推特等)的授權哈希。它看起來像這樣。Rails:用RSpec測試自定義類
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
我已經將該目錄添加到autoload路徑,所以我可以在我的控制器中使用它。現在我想爲它寫一些測試。
我使用RSpec與FactoryGirl進行測試。所以我開始在spec/factories/
加入一個叫做auth_hashes.rb
的工廠,但是我不能在工廠裏定義一個散列作爲一個字段。 於是我將聲明移至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=>"[email protected]_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=>"[email protected]",: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
但它似乎仍然沒有工作。
我知道這應該是很簡單,然後我試圖做,但我無法弄清楚。
我不能只添加'require'auth_hash'',因爲它在自動加載路徑中? – Almaron
除非您將Rails設置爲自動加載您的新目錄,否則它不會自動加載它。參考這個。 http://stackoverflow.com/questions/4073856/rails-3-autoload – nathanengineer
你讀過我的評論和問題嗎?我做到了自動加載。所以,是的,只是簡單的要求似乎工作。但我認爲應該有辦法將原型聲明從測試轉移到某種工廠。 – Almaron