Question: Hi, I wrote this code for this project, I just need help writing the three tests under the testing your project part which is below
Hi, I wrote this code for this project, I just need help writing the three tests under the "testing your project" part which is below in the images, the tests are the following
- there should be 3 different unit tests that run using mocks and spies: 1 -the function that actually sends emails gets called 2- it gets called once, not twice 3 -it is called correctly, with the correct arguments
- More is written in detail under "Testing your project" in the images below
- The whole project is explained in the images below
- This is a project that sends emails, it is about demonstrating understanding of mocks/spies
- Under "Instructions for reviewer" there is more information about how the tests should be
import os import smtplib import datetime as dt import random import sys from dotenv import load_dotenv # Loading environmental variables load_dotenv() SENDER = sys.argv[1] # SENDER = os.getenv('SENDER') PASSWORD = os.getenv('PASSWORD') # Accounts receivers RECEIVER = os.getenv('RECEIVERS') # SMTP server options HOST = os.getenv('HOST') PORT = os.getenv('PORT') def email_random(): now = dt.datetime.now() weekday = now.weekday() if weekday == 3: with open("quotes.txt") as quote_file: all_quotes = quote_file.readlines() quote = random.choice(all_quotes) with smtplib.SMTP("smtp.mail.com") as connection: connection.starttls() connection.login(SENDER, PASSWORD) connection.sendmail( from_addr=SENDER, to_addrs=RECEIVER, msg=f"Subject:Thursday Motivation {quote}" ) return quote email_random()


I used .env file inplace of smtp_secrets.sh file for environment variables that we need to configure the SMTP client.:
cat .env #!/bin/sh
export SMTP_SERVER=smtp.gimail.com export SMTP_PORT=587 export SMTP_LOGIN= export SMTP_PASSWORD=
I don't know if I provided enough information?
Write a program that sends a random inspirational quote to an email address. The email address should be a command-line parameter passed to the program. You should have a list of quotes in a file by itself. Your program should grab one and send the email. Your final email quotes should be formatted like this: "The only true measure of success if the number of people you have helped" Ray Dalio Node After npm init you should add your own script to package.json. Look for scripts inside the file. you should be able to do the following: npm run send_inspiration ... The command needs to allow you to pass in an email address from the command line. Take a look at this Also note that good code is written to be reusable. Make sure all your executable code is inside useful functions. When we run npm run send_inspiration then the right functions will need to be executed. Python You should be able to run your code using python send_inspiration.py ... The command needs to allow you to pass in an email address from the command line. Take a look at this Go with the simplest option you can find initially - because KISS Also note that good code is written to be reusable. Make sure all your executable code is inside useful functions. When we run npm run send_inspiration then the Also note that good code is written to be reusable. Make sure all your executable code is inside useful functions. When we run npm run send_inspiration then the right functions will need to be executed. Testing your project Your unit tests should make sure that when the application runs then it sends one email with one quote to one person. If your tests send actual emails require your smtp secret values then they are wrong. Please make sure that you understand what mocks and spies are for when writing your tests. These things do not exist to take up space, waste time or look fancy. They do have a purpose. If you do anything that looks like the following pseudocode then you are doing it wrong: spyon (myFunction) myFunction() assert myFunction wasCalleonce Here is a useful way to think about mocks and spies: Basically, sending emails is considered "expensive". Why? Because it costs actual money if you do it in bulk. Also it costs time. It is not a fast and free operation. But it is an important operation. You need to test that when you want to send an email then: 1. the function that actually sends emails gets called 2. it gets called once, not twice 3. it is called correctly, with the correct arguments You don't want your unit tests to actually send emails. You just want them to prove that the part of your machine that sends emails is being used correctly. Instructions for reviewer Unit tests: The learner should demonstrate that the email sends only once, with the correct arguments. The learner should demonstrate an understanding of mocks and spies. Please see TOPICS: Unit testing with mocks and spies. Sending emails to multiple recipients is not a requirement, but if the code is structured well enough, then doing so should be easy. The unit test should demonstrate that the code in not restricted to sending just one email. Write a program that sends a random inspirational quote to an email address. The email address should be a command-line parameter passed to the program. You should have a list of quotes in a file by itself. Your program should grab one and send the email. Your final email quotes should be formatted like this: "The only true measure of success if the number of people you have helped" Ray Dalio Node After npm init you should add your own script to package.json. Look for scripts inside the file. you should be able to do the following: npm run send_inspiration ... The command needs to allow you to pass in an email address from the command line. Take a look at this Also note that good code is written to be reusable. Make sure all your executable code is inside useful functions. When we run npm run send_inspiration then the right functions will need to be executed. Python You should be able to run your code using python send_inspiration.py ... The command needs to allow you to pass in an email address from the command line. Take a look at this Go with the simplest option you can find initially - because KISS Also note that good code is written to be reusable. Make sure all your executable code is inside useful functions. When we run npm run send_inspiration then the Also note that good code is written to be reusable. Make sure all your executable code is inside useful functions. When we run npm run send_inspiration then the right functions will need to be executed. Testing your project Your unit tests should make sure that when the application runs then it sends one email with one quote to one person. If your tests send actual emails require your smtp secret values then they are wrong. Please make sure that you understand what mocks and spies are for when writing your tests. These things do not exist to take up space, waste time or look fancy. They do have a purpose. If you do anything that looks like the following pseudocode then you are doing it wrong: spyon (myFunction) myFunction() assert myFunction wasCalleonce Here is a useful way to think about mocks and spies: Basically, sending emails is considered "expensive". Why? Because it costs actual money if you do it in bulk. Also it costs time. It is not a fast and free operation. But it is an important operation. You need to test that when you want to send an email then: 1. the function that actually sends emails gets called 2. it gets called once, not twice 3. it is called correctly, with the correct arguments You don't want your unit tests to actually send emails. You just want them to prove that the part of your machine that sends emails is being used correctly. Instructions for reviewer Unit tests: The learner should demonstrate that the email sends only once, with the correct arguments. The learner should demonstrate an understanding of mocks and spies. Please see TOPICS: Unit testing with mocks and spies. Sending emails to multiple recipients is not a requirement, but if the code is structured well enough, then doing so should be easy. The unit test should demonstrate that the code in not restricted to sending just one email
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
