Cloud Functions Shell 提供互動式 Shell,可使用測試資料叫用函式。這個殼層支援所有觸發條件類型。
設定管理員憑證 (選用)
如要讓函式測試透過 Firebase Admin SDK 與 Google API 或其他 Firebase API 互動,可能需要設定管理員憑證。
- Cloud Firestore 和 Realtime Database 觸發條件已具備足夠的憑證,不需要額外設定。
- 所有其他 API (包括 Firebase API,例如 Authentication 和 FCM,或 Google API,例如 Cloud Translation 或 Cloud Speech) 都需要本節所述的設定步驟。無論您使用的是 Cloud Functions 殼層或
firebase emulators:start
,都適用這項功能。
如要為模擬函式設定管理員憑證,請按照下列步驟操作:
- 開啟 Google Cloud 主控台的「Service Accounts」(服務帳戶) 窗格。
- 確認已選取App Engine預設服務帳戶,然後使用右側的選項選單選取「建立金鑰」。
- 系統提示時,請選取「JSON」做為金鑰類型,然後按一下「建立」。
將 Google 預設憑證設為指向下載的金鑰:
Unix
export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json" firebase functions:shell
Windows
set GOOGLE_APPLICATION_CREDENTIALS=path\to\key.json firebase functions:shell
完成這些步驟後,函式測試就能使用 Admin SDK 存取 Firebase 和 Google API。舉例來說,在測試 Authentication 觸發程序時,模擬函式可以呼叫 admin.auth().getUserByEmail(email)
。
使用 Cloud Functions Shell 提供函式
Cloud Functions Shell 會模擬所有類型的函式觸發條件,並提供互動式 Shell,讓您使用測試資料叫用函式。選項會因函式類型而異,但基本使用格式如下:
myFunctionName(data, options)
Realtime Database、Cloud Firestore 和 PubSub 觸發程序需要 data
參數,其他函式類型則為選用參數。此外,選用的 options
參數僅適用於 Realtime Database 和 Cloud Firestore 函式。
您也可以選擇從本機檔案載入測試資料,方法是將檔案儲存為變數,然後使用該變數叫用函式:
var data = require('./path/to/testData.json');
myFunction(data);
安裝及設定 Cloud Functions 殼層
如要使用這項功能,firebase-tools
必須為 3.11.0 以上版本,且 firebase-functions
SDK 必須為 0.6.2 以上版本。如要同時更新兩者,請在專案的 functions/
目錄中執行下列指令:
npm install --save firebase-functions@latest
npm install -g firebase-tools
如果您使用自訂函式設定變數,請先執行指令,在本機環境中取得自訂設定 (在 functions
目錄中執行):
firebase functions:config:get > .runtimeconfig.json # If using Windows PowerShell, replace the above with: # firebase functions:config:get | ac .runtimeconfig.json
最後,執行下列指令來執行殼層:
firebase functions:shell
叫用 HTTPS 函式
如要在殼層中叫用 HTTPS 函式,用法與 request
NPM 模組相同,但請將 request
替換為要模擬的函式名稱。例如:
# invoke
myHttpsFunction()
myHttpsFunction.get()
myHttpsFunction.post()
# invoke at sub-path
myHttpsFunction('/path')
myHttpsFunction.get('/path')
myHttpsFunction.post('/path')
# send POST request with form data
myHttpsFunction.post('/path').form( {foo: 'bar' })
叫用 HTTPS 可呼叫函式
在本機叫用 HTTPS 可呼叫函式時,您需要提供適當的測試資料。
# invoke
myCallableFunction('test data')
myCallableFunction({'foo': 'bar'})
您可以選擇傳遞 Firebase-Instance-ID-token
做為第二個參數。這必須是字串。
# invoke with FCM registration token
myCallableFunction('test data', {instanceIdToken: 'sample token'})
目前無法模擬 context.auth
。
叫用即時資料庫函式
在本機執行 Realtime Database 函式時,您需要提供適當的測試資料。這通常表示要為 onCreate
作業提供新的測試資料,為 onDelete
作業提供舊資料/已移除的資料,並為 onUpdate
或 onWrite
函式提供兩者:
# invoke onCreate function
myDatabaseFunction('new_data')
# invoke onDelete function
myDatabaseFunction('old_data')
# invoke onUpdate or onWrite function
myDatabaseFunction({before: 'old_data', after: 'new_data' })
除了 before/after
選項,殼層還提供 params
選項,可在路徑中模擬萬用字元:
# mock wildcards in path, for example: if the path was input/{group}/{id}
myDatabaseFunction('data', {params: {group: 'a', id: 123}})
根據預設,這個殼層會以管理員 (服務帳戶) 權限執行 Realtime Database 函式。使用 auth
選項,以特定使用者或未經驗證的使用者身分執行函式:
# to mock unauthenticated user
myDatabaseFunction('data', {authMode: 'USER'})
# to mock end user
myDatabaseFunction('data', {auth: {uid: 'abcd'}})
叫用 Firestore 函式
在本機執行 Firestore 函式時,您需要提供適當的測試資料。一般來說,這表示要為 onCreate
作業提供新的測試資料,為 onDelete
作業提供舊資料/已移除的資料,並為 onUpdate
或 onWrite
函式提供這兩者。請注意,Firestore 資料必須是鍵/值配對;請參閱「支援的資料類型」。
# invoke onCreate function
myFirestoreFunction({foo: ‘new’})
# invoke onDelete function
myFirestoreFunction({foo: ‘old’})
# invoke onUpdate or onWrite function
myFirestoreFunction({before: {foo: ‘old’}, after: {foo: ‘new’} })
除了 before/after
物件的 data
欄位,您也可以使用 options
物件的 params
欄位,模擬文件名稱中的萬用字元:
# mock wildcards in document name, for example: if the name was input/{group}/{id}
myFirestoreFunction({foo: ‘new’}, {params: {group: 'a', id: 123}})
Shell 一律會以管理權限執行 Firestore 函式,也就是說,它會模擬建立/更新/刪除事件,就像是由管理使用者所為。
叫用 Pub/Sub 函式
如果是 PubSub 函式,請在 Buffer
執行個體中插入訊息酬載,並視需要新增資料屬性,如下所示:
// invokes a function with the JSON message { hello: 'world' } and attributes { foo: 'bar' }
myPubsubFunction({data: new Buffer('{"hello":"world"}'), attributes: {foo: 'bar'}})
叫用 Analytics 函式
您可以在殼層中執行 myAnalyticsFunction()
,不使用任何資料來叫用 Analytics 函式。如要使用測試資料執行函式,建議為函式需要的特定事件資料欄位定義變數:
var data = {
eventDim: [{
// populates event.data.params
params: {foo: {stringValue: 'bar'} },
// Also valid:
// {intValue: '10'}, {floatValue: '1.0'}, {doubleValue: '1.0'}
// populates event.data.name
name: 'event_name',
// populates event.data.logTime, specify in microseconds
timestampMicros: Date.now() * 1000,
// populates event.data.previousLogTime, specify in microseconds
previousTimestampMicros: Date.now() * 1000,
// populates event.data.reportingDate, specify in 'YYYYMMDD' format
date: '20170930',
// populates event.data.valueInUSD
valueInUsd: 230
}],
userDim: userDim
};
myAnalyticsFunction(data);
叫用 Storage 和 Auth 函式
如果是 Storage 和 Auth 函式,請使用您想在函式中查看的測試資料,叫用本機函式。測試資料必須符合對應的資料格式:
- 針對 Cloud Storage:
ObjectMetadata
- 針對 Authentication:
UserRecord
請只指定程式碼相依的欄位,或完全不指定 (如果您只想執行函式)。