Cloud Functions की यूनिट टेस्टिंग

इस पेज पर, आपके फ़ंक्शन के लिए यूनिट टेस्ट लिखने के सबसे सही तरीके और टूल के बारे में बताया गया है. जैसे, ऐसे टेस्ट जो कंटीन्यूअस इंटिग्रेशन (सीआई) सिस्टम का हिस्सा होंगे. टेस्टिंग को आसान बनाने के लिए, Firebase Cloud Functions के लिए Firebase Test SDK उपलब्ध कराता है. इसे npm पर firebase-functions-test के तौर पर डिस्ट्रिब्यूट किया जाता है. साथ ही, यह firebase-functions के लिए कंपैनियन टेस्ट एसडीके है. Cloud Functions के लिए Firebase Test SDK:

  • यह आपकी जांचों के लिए सही सेटअप और टियरडाउन का ध्यान रखता है. जैसे, firebase-functions के लिए ज़रूरी एनवायरमेंट वैरिएबल सेट और अनसेट करना.
  • यह सैंपल डेटा और इवेंट का कॉन्टेक्स्ट जनरेट करता है, ताकि आपको सिर्फ़ उन फ़ील्ड के बारे में बताना पड़े जो आपके टेस्ट के लिए ज़रूरी हैं.

टेस्ट सेटअप

अपने फ़ंक्शन फ़ोल्डर में ये निर्देश चलाकर, firebase-functions-test और Mocha, दोनों को इंस्टॉल करें. यह एक टेस्टिंग फ़्रेमवर्क है:

npm install --save-dev firebase-functions-test
npm install --save-dev mocha

इसके बाद, फ़ंक्शन फ़ोल्डर में test फ़ोल्डर बनाएं. इसमें अपने टेस्ट कोड के लिए एक नई फ़ाइल बनाएं और उसे index.test.js जैसा कोई नाम दें.

आखिर में, functions/package.json में बदलाव करके ये चीज़ें जोड़ें:

"scripts": {
  "test": "mocha --reporter spec"
}

टेस्ट लिखने के बाद, उन्हें चलाने के लिए npm test को फ़ंक्शन डायरेक्ट्री में चलाएं.

Cloud Functions के लिए Firebase Test SDK शुरू हो रहा है

firebase-functions-test का इस्तेमाल दो तरीकों से किया जा सकता है:

  1. ऑनलाइन मोड (सुझाया गया): ऐसे टेस्ट लिखें जो टेस्टिंग के लिए बनाए गए Firebase प्रोजेक्ट के साथ इंटरैक्ट करते हैं. इससे डेटाबेस में लिखने, उपयोगकर्ता बनाने वगैरह जैसी कार्रवाइयां असल में होंगी. साथ ही, आपका टेस्ट कोड नतीजों की जांच कर पाएगा. इसका यह भी मतलब है कि आपके फ़ंक्शन में इस्तेमाल किए गए अन्य Google SDK टूल भी काम करेंगे.
  2. ऑफ़लाइन मोड: बिना किसी साइड इफ़ेक्ट के, अलग-अलग और ऑफ़लाइन यूनिट टेस्ट लिखें. इसका मतलब है कि Firebase के किसी प्रॉडक्ट (जैसे कि डेटाबेस में लिखना या उपयोगकर्ता बनाना) से इंटरैक्ट करने वाले किसी भी तरीके के कॉल को स्टब किया जाना चाहिए. अगर आपके पास Cloud Firestore या Realtime Database फ़ंक्शन हैं, तो आम तौर पर ऑफ़लाइन मोड का इस्तेमाल करने का सुझाव नहीं दिया जाता. ऐसा इसलिए, क्योंकि इससे आपके टेस्ट कोड की जटिलता काफ़ी बढ़ जाती है.

ऑनलाइन मोड में एसडीके को शुरू करना (सुझाया गया)

अगर आपको ऐसे टेस्ट लिखने हैं जो किसी टेस्ट प्रोजेक्ट के साथ इंटरैक्ट करते हैं, तो आपको firebase-admin के ज़रिए, प्रोजेक्ट कॉन्फ़िगरेशन की वे वैल्यू देनी होंगी जो ऐप्लिकेशन को शुरू करने के लिए ज़रूरी हैं. साथ ही, सेवा खाते की कुंजी वाली फ़ाइल का पाथ भी देना होगा.

