2013-01-16 3 views
0

master 대신 slave 데이터베이스를 쿼리하는 resque 백그라운드 작업을 설정했습니다. 내 resque 클래스에서 나는 슬레이브에 대한 연결을 설정하는 코드를 추가 한 다음 메서드의 끝에서 연결을 해제합니다. 내 질문은 rspec에서 쿼리가 메서드 내에서 특정 데이터베이스를 치는 방법을 테스트하는 방법입니다. 아래 코드 샘플 :rspec에서 데이터베이스 연결 테스트

class ResqueJob 
    @queue = :resque_job 

def self.perform(user_id) 
    ActiveRecord::Base.establish_connection(
     :adapter => "mysql2", 
     :host  => "slave_database.com", 
     :username => "test", 
     :database => "sample" 
    ) 
    user = User.find_by_id(current_user_id) 
    #bunch of code in here 

    ActiveRecord::Base.clear_active_connections! # Disconnect from slave database 
end 

end 

답변

3

일반적으로 라이브러리 코드의 동작을 테스트해서는 안됩니다. 일단 연결을 설정하면 ActiveRecord가 올바른 데이터베이스에 쿼리를 보냅니다. 대신 코드가 작업의 일부인지 테스트합니다.

it "sets the database connection to the slave" do 
    params = { 
     :adapter => "mysql2", 
     :host  => "slave_database.com", 
     :username => "test", 
     :database => "sample" 
    } 
    ActiveRecord::Base.should_receive(:establish_connection).with(params) 
    ActiveRecord::Base.should_receive(:clear_active_connections!) 
    ResqueJob.perform(user) 
end 
관련 문제