Тест мокко - решение assertion_error - PullRequest
0 голосов
/ 30 апреля 2019

Я практикую работу с мокко и классами.Я писал несколько тестов, которые были успешными, пока я не столкнулся с ошибкой утверждения, с которой я все еще не слишком знаком.

Мой файл test.js выглядит следующим образом, начиная с пары классов:

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;}
  }
}

Далее я начинаю свои тесты:

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);
  });
}

На этом этапе я могу запустить команду "mocha test" и начать тестирование.

Как указановыше, я имел успех.Затем я получил следующее сообщение:

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)

По сути, я пытаюсь выяснить, почему я получаю ошибку подтверждения и исправляю ее.

1 Ответ

1 голос
/ 30 апреля 2019

Если вам так нравится

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

, в качестве результата всегда возвращается false.Потому что когда вы вызываете x = new Payment("50.00"), он создает {name:"50.00", card : undefined, price:undefined}.Итак, ясно видно, что ценового элемента нет.Поэтому он возвращает ложь.Вот почему ваше утверждение провалилось, я думаю.

Если вы хотите установить только элемент цены, то вы можете сделать либо x = new Payment(null,null,"50.00"), либо x = new Payment("","","50.00")

...