我測試類的初始化塊如下expect_any_instance_of與對象參數
class A
attr_accessor :client
def initialize(options, configuration)
self.client = B.new(options)
config = C.new(
url: configuration[:url],
headers: configuration[:headers],
username: configuration[:username],
password: configuration[:password]
)
client.configure(config)
end
end
class C
def initialize(options)
# does something with options hash
end
end
class B
def initialize(options)
# does something with options hash
end
def configure(config)
# some configuration with config object
end
end
我的測試情況如下:
let(:options) {
{
force_basic_auth: true
}
}
let(:configuration) {
{
url: 'https://localhost:3000',
headers: { awesome: true },
username: 'test',
password: 'pass'
}
}
let(:api_config) {
C.new(configuration)
}
it 'configures object with passed params' do
expect_any_instance_of(B).to receive(:configure)
.with(api_config)
A.new(
options,
configuration
)
end
這失敗我測試的情況下,因爲在創建該對象初始化塊與我在期望中使用的api_config的object_id
有不同的object_id
。
-[#<C:0x00000002b51128 @url="https://localhost:3000", @headers={:awesome=>true}, @username="test", @password="pass">]
+[#<C:0x00000002a1b628 @url="https://localhost:3000", @headers={:awesome=>true}, @username="test", @password="pass">]
看到這種失敗我在想是否是在初始化塊中直接傳遞這些對象的最佳做法。我的意思是我可以通過直接在初始化塊中傳遞對象來修復它。 有許多函數正在初始化A
類,因爲我正在以當前方式進行散列選項傳遞。
有沒有一種方法來期望在rspec中傳遞的對象的內容,而不是驗證對象是否一樣?直接在初始化中傳遞對象是一種更好的方法嗎?
什麼是'B'?什麼是「配置」?他們是一樣的嗎? –
@UriAgassi對不起,我無法粘貼確切的代碼,所以我改變了類名。我已經添加了問題所需的'B'和'C'類的最小定義。如果我錯過了其他任何事,請告訴我。謝謝:) –
不應''api_config'類型'C'? –