如何为波纹管文件创建单元测试
index.ts
mAPIKey = getArguments().getString(ConversationModuleConstants.API_KEY_KEY);
mApplicatoinId = getArguments().getString(ConversationModuleConstants.APPLICATION_ID_KEY);
mDatabaseUrl = getArguments().getString(ConversationModuleConstants.DATABASE_URL_KEY);
FirebaseOptions options = new FirebaseOptions.Builder()
.setApiKey(mAPIKey)
.setApplicationId(mApplicatoinId)
.setDatabaseUrl(mDatabaseUrl)
.build();
boolean hasBeenInitialized = false;
List<FirebaseApp> firebaseApps = FirebaseApp.getApps(getActivity());
for (FirebaseApp app : firebaseApps) {
if (app.getName().equals(FIREBASE_APP_NAME)) {
hasBeenInitialized = true;
mFirebaseApp = app;
}
}
if (!hasBeenInitialized) {
mFirebaseApp = FirebaseApp.initializeApp(getActivity(), options, FIREBASE_APP_NAME);
}
if (FirebaseAuth.getInstance(mFirebaseApp).getCurrentUser() == null) {
Toast.makeText(getActivity(), "Login Required!", Toast.LENGTH_SHORT).show();
return;
}
答案 0 :(得分:1)
测试不是单元的东西有点困难,因为单元通常会公开要测试的方法,所以我假设你最终在测试代码中这样做。所以这里:
///////////
// index.js
///////////
// stubs for this example to work
var createWorkerPromise = function(){ return Promise.resolve(); }
var workerService, kafkaConfig;
function KafkaService(){ throw new Error("crashes test"); }
function exportedFunction(){
return createWorkerPromise()
.then(function(){
var kafkaService = new KafkaService(kafkaConfig, workerService);
})
.catch(function (err) {
console.log('Error while creating workers:', err);
});
}
///////////
// test.js
///////////
var log = console.log.bind(console);
var assert = function(expr) {
if(!expr) throw new Error("AssertionError");
log("TEST OK");
}
// the test
var spy = sinon.spy(console, 'log');
exportedFunction().then(function test() {
assert(spy.called);
}).catch(log.bind(null, "TEST FAILED"))
<script src="https://unpkg.com/sinon@latest/pkg/sinon.js"></script>