2013-02-22 10 views
6

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)); 
    }); 
}); 
  1. Passo
  2. Expected null to equal <jasmine.any(function String() { [native code] })>.
  3. Passo
  4. 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(); 
    }); 
}); 
+0

ci si può aspettare un unico ingresso da darvi 'STRING' o' null'? Non capisco perché un singolo 'expect' dovrebbe coprire entrambi i casi. – Mathletics

+0

@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

+0

Puoi mostrare un esempio di un metodo che, da un singolo input, potrebbe potenzialmente restituire entrambi? – Mathletics

risposta

11

Scrivi la tua matcher personalizzato:

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

A condizione che è sicuramente desidera i casi di lavorare sullo stesso test, si potrebbe scrivere il proprio matcher. Qualcosa di simile

toBeStringOrNull: function() { 
    var actual = this.actual; 

    this.message = function() { 
    return "Expected " + actual + " to be either string or null"; 
    } 

    return jasmine.any(String) || actual === null; 
} 
+0

Spettacolare, grazie. Penso che cercherò di evitare quello che stavo tentando, ma è bello sapere che posso realizzare qualcosa del genere in Jasmine abbastanza facilmente. – JackMorrissey

+1

Questo non funziona. restituendo jasmine.any (String) significa che il matcher sopra riportato restituisce sempre true. Vedi http://stackoverflow.com/questions/8947787/object-equality-in-jasmine-custom-matcher per ulteriori informazioni su come utilizzare un matcher all'interno di un altro. – tukushan

+0

@tukushan Hai assolutamente ragione. Grazie per la correzione e il puntatore. – JackMorrissey

Problemi correlati