Sono molto nuovo a Jasmine e ho trovato una situazione in cui mi sarei aspettato una stringa o null. Ho provato a fare un o all'interno di toEqual, ma sto vedendo alcuni risultati strani che mi portano a credere che sto andando su questo nel modo sbagliato. Qual è il modo migliore per affrontare una situazione come questa?È possibile eseguire il controllo di più tipi all'interno di un toEqual in Jasmine.Js?
Forse sto solo facendo il mio test sbagliato. Dovrei semplicemente scartare questa idea di avere un test per testare entrambe le situazioni?
describe("Jasmine", function() {
//1
it("should be able to handle any(String) || null within toEqual for string", function() {
expect("aString").toEqual(jasmine.any(String) || null);
});
//2
it("should be able to handle any(String) || null within toEqual for null", function() {
expect(null).toEqual(jasmine.any(String) || null);
});
//3
it("should be able to handle null || any(String) within toEqual for string", function() {
expect("aString").toEqual(null || jasmine.any(String));
});
//4
it("should be able to handle null || any(String) within toEqual for null", function() {
expect(null).toEqual(null || jasmine.any(String));
});
});
- Passo
Expected null to equal <jasmine.any(function String() { [native code] })>.
- Passo
Expected null to equal <jasmine.any(function String() { [native code] })>.
mi rendo conto che c'è anche un toBeNull(), che è probabilmente il motivo per cui i risultati sono così traballante, ma senza un "o" concatenando non sapevo come incorporarlo.
(Esecuzione Jasmine 1.3.1 revisione 1354556913)
risolto! soluzione completa qui sotto se qualcuno è interessato
describe("Jasmine", function() {
beforeEach(function() {
this.addMatchers({
toBeStringOrNull: function() {
var actual = this.actual;
this.message = function() {
return "Expected " + actual + " to be either string or null";
};
return typeof actual === 'string' || actual instanceof String || actual === null;
}
});
});
//1
it("should be able to handle any(String) || null within toEqual for string", function() {
expect("aString").toBeStringOrNull();
});
//2
it("should be able to handle any(String) || null within toEqual for null", function() {
expect(null).toBeStringOrNull();
});
//3
it("should be able to handle null || any(String) within toEqual for string", function() {
expect("aString").toBeStringOrNull();
});
//4
it("should be able to handle null || any(String) within toEqual for null", function() {
expect(null).toBeStringOrNull();
});
});
ci si può aspettare un unico ingresso da darvi 'STRING' o' null'? Non capisco perché un singolo 'expect' dovrebbe coprire entrambi i casi. – Mathletics
@Mathletics mi aspetterei sia, che è il motivo per cui sto colpendo questo muro. Stavo cercando di creare un test in modo che durante i test di integrazione potessi riutilizzarlo per entrambi i casi. Penso di rendermi conto che questo non è il modo standard per farlo. – JackMorrissey
Puoi mostrare un esempio di un metodo che, da un singolo input, potrebbe potenzialmente restituire entrambi? – Mathletics