簡體   English   中英

如何讓 Mocha 在測試中失敗

[英]How to get Mocha to fail a test

我有以下測試:

it.only('validation should fail', function(done) {
    var body = {
        title: "dffdasfsdfsdafddfsadsa",
        description: "Postman Description",
        beginDate: now.add(3, 'd').format(),
        endDate: now.add(4, 'd').format()
    }


    var rules = eventsValidation.eventCreationRules();
    var valMessages = eventsValidation.eventCreationMessages();

    indicative
        .validateAll(rules, body, valMessages)
        .then(function(data) {
            console.log("SHOULD NOT GET HERE");
            should.fail("should not get here");
            done();

        })
        .catch(function(error) {
            console.log("SHOULD GET HERE");
            console.log(error);
        });
    done();
});

測試執行路徑正確。 當我驗證數據時,它會顯示“不應該到達這里”。 測試實際上是為了確保它沒有。 當我輸入非驗證數據時,代碼會轉到“應該到達這里”。 所以驗證規則有效。

我想要做的是確保當我有錯誤的驗證數據時測試失敗並且它驗證。 但是,當我使用良好的數據運行它時,它會驗證,運行失敗,但 mocha 仍將其標記為通過。 如果執行達到“不應該到達這里”,我希望它失敗。

我試過 throw new Error("fail"); 也沒有運氣。 在這兩種情況下,它實際上似乎也在 .catch 塊中運行代碼。

有什么建議? 我在一個類似的問題中找到了解決方案。 寫這個問題是因為這些解決方案似乎對我不起作用。

您可以調用assert.fail

it("should return empty set of tags", function()
{
    assert.fail("actual", "expected", "Error message");
});

此外,如果您使用參數調用done()函數,Mocha 會認為測試失敗。

例如:

it("should return empty set of tags", function(done)
{
    done(new Error("Some error message here"));
});

雖然第一個對我來說看起來更清晰。

在 ES2017 async / await世界中, chai-as-promised不需要那么多。 盡管簡單的拒絕是一個地方chai-as-promised仍然使用起來更整潔。

如果您想更詳細地測試錯誤,則需要catch

it.only('validation should fail', async function(){
    let body = { ... }
    let rules = eventsValidation.eventCreationRules()
    let valMessages = eventsValidation.eventCreationMessages()

    try {
        await indicative.validateAll(rules, body, valMessages)
    } catch (error) {
        expect(error).to.be.instanceOf(Error)
        expect(error.message).to.match(/Oh no!/)
        return
    }
    expect.fail(null, null, 'validateAll did not reject with an error')
    // or throw new Error('validateAll did not reject with an error')
})

async / await需要Node.js 7.6+或像Babel這樣的編譯器

使用chai-as-promised和原生 Mocha 承諾處理程序。

var chai = require('chai').use(require('chai-as-promised'));
var should = chai.should(); // This will enable .should for promise assertions

你不再需要done ,只需返回承諾。

// Remove `done` from the line below
it.only('validation should fail', function(/* done */) {
    var body = {
        title: "dffdasfsdfsdafddfsadsa",
        description: "Postman Description",
        beginDate: now.add(3, 'd').format(),
        endDate: now.add(4, 'd').format()
    }

    var rules = eventsValidation.eventCreationRules();
    var valMessages = eventsValidation.eventCreationMessages();

    // Return the promise
    return indicative
        .validateAll(rules, body, valMessages)
        .should.be.rejected; // The test will pass only if the promise is rejected

    // Remove done, we no longer need it
    // done();
});

這個簡單的投擲對我有用

describe('Lead', () => {
  it('should create a new lead', async () => {
     throw 'not implemented'
  })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM