Search code examples
javascriptmocha.jstddassert

Mocha test - solving assertion_error


I am practising working with Mocha and classes. I was writing a few tests that were successful until I hit a an assertion error, which is something I am still not too familiar with.

My test.js file is as follows, starting with a couple of classes:

var assert = require('assert');

class Payment 
{
  constructor (name,card,price) 
  {
  this.name = name;
  this.card = card;
  this.price = price;
  }
}

class BillingService 
{
  processPayment(payment) 
  {
    if(payment.card == "123"){return true;}
    else{return false;}
  }

  checkName(payment)
  {
    if(payment.name == "John B"){return true;}
    else{return false;}
  }

  checkTotal(payment)
  {
    if(payment.price == "50.00"){return true;}
    else{return false;}
  }
}

Next, I begin my tests:

describe('BillingService', function() 
{
  it('should process payment', function() 
  {
    var x = new Payment("John B", "123", "50.00");
    var billingService = new BillingService();
    var outcome = billingService.processPayment(x);
    assert.equal(true, outcome);
  });

  it('should not process payment', function() 
  { 
    var x = new Payment("John B", "xf23", "50.00");
    var billingService = new BillingService();
    var outcome = billingService.processPayment(x);
    assert.equal(false, outcome);
  });

  it('should get name', function()
  {
    var x = new Payment("John B");
    var billingService = new BillingService();
    var outcome = billingService.checkName(x);
    assert.equal(true, outcome);
  });

  it('should not get name', function()
  {
    var x = new Payment("Scarlett");
    var billingService = new BillingService();
    var outcome = billingService.checkName(x);
    assert.equal(false, outcome);
  });

  it('should return price', function()
  {
    var x = new Payment("50.00");
    var billingService = new BillingService();
    var outcome = billingService.checkTotal(x);
    assert.equal(true, outcome);
  });

  it('should not return price', function()
  {
    var x = new Payment("23.00");
    var billingService = new BillingService();
    var outcome = billingService.checkTotal(x);
    assert.equal(false, outcome);
  });
}

At this point, I can run the "mocha test" command and begin testing.

As stated above, I was having success. I then received the following message:

BillingService
✓ should process payment
✓ should not process payment
✓ should get name
✓ should not get name
1) should return price
✓ should not return price

5 passing (11ms)
1 failing

1) BillingService
   should return price:

  AssertionError [ERR_ASSERTION]: true == false
  + expected - actual

  -true
  +false

  at Context.<anonymous> (test.js:130:12)

Basically, I'm trying to figure out why I'm getting the assertion error and fix it.


Solution

  • If you do like this

    it('should return price', function()
    {
    var x = new Payment("50.00");
    var billingService = new BillingService();
    var outcome = billingService.checkTotal(x);
    assert.equal(true, outcome);
    });
    

    it always return false as outcome. Because when you called x = new Payment("50.00"), it creates {name:"50.00", card : undefined, price:undefined}. So clearly we can see that there is no price element. Therefore it returns false. That's why your assertion failed I think.

    If you want to set only the price element, then you can do as either x = new Payment(null,null,"50.00") or x = new Payment("","","50.00")