我正在尝试对此代码进行单元测试:
Bundle cidParam(String accountId) {
Bundle params = new Bundle(1);
params.putString(Params.CID, accountId);
return params;
}
Run Code Online (Sandbox Code Playgroud)
这是单元测试:
private void mockBundle(String cid) throws Exception {
Bundle mBundle = PowerMockito.mock(Bundle.class);
PowerMockito.doNothing().when((BaseBundle)mBundle).putString(AnalyticsController.Params.CID, cid);
}
Run Code Online (Sandbox Code Playgroud)
但是,它总是返回:
java.lang.RuntimeException: Method putString in android.os.BaseBundle not mocked.
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用 roboelectric 来启动模拟器并调用真实的包。然而,它会减慢单元测试的速度。有谁知道如何模拟 Android .os.base ?谢谢。
Recently I spent quite some time to figure it out the issue with my unit test. The error that I saw in my unit test is :
Ambiguous type name 'AssetIdentifier' in 'UIImage'
Run Code Online (Sandbox Code Playgroud)
This is the complete code :
import XCTest
import module
@testable import module
class VoucherOptionsViewControllerTests: XCTestCase {
lazy var result = ""
class VoucherOptionsViewControllerMock: VoucherOptionsViewController {
var result = ""
override func showOverlayView(title: String, message: String, assetIdentifier: UIImage.AssetIdentifier) {
self.result = "lalalala"
}
}
}
Run Code Online (Sandbox Code Playgroud)
AssetIdentifier is …