Jasmine esperar lógica (esperar A O B)
Necesito establecer la prueba para tener éxito si se cumple una de las dos expectativas:
expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number));
expect(mySpy.mostRecentCall.args[0]).toEqual(false);
Esperaba que se viera así:
expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number)).or.toEqual(false);
¿Hay algo que me perdí en los documentos o tengo que escribir mi propio matcher?
3 answers
Nota: Esta solución contiene sintaxis para versiones anteriores a Jasmine v2.0. Para obtener más información sobre los emparejadores personalizados ahora, consulte: https://jasmine.github.io/2.0/custom_matcher.html
Matchers.js solo funciona con un' modificador de resultados'- not
:
Core / Spec.js:
jasmine.Spec.prototype.expect = function(actual) {
var positive = new (this.getMatchersClass_())(this.env, actual, this);
positive.not = new (this.getMatchersClass_())(this.env, actual, this, true);
return positive;
Core / Matchers.js:
jasmine.Matchers = function(env, actual, spec, opt_isNot) {
...
this.isNot = opt_isNot || false;
}
...
jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
return function() {
...
if (this.isNot) {
result = !result;
}
}
}
Así que parece que realmente necesitas escribir tu propio matcher (desde dentro de un bloque before
o it
para this
correcto). Para ejemplo:
this.addMatchers({
toBeAnyOf: function(expecteds) {
var result = false;
for (var i = 0, l = expecteds.length; i < l; i++) {
if (this.actual === expecteds[i]) {
result = true;
break;
}
}
return result;
}
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-18 22:14:13
Agregue varias cadenas comparables en una matriz y luego compare. Invertir el orden de la comparación.
expect(["New", "In Progress"]).toContain(Status);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-12 11:59:42
Esta es una vieja pregunta, pero en caso de que alguien todavía esté buscando, tengo otra respuesta.
¿Qué tal construir la lógica O expresión y esperar eso? Así:
var argIsANumber = !isNaN(mySpy.mostRecentCall.args[0]);
var argIsBooleanFalse = (mySpy.mostRecentCall.args[0] === false);
expect( argIsANumber || argIsBooleanFalse ).toBe(true);
De esta manera, puede probar/esperar explícitamente la condición OR, y solo necesita usar Jasmine para probar una coincidencia/desajuste booleano. Funcionará en Jasmine 1 o Jasmine 2:)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-13 00:48:49