2016-07-26 10 views
0

Verwenden von Band, wie kann ich eine benutzerdefinierte Assertion-Methode anstelle von schreiben t.equal()? Oder gibt es eine Test-Assertion-Methode, die nach einer Teilzeichenfolge suchen kann, so dass die gesamte Zeichenfolge, die ich testen möchte, nicht wörtlich mit t.deepEqual() verglichen werden muss?Wie schreibe ich eine benutzerdefinierte Zusicherung für das Testen von Knoten oder Javascript mit Band, oder prüfen Sie auf Teilstring statt t.deepEquals()?

var test = require("tape") 

test('messages contain key words', function (t) { 
    // this is what I'm using: 
    t.equal(MyEncode(Fruit).indexOf('eat more') > -1,true,'should contain "eat more"') 

    // this is what I want: 
    t.contains(myEncode(Fruit),'eat more','should contain "eat more"') 
    t.end() 
}) 

Wenn ich myEncode testen, kann ich sehen, dass die Zeichenfolge nicht den Teil enthalten, aber ich kann den tatsächlichen Wert nicht anzeigen, da sie nur als falsch auswertet, die nicht informativ:

not ok 1 should contain "eat more" 
--- 
    operator: equal 
    expected: true 
    actual: false 
    at: checkCmd (/test.js:63:11) 
... 

Nach dem Lesen der obigen Testausgabe weiß ich nicht, ob mein Test zu restriktiv war oder ob die Ausgabe tatsächlich falsch war. Stattdessen würde Ich mag den tatsächlichen Wert von myEncode zurück, um zu sehen zu beschleunigen Lokalisieren des Problems:

not ok 2 should contain "eat more" 
--- 
    operator: contains 
    expected: "eat more" 
    actual: "Apples are allowed to be eaten on weekdays and weekends" 
    at: checkCmd (/test.js:66:11) 
... 
+0

Ich habe [extend-tape] installiert (https://www.npmjs.com/package/extend-tape), aber es stellt sich heraus, dass es die "Import" -Funktion erfordert, die noch nicht von V8 unterstützt wird 'Band von 'Band' importieren; import addAssertions von 'extend-tape'; 'hat nicht funktioniert. – user6641586

Antwort

0

Ich habe es dank zu arbeiten, beispielsweise Testverfahren in der Github tape Ausgabe How to define new test methods? #154 @mbostock.

Funktion in einer anderen Datei getestet, in der Regel wird (zB appToTest.js)

function brownFox(argument) { 
    return "The "+argument+" fox jumped" 
} 
exports.brownFox = brownFox 

Das Testskript:

// Test module typically named test.js 
var test = require("tape") 

var f = require("./appToTest") 
// regular built-in test 

test('String must match exactly', function (t) { // can be too specific 
    t.deepEqual(f.brownFox('quick brown'), "The quick brown fox jumped") // should pass 
    t.deepEqual(f.brownFox('quick black'), "The quick brown fox jumped") // will fail 
    t.deepEqual(f.brownFox('quick white'), "The quick white fox jumped") // should pass 
    t.comment('Strings must be fully specified to match word for word') 
    t.end() 
}) 
// It can be too tedious to maintain the test string to always 
// match exactly the string in the code being tested 

// I don't care what the fox is as long as it jumped 
// Since there is no "contains" test in tape, I hack t.equal() 
test('String should contain a fox', function (t) { 
    // does not show actual value when test fails; shows true or false instead 
    // "jumped" must be tediously repeated to know what the test is about 
    t.equal(f.brownFox('quick brown').indexOf("jumped") > -1, true, "contains jumped") // should pass 
    t.equal(f.brownFox('quick black').indexOf("junped") > -1, true, "contains jumped") // should fail 
    t.comment('failures are not descriptive') 
    t.end() 
}) 
// Using equal() can result in more time spent fixing or adjusting the test 
// than coding the application 

// So define your own tape test method 
/////////////// Example of defining a custom tape test method 
test.Test.prototype.stringContains = function(actual, contents, message) { 
    this._assert(actual.indexOf(contents) > -1, { 
    message: message || 'should contain '+contents, 
    operator: 'stringContains', 
    actual: actual, 
    expected: contents 
    }); 
}; 

/////////////// Example using a custom tape test method 
test('String should contain a fox', function (t) { 
    // shows actual value when test fails 
    t.stringContains(f.brownFox('quick brown'), "jumped") // should pass 
    t.stringContains(f.brownFox('quick brown'), "junped") // should fail 
    // still supports custom message to override default message: 
    t.stringContains(f.brownFox('quick brown'), "jumped", 'must contain "jumped"') // should pass 
    t.stringContains(f.brownFox('quik white'), "jumped") // should pass 
    t.comment('failures are more descriptive') 
    t.end() 
}) 

Beachten Sie, dass die angepasste Testausgabe berichtet nun das "tatsächliche" string :

TAP version 13 
# String must match exactly 
ok 1 should be equivalent 
not ok 2 should be equivalent 
    --- 
    operator: deepEqual 
    expected: 'The quick brown fox jumped' 
    actual: 'The quick black fox jumped' 
    at: Test.<anonymous> (./tape-test.js:9:7) 
    ... 
ok 3 should be equivalent 
# Strings must be fully specified to match word for word 
# String should contain a fox 
ok 4 contains jumped 
not ok 5 contains jumped 
    --- 
    operator: equal 
    expected: true 
    actual: false 
    at: Test.<anonymous> (./tape-test.js:23:7) 
    ... 
# failures are not descriptive 
# String should contain a fox 
ok 6 should contain jumped 
not ok 7 should contain junped 
    --- 
    operator: stringContains 
    expected: 'junped' 
    actual: 'The quick brown fox jumped' 
    at: Test.test.Test.stringContains (./tape-test.js:33:8) 
ok 8 must contain "jumped" 
ok 9 should contain jumped 
# failures are more descriptive 

1..9 
# tests 9 
# pass 6 
# fail 3 

ich installierte extend-tape und erkannte dann hätte ich um es mit babel zu verwenden, weil V8 Importe noch nicht unterstützt, aber ich nicht babel als Abhängigkeit hinzufügen wollte.

Da der obige Code funktioniert, sehe ich nicht den Punkt der Verwendung extend-tape.