Write a unit test for addInventory(). Call redSweater.addInventory() with parameter sweaterShipment. Print the shown error if the subsequent quantity is incorrect. Sample output for failed unit test given initial quantity is 10 and sweaterShipment is 50:
Beginning tests.
UNIT TEST FAILED: addInventory()
Tests complete.
Note: UNIT TEST FAILED is preceded by 3 spaces.

#include
using namespace std;

class InventoryTag {
public:
InventoryTag();
int getQuantityRemaining() const;
void addInventory(int numItems);

private:
int quantityRemaining;
};

InventoryTag::InventoryTag() {
quantityRemaining = 0;
}

int InventoryTag::getQuantityRemaining() const {
return quantityRemaining;
}

void InventoryTag::addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
}

int main() {
InventoryTag redSweater;
int sweaterShipment = 0;
int sweaterInventoryBefore = 0;

sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = 25;

cout << "Beginning tests." << endl;

// FIXME add unit test for addInventory

/* Your solution goes here */

cout << "Tests complete." << endl;

return 0;
}

Respuesta :

Answer:

redSweater.addInventory(sweaterShipment);

if (redSweater.getQuantityRemaining() != sweaterShipment + sweaterInventoryBefore) {

System.out.println("   UNIT TEST FAILED: addInventory()");

}

Explanation:

The if statement sets a condition for the unit test. If the condition isn't met then it prints the unit failed statement. Also, the answer above is kinda right but has lots of extra stuff in the code. Remember to have 3 spaces before the unit failed statement, 1 test will pass but the last one won't.