Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problems with Unit test for googleApi #1511

Closed
odaymard opened this issue Dec 19, 2018 · 1 comment
Closed

problems with Unit test for googleApi #1511

odaymard opened this issue Dec 19, 2018 · 1 comment
Assignees
Labels
type: question Request for information or clarification. Not an issue.

Comments

@odaymard
Copy link

I have the following code and I am trying to create unit test
to make sure sendToGoogle function is working so I Stub gmail.users.messages's send method and expect the method to be called by sendToGoogle method but I got this error

AssertionError: expected send to have been called at least once, but
it was never called

//email.js

 const helper = require('./helper')

 /**
  * sendEmail - sends an email to a given address
  *
  * @param {String} to - The address of the recipient.
  * @param {String} from - The address of the sender.
  * @param {String} subject - subject of the email.
  * @param {String} bodyText - text of the email.
  **/
function sendEmail(to, from, subject, bodyText) {
  const oAuthClient = helper.getAuth(process.env.CLIENT_ID, process.env.PRIVATE_KEY, from);
  const emailLines = helper.createEmail(to, from, subject, bodyText);
  helper.sendToGoogle(oAuthClient, from, emailLines);
}

 module.exports.sendEmail = sendEmail;

here is helper.js which contains my code

//helper.js

const {google} = require('googleapis');
const base64 = require('base-64');
const gmail = google.gmail("v1");

 /**
  * createEmail - createEmail an email message
  *
  * @param {String} to - The address of the recipient.
  * @param {String} from - The address of the sender.
  * @param {String} subject - subject of the email.
  * @param {String} bodyText - text of the email.
  * @return {String} - Message to send.
  **/

const createEmail = function (to, from, subject, bodyText) {
  const emailLines = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
    "MIME-Version: 1.0\n",
    "Content-Transfer-Encoding: 7bit\n",
    "to: ", to, "\n",
    "from: ", from, "\n",
    "subject: ", subject, "\n\n",
    bodyText
  ].join('')
  const messageBase64 = base64.encode(emailLines.trim()).replace(/\+/g, '-').replace(/\//g, '_');
  return messageBase64
}
  /**
   *
   *
   * @param {String} clientKey
   * @param {String} privateKey
   * @param {String} from
   * @returns
   */
  const getAuth = function (clientKey, privateKey, from) {
    return new google.auth.JWT(
      clientKey,
      null,
      privateKey,
      ['https://www.googleapis.com/auth/gmail.send'],
      from
    );
  }
   /**
    * @param {String} oAuthClient
    * @param {String} from
    * @param {String} message
    */
   const sendToGoogle = function (oAuthClient, from, message) {
     console.log("sendtogoogle calllled")
     gmail.users.messages.send({
       auth: oAuthClient,
       userId: from,
       resource: {
         raw: message
       }
     }, function (err, resp) {
       if (!err) {
         return resp
       }
       console.error(err)

     });
   }
  module.exports.createEmail = createEmail
  module.exports.getAuth = getAuth
  module.exports.sendToGoogle = sendToGoogle

and this is the unittest file

//email.spec.js

 it ("send to gmail",function(){
   const gmail = google.gmail("v1");
   const SendStub = sinon.stub(gmail.users.messages, 'send').returns('test')
   const result = helper.sendToGoogle("oAuthClient", from,"message")
   SendStub.should.have.been.called
  })

Your help is appreciated.

@JustinBeckwith JustinBeckwith added triage me I really want to be triaged. type: question Request for information or clarification. Not an issue. and removed triage me I really want to be triaged. labels Dec 20, 2018
@grant
Copy link
Contributor

grant commented Jan 30, 2019

@odaymard This GitHub repo isn't the best place to ask general questions.
Can you try using StackOverflow?


AssertionError: expected send to have been called at least once, but it was never called

This looks like a unit testing framework error.

Let us know how we can help with the Node client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants