Cloud फ़ंक्शन की यूनिट टेस्टिंग

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

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

सेटअप की जांच करें

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

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 टेस्ट SDK टूल शुरू करना

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

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

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

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

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

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

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

मुख्य फ़ाइल बनाने के लिए:

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

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

// At the top of test/index.test.js
const test = require('firebase-functions-test')({
  databaseURL: 'https://my-project.firebaseio.com',
  storageBucket: 'my-project.appspot.com',
  projectId: 'my-project',
}, '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');
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();

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

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

अगर आपने ऑफ़लाइन मोड में SDK टूल शुरू किया है और Cloud Firestore या रीयलटाइम डेटाबेस फ़ंक्शन की जांच की है, तो आपको असल 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);

दावे करना

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

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

अगर आपने ऑनलाइन मोड में Cloud Functions के लिए, Firebase टेस्ट 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 assert.equal(wrapped(snap), true);

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

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

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

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

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

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

  • रिस्पॉन्स ऑब्जेक्ट में रीडायरेक्ट फ़ंक्शन को बदलें, क्योंकि sendMessage() इसे कॉल करता है.
  • रीडायरेक्ट फ़ंक्शन में, chai.ACErt का इस्तेमाल करके ये तय करें कि रीडायरेक्ट फ़ंक्शन को किन पैरामीटर के साथ कॉल किया जाना चाहिए:
// 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);

क्लीनअप की जांच करें

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

test.cleanup();

सभी उदाहरण देखें और ज़्यादा जानें

Firebase GitHub रिपॉज़िटरी में सभी उदाहरण देखे जा सकते हैं.

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