0
我正在爲JRuby中的JDBC編寫一個簡單的包裝器。基本上我想要一個包裝類DBW
,它需要一個連接字符串和一個可選的初始化塊。使用RSpec-mock模擬JRuby中的Java接口
require 'java'
class DBW
def initialize (connection_string, &optional_init_block)
if optional_init_block
yield
end
@connection_string = connection_string
end
def get_connection
Java::java.sql.DriverManager.getConnection(@connection_string)
end
end
但是在我的測試中,我想用一個測試雙象,與DriverManager
註冊Driver
,所以我做了一個RSpec的測試如下:
it "can produce a connection using the connection string" do
mock_conn = instance_double("Connection") # A dummy connection instance
mock_driver = instance_double("Driver")
allow(mock_driver).to receive(:acceptsURL).with(any_args) {
# this doesn't get called when the wrapper calls getConnection on the DriverManager
true
}
# Expecting that connect would be called on the driver with the connecion string...
allow(mock_driver).to receive(:connect).with(any_args).and_return(mock_conn)
wrapper = DBW.new "jdbc:subprotocol:subname" do
# Initialize the DriverManager with the mock driver
Java::java.sql.DriverManager.registerDriver(mock_driver)
end
# This should call in to DriverManager.getConnection
conn = wrapper.get_connection
expect(conn).to eq(mock_conn)
end
我得到以下錯誤運行測試:
Failures:
1) DBW can produce a connection using the connection string
Failure/Error: Unable to find matching line from backtrace
Java::JavaSql::SQLException:
No suitable driver found for jdbc:subprotocol:subname
# java.sql.DriverManager.getConnection(java/sql/DriverManager.java:689)
# java.sql.DriverManager.getConnection(java/sql/DriverManager.java:270)
# java.lang.reflect.Method.invoke(java/lang/reflect/Method.java:497)
# RUBY.get_connection(/playground/dbw.rb:12)
# RUBY.(root)(/playground/specs/dbw_spec.rb:39)
如在測試代碼注意到acceptsURL方法是沒有得到由DriverManager
調用。任何想法我失蹤?