अपने Firebase प्रोजेक्ट की कॉन्फ़िगरेशन वैल्यू पाने के लिए:

  1. Firebase कंसोल में जाकर, अपने प्रोजेक्ट की सेटिंग खोलें.
  2. आपके ऐप्लिकेशन में जाकर,अपनी पसंद का ऐप्लिकेशन चुनें.
  3. दाएं पैनल में, Apple और Android ऐप्लिकेशन के लिए कॉन्फ़िगरेशन फ़ाइल डाउनलोड करने का विकल्प चुनें.

    वेब ऐप्लिकेशन के लिए, कॉन्फ़िगरेशन वैल्यू दिखाने के लिए कॉन्फ़िगरेशन चुनें.

कुंजी फ़ाइल बनाने के लिए:

  1. Google Cloud कंसोल में, सेवा खाते वाला पैनल खोलें.
  2. App Engine डिफ़ॉल्ट सेवा खाता चुनें. इसके बाद, दाईं ओर मौजूद विकल्प मेन्यू का इस्तेमाल करके, कुंजी बनाएं चुनें.
  3. जब कहा जाए, तब कुंजी के टाइप के लिए JSON चुनें. इसके बाद, बनाएं पर क्लिक करें.

कुंजी फ़ाइल सेव करने के बाद, एसडीके को शुरू करें:

// At the top of test/index.test.js
// Make sure to use values from your actual Firebase configuration
const test = require('firebase-functions-test')({
  databaseURL: 'https://PROJECT_ID.firebaseio.com',
  storageBucket: 'PROJECT_ID.firebasestorage.app',
  projectId: 'PROJECT_ID',
}, 'path/to/serviceAccountKey.json');

ऑफ़लाइन मोड में SDK टूल शुरू करना

अगर आपको पूरी तरह से ऑफ़लाइन टेस्ट लिखने हैं, तो बिना किसी पैरामीटर के SDK शुरू किया जा सकता है:

// At the top of test/index.test.js
const test = require('firebase-functions-test')();

कॉन्फ़िगरेशन वैल्यू को मॉक करना

अगर आपने अपने फ़ंक्शन कोड में functions.config() का इस्तेमाल किया है, तो कॉन्फ़िगरेशन वैल्यू को मॉक किया जा सकता है. उदाहरण के लिए, अगर functions/index.js में यह कोड शामिल है:

const functions = require('firebase-functions/v1');
const key = functions.config().stripe.key;

इसके बाद, अपनी टेस्ट फ़ाइल में वैल्यू को इस तरह से मॉक किया जा सकता है:

// Mock functions config values
test.mockConfig({ stripe: { key: '23wr42ewr34' }});

फ़ंक्शन इंपोर्ट करना

अपने फ़ंक्शन इंपोर्ट करने के लिए, require का इस्तेमाल करके अपनी मुख्य फ़ंक्शन फ़ाइल को मॉड्यूल के तौर पर इंपोर्ट करें. पक्का करें कि आपने firebase-functions-test को शुरू करने और कॉन्फ़िगरेशन की वैल्यू को मॉक करने के बाद ही ऐसा किया हो.

// after firebase-functions-test has been initialized
const myFunctions = require('../index.js'); // relative path to functions code

अगर आपने firebase-functions-test को ऑफ़लाइन मोड में शुरू किया है और आपके फ़ंक्शन कोड में admin.initializeApp() है, तो आपको फ़ंक्शन इंपोर्ट करने से पहले इसे स्टब करना होगा:

// If index.js calls admin.initializeApp at the top of the file,
// we need to stub it out before requiring index.js. This is because the
// functions will be executed as a part of the require process.
// Here we stub admin.initializeApp to be a dummy function that doesn't do anything.
adminInitStub = sinon.stub(admin, 'initializeApp');
// Now we can require index.js and save the exports inside a namespace called myFunctions.
myFunctions = require('../index');

बैकग्राउंड में चलने वाले फ़ंक्शन (नॉन-एचटीटीपी) की जांच करना

नॉन-एचटीटीपी फ़ंक्शन की टेस्टिंग के लिए, यह तरीका अपनाएं:

  1. जिस फ़ंक्शन की जांच करनी है उसे test.wrap तरीके से रैप करें
  2. जांच के लिए डेटा बनाना
  3. आपने जो टेस्ट डेटा बनाया है उसके साथ रैप किए गए फ़ंक्शन को कॉल करें. साथ ही, इवेंट के कॉन्टेक्स्ट वाले उन फ़ील्ड को कॉल करें जिन्हें आपको तय करना है.
  4. व्यवहार के बारे में दावे करना.

सबसे पहले, उस फ़ंक्शन को रैप करें जिसे आपको टेस्ट करना है. मान लें कि आपके पास functions/index.js में makeUppercase नाम का एक फ़ंक्शन है और आपको उसकी जांच करनी है. functions/test/index.test.js में यह लिखें

// "Wrap" the makeUpperCase function from index.js
const myFunctions = require('../index.js');
const wrapped = test.wrap(myFunctions.makeUppercase);

wrapped एक ऐसा फ़ंक्शन है जिसे कॉल करने पर, makeUppercase शुरू हो जाता है. wrapped दो पैरामीटर लेता है:

  1. data (ज़रूरी है): makeUppercase को भेजा जाने वाला डेटा. यह सीधे तौर पर, फ़ंक्शन हैंडलर को भेजे गए पहले पैरामीटर से मेल खाता है. firebase-functions-test, कस्टम डेटा या उदाहरण डेटा बनाने के तरीके उपलब्ध कराता है.
  2. eventContextOptions (ज़रूरी नहीं है): इवेंट के कॉन्टेक्स्ट के वे फ़ील्ड जिन्हें आपको तय करना है. इवेंट कॉन्टेक्स्ट, आपके लिखे गए फ़ंक्शन हैंडलर को भेजा गया दूसरा पैरामीटर होता है. wrapped को कॉल करते समय eventContextOptions पैरामीटर शामिल न करने पर भी, इवेंट का कॉन्टेक्स्ट जनरेट होता है. इसमें काम के फ़ील्ड शामिल होते हैं. यहां कुछ फ़ील्ड तय करके, जनरेट किए गए फ़ील्ड की वैल्यू बदली जा सकती है. ध्यान दें कि आपको सिर्फ़ वे फ़ील्ड शामिल करने हैं जिनकी वैल्यू बदलनी है. आपने जिन फ़ील्ड को ओवरराइड नहीं किया है उन्हें जनरेट किया जाता है.
const data =  // See next section for constructing test data

// Invoke the wrapped function without specifying the event context.
wrapped(data);

// Invoke the function, and specify params
wrapped(data, {
  params: {
    pushId: '234234'
  }
});

// Invoke the function, and specify auth and auth Type (for real time database functions only)
wrapped(data, {
  auth: {
    uid: 'jckS2Q0'
  },
  authType: 'USER'
});

// Invoke the function, and specify all the fields that can be specified
wrapped(data, {
  eventId: 'abc',
  timestamp: '2018-03-23T17:27:17.099Z',
  params: {
    pushId: '234234'
  },
  auth: {
    uid: 'jckS2Q0' // only for real time database functions
  },
  authType: 'USER' // only for real time database functions
});

जांच के लिए डेटा बनाना

रैप किए गए फ़ंक्शन का पहला पैरामीटर, टेस्ट डेटा होता है. इसका इस्तेमाल, बुनियादी फ़ंक्शन को चालू करने के लिए किया जाता है. टेस्ट डेटा बनाने के कई तरीके हैं.

कस्टम डेटा का इस्तेमाल करना

firebase-functions-test में, आपके फ़ंक्शन की जांच करने के लिए ज़रूरी डेटा बनाने के कई फ़ंक्शन होते हैं. उदाहरण के लिए, Firestore DocumentSnapshot बनाने के लिए, test.firestore.makeDocumentSnapshot का इस्तेमाल करें. पहला तर्क डेटा है और दूसरा तर्क पूरा रेफ़रंस पाथ है. साथ ही, स्नैपशॉट की अन्य प्रॉपर्टी के लिए तीसरा तर्क (ज़रूरी नहीं) होता है.

// Make snapshot
const snap = test.firestore.makeDocumentSnapshot({foo: 'bar'}, 'document/path');
// Call wrapped function with the snapshot
const wrapped = test.wrap(myFunctions.myFirestoreDeleteFunction);
wrapped(snap);

अगर आपको onUpdate या onWrite फ़ंक्शन की जांच करनी है, तो आपको दो स्नैपशॉट बनाने होंगे: एक पहले की स्थिति के लिए और दूसरा बाद की स्थिति के लिए. इसके बाद, इन स्नैपशॉट की मदद से Change ऑब्जेक्ट बनाने के लिए, makeChange तरीके का इस्तेमाल किया जा सकता है.

// Make snapshot for state of database beforehand
const beforeSnap = test.firestore.makeDocumentSnapshot({foo: 'bar'}, 'document/path');
// Make snapshot for state of database after the change
const afterSnap = test.firestore.makeDocumentSnapshot({foo: 'faz'}, 'document/path');
const change = test.makeChange(beforeSnap, afterSnap);
// Call wrapped function with the Change object
const wrapped = test.wrap(myFunctions.myFirestoreUpdateFunction);
wrapped(change);

अन्य सभी डेटा टाइप के लिए, इसी तरह के फ़ंक्शन देखने के लिए एपीआई रेफ़रंस देखें.

उदाहरण के तौर पर दिए गए डेटा का इस्तेमाल करना

अगर आपको अपने टेस्ट में इस्तेमाल किए गए डेटा में बदलाव नहीं करना है, तो firebase-functions-test हर फ़ंक्शन टाइप के लिए उदाहरण डेटा जनरेट करने के तरीके उपलब्ध कराता है.

// For Firestore onCreate or onDelete functions
const snap = test.firestore.exampleDocumentSnapshot();
// For Firestore onUpdate or onWrite functions
const change = test.firestore.exampleDocumentSnapshotChange();

हर फ़ंक्शन टाइप के लिए उदाहरण डेटा पाने के तरीकों के बारे में जानने के लिए, एपीआई का संदर्भ देखें.

स्टब किए गए डेटा का इस्तेमाल करना (ऑफ़लाइन मोड के लिए)

अगर आपने एसडीके को ऑफ़लाइन मोड में शुरू किया है और Cloud Firestore या Realtime Database फ़ंक्शन की जांच कर रहे हैं, तो आपको असली DocumentSnapshot या DataSnapshot बनाने के बजाय, स्टब के साथ एक सामान्य ऑब्जेक्ट का इस्तेमाल करना चाहिए.

मान लें कि आपको इस फ़ंक्शन के लिए यूनिट टेस्ट लिखना है:

// Listens for new messages added to /messages/:pushId/original and creates an
// uppercase version of the message to /messages/:pushId/uppercase
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
    .onCreate((snapshot, context) => {
      // Grab the current value of what was written to the Realtime Database.
      const original = snapshot.val();
      functions.logger.log('Uppercasing', context.params.pushId, original);
      const uppercase = original.toUpperCase();
      // You must return a Promise when performing asynchronous tasks inside a Functions such as
      // writing to the Firebase Realtime Database.
      // Setting an "uppercase" sibling in the Realtime Database returns a Promise.
      return snapshot.ref.parent.child('uppercase').set(uppercase);
    });

फ़ंक्शन में, snap का इस्तेमाल दो बार किया गया है:

  • snap.val()
  • snap.ref.parent.child('uppercase').set(uppercase)

टेस्ट कोड में, एक सामान्य ऑब्जेक्ट बनाएं. इसमें ये दोनों कोड पाथ काम करेंगे. साथ ही, तरीकों को स्टब करने के लिए Sinon का इस्तेमाल करें.

// The following lines creates a fake snapshot, 'snap', which returns 'input' when snap.val() is called,
// and returns true when snap.ref.parent.child('uppercase').set('INPUT') is called.
const snap = {
  val: () => 'input',
  ref: {
    parent: {
      child: childStub,
    }
  }
};
childStub.withArgs(childParam).returns({ set: setStub });
setStub.withArgs(setParam).returns(true);

दावे करना

एसडीके को शुरू करने, फ़ंक्शन को रैप करने, और डेटा बनाने के बाद, बनाए गए डेटा के साथ रैप किए गए फ़ंक्शन को कॉल किया जा सकता है. साथ ही, व्यवहार के बारे में दावे किए जा सकते हैं. इन दावों को करने के लिए, Chai जैसी लाइब्रेरी का इस्तेमाल किया जा सकता है.

ऑनलाइन मोड में दावे करना

अगर आपने ऑनलाइन मोड में Cloud Functions के लिए Firebase Test SDK को शुरू किया है, तो firebase-admin SDK टूल का इस्तेमाल करके यह पुष्टि की जा सकती है कि ज़रूरी कार्रवाइयां (जैसे कि डेटाबेस में लिखना) हो गई हैं.

नीचे दिए गए उदाहरण में यह दावा किया गया है कि 'INPUT' को टेस्ट प्रोजेक्ट के डेटाबेस में लिखा गया है.

// Create a DataSnapshot with the value 'input' and the reference path 'messages/11111/original'.
const snap = test.database.makeDataSnapshot('input', 'messages/11111/original');

// Wrap the makeUppercase function
const wrapped = test.wrap(myFunctions.makeUppercase);
// Call the wrapped function with the snapshot you constructed.
return wrapped(snap).then(() => {
  // Read the value of the data at messages/11111/uppercase. Because `admin.initializeApp()` is
  // called in functions/index.js, there's already a Firebase app initialized. Otherwise, add
  // `admin.initializeApp()` before this line.
  return admin.database().ref('messages/11111/uppercase').once('value').then((createdSnap) => {
    // Assert that the value is the uppercased version of our input.
    assert.equal(createdSnap.val(), 'INPUT');
  });
});

ऑफ़लाइन मोड में पुष्टि करना

फ़ंक्शन की अनुमानित रिटर्न वैल्यू के बारे में दावे किए जा सकते हैं:

const childParam = 'uppercase';
const setParam = 'INPUT';
// Stubs are objects that fake and/or record function calls.
// These are excellent for verifying that functions have been called and to validate the
// parameters passed to those functions.
const childStub = sinon.stub();
const setStub = sinon.stub();
// The following lines creates a fake snapshot, 'snap', which returns 'input' when snap.val() is called,
// and returns true when snap.ref.parent.child('uppercase').set('INPUT') is called.
const snap = {
  val: () => 'input',
  ref: {
    parent: {
      child: childStub,
    }
  }
};
childStub.withArgs(childParam).returns({ set: setStub });
setStub.withArgs(setParam).returns(true);
// Wrap the makeUppercase function.
const wrapped = test.wrap(myFunctions.makeUppercase);
// Since we've stubbed snap.ref.parent.child(childParam).set(setParam) to return true if it was
// called with the parameters we expect, we assert that it indeed returned true.
return wrapped(snap).then(makeUppercaseResult => {
  return assert.equal(makeUppercaseResult, true);
});

Sinon spies का इस्तेमाल यह पुष्टि करने के लिए भी किया जा सकता है कि कुछ तरीकों को कॉल किया गया है और वे आपके अनुमान के मुताबिक पैरामीटर के साथ कॉल किए गए हैं.

एचटीटीपी फ़ंक्शन की जांच करना

एचटीटीपी onCall फ़ंक्शन की जांच करने के लिए, वही तरीका अपनाएं जो बैकग्राउंड फ़ंक्शन की जांच करने के लिए इस्तेमाल किया जाता है.

अगर आपको onRequest फ़ंक्शन के एचटीटीपी वर्शन की जांच करनी है, तो आपको firebase-functions-test if का इस्तेमाल करना चाहिए:

  • functions.config() का इस्तेमाल किया जाता है
  • आपका फ़ंक्शन, Firebase प्रोजेक्ट या अन्य Google API के साथ इंटरैक्ट करता है. साथ ही, आपको अपनी जांचों के लिए, असली Firebase प्रोजेक्ट और उसके क्रेडेंशियल का इस्तेमाल करना है.

एचटीटीपी onRequest फ़ंक्शन में दो पैरामीटर होते हैं: अनुरोध ऑब्जेक्ट और जवाब ऑब्जेक्ट. addMessage() उदाहरण फ़ंक्शन को इस तरह से टेस्ट किया जा सकता है:

  • जवाब ऑब्जेक्ट में रीडायरेक्ट फ़ंक्शन को बदलें, क्योंकि sendMessage() इसे कॉल करता है.
  • रीडायरेक्ट फ़ंक्शन में, chai.assert का इस्तेमाल करें. इससे यह पुष्टि करने में मदद मिलती है कि रीडायरेक्ट फ़ंक्शन को किन पैरामीटर के साथ कॉल किया जाना चाहिए:
// A fake request object, with req.query.text set to 'input'
const req = { query: {text: 'input'} };
// A fake response object, with a stubbed redirect function which asserts that it is called
// with parameters 303, 'new_ref'.
const res = {
  redirect: (code, url) => {
    assert.equal(code, 303);
    assert.equal(url, 'new_ref');
    done();
  }
};

// Invoke addMessage with our fake request and response objects. This will cause the
// assertions in the response object to be evaluated.
myFunctions.addMessage(req, res);

टेस्ट क्लीनअप

अपने टेस्ट कोड के आखिर में, क्लीनअप फ़ंक्शन को कॉल करें. इससे, एसडीके के शुरू होने पर सेट किए गए एनवायरमेंट वैरिएबल हट जाते हैं. साथ ही, अगर आपने एसडीके का इस्तेमाल करके रीयल टाइम डेटाबेस DataSnapshot या Firestore DocumentSnapshot बनाया है, तो इससे बनाए गए Firebase ऐप्लिकेशन मिट जाते हैं.

test.cleanup();

पूरे उदाहरण देखें और ज़्यादा जानें

Firebase GitHub repository पर जाकर, सभी उदाहरण देखे जा सकते हैं.

ज़्यादा जानने के लिए, firebase-functions-test के एपीआई रेफ़रंस देखें.