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

इस पेज पर आपके लिए, यूनिट टेस्ट लिखने के सबसे सही तरीकों और टूल के बारे में बताया गया है फ़ंक्शन, जैसे कि वे टेस्ट जो कंटिन्यूअस इंटिग्रेशन (सीआई) का हिस्सा होंगे सिस्टम. टेस्टिंग को आसान बनाने के लिए, Firebase, Cloud Functions के लिए Firebase टेस्ट SDK टूल उपलब्ध कराता है. यह एनपीएम पर firebase-functions-test के तौर पर डिस्ट्रिब्यूट किया जाता है. साथ ही, यह कंपैनियन टेस्ट एसडीके है firebase-functions तक. 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. दाएं पैनल में, कॉन्फ़िगरेशन फ़ाइल डाउनलोड करने का विकल्प चुनें 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. व्यवहार के बारे में दावे करना.

पहले उस फ़ंक्शन को रैप करें जिसे आपको टेस्ट करना है. मान लें कि आपके पास makeUppercase नाम का functions/index.js, जिसकी आपको जांच करनी है. मेरी टीम को 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 अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है 2 पैरामीटर लेता है:

  1. data (ज़रूरी है): makeUppercase को भेजा जाने वाला डेटा. यह सीधे आपके फ़ंक्शन हैंडलर को भेजे गए पहले पैरामीटर से मेल खाता है लिखा गया. firebase-functions-test कस्टम डेटा या उदाहरण डेटा.
  2. eventContextOptions (ज़रूरी नहीं): इवेंट कॉन्टेक्स्ट के वे फ़ील्ड जिन्हें करना है. इवेंट का संदर्भ, फ़ंक्शन हैंडलर जो आपने लिखा है. eventContextOptions को शामिल न करने पर wrapped को कॉल करते समय पैरामीटर जोड़ने के बाद भी इवेंट संदर्भ जनरेट होता है को अनुमति दें. जनरेट किए गए कुछ फ़ील्ड को इस तरीके से बदला जा सकता है: और इस बारे में ज़्यादा जानकारी मिलेगी. ध्यान दें कि आपको सिर्फ़ वे फ़ील्ड शामिल करने होंगे जो को बदलना है. आपने जिन फ़ील्ड को ओवरराइड नहीं किया है वे जनरेट हो जाते हैं.
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 में कई फ़ंक्शन हैं का इस्तेमाल करें. उदाहरण के लिए, test.firestore.makeDocumentSnapshot इस्तेमाल करें एक Firestore DocumentSnapshot बनाने के लिए. पहला तर्क डेटा है और दूसरा तर्क पूरा रेफ़रंस पाथ होता है और यहां तीसरा तर्क (ज़रूरी नहीं) की अन्य प्रॉपर्टी के लिए इसका इस्तेमाल किया जा सकता है.

// 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.asrt का इस्तेमाल करें इससे यह पता लगाने में मदद मिलती है कि रीडायरेक्ट फ़ंक्शन को कौनसे पैरामीटर इस्तेमाल करने चाहिए इससे कॉल किया जा सकता है:
// 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.

test.cleanup();

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

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

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