【问题标题】:Mocha test - solving assertion_error摩卡测试 - 解决 assertion_error
【发布时间】:2019-04-30 03:34:43
【问题描述】:

我正在练习使用 Mocha 和课程。我正在编写一些成功的测试,直到我遇到了一个断言错误,这是我仍然不太熟悉的东西。

我的 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)

基本上,我试图弄清楚为什么我会收到断言错误并修复它。

【问题讨论】:

    标签: javascript mocha.js tdd assert


    【解决方案1】:

    如果你这样做

    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")

    【讨论】:

    • payment 类的构造函数接受三个参数,顺序为name、card、price。因此,如果您将单个值传递给构造函数,那么它将转到名称变量。所以请保持你脑海中使用的论点的顺序
    • 感谢您的意见。似乎在我测试的第一部分(应该处理付款)中,我包含了 3 个参数。我停止使用 3 个参数,一切正常,直到“应该返回价格”测试。我会让你知道这行得通。
    • 测试成功。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    相关资源
    最近更新 更多