2013-06-12 11 views
6

Ho creato un servizio Env che racchiude informazioni sull'ambiente e attualmente sto utilizzando $location.host() per determinare l'ambiente in cui mi trovo. Come faccio a prenderlo in giro nei miei test?

ho letto https://groups.google.com/forum/?fromgroups#!topic/angular/F0jFWC4G9hI, ma non sembra funzionare, per esempio:

describe("Env (environment) service", function() { 

    var Env; 

    beforeEach(module('App')); 

    beforeEach(inject(
    ['Env', function(e) { 
     Env = e; 
    }] 
)); 

    describe("for staging", function() { 
    beforeEach(inject(function($location, $rootScope) { 
     $location.host("http://staging-site.com"); 
     $rootScope.$apply(); 
    })); 

    it("returns envrionment as staging", function() { 
     expect(Env.environment).toEqual("staging"); 
    }); 

    it("returns .isStaging() as true", function() { 
     expect(Env.isStaging()).toBeTruthy(); 
    }); 
    }); 
}); 

Ho anche provato la variante $browser, ma che non funziona neanche. Qualche idea?

+0

Ho anche provato '$ browser.url()' come da http://www.thecssninja.com/javascript/angular -location-testing, ma anche questo non funziona. – zlog

+0

Ho un problema simile, ho bisogno di iniettare $ posizione nel mio test dal momento che il servizio lo richiede. –

+1

https://github.com/angular/angular.js/issues/493 –

risposta

4

Ho avuto un problema simile e ho utilizzato il servizio $ injector. (Non so se è la soluzione più semplice, ma ha funzionato per me :))

Poiché $ posizione non può essere invocato durante le prove, ho preparato il mio finto. Innanzitutto, è necessario creare un metodo di fabbrica. (O servizio o provider se si preferisce - vedi https://gist.github.com/Mithrandir0x/3639232 per il confronto):

function locationFactory(_host) { 
    return function() { 
    return { 
     /* If you need more then $location.host(), add more methods */ 
     host: function() { 
     return _host; 
     } 
    }; 
    }; 
} 

Poi prima di creare il 'Env', alimentazione iniettore con questo $ location finto:

module(function ($provide) { 
    $provide.factory('$location', locationFactory('http://staging-site.com')); 
}); 

Ora ogni tempo il tuo accesso $ posizione nel tuo codice viene iniettata la tua simulazione, quindi restituisce tutto il necessario. Altro su $ fornire il metodo è in angular docs

Spero che questo ti aiuti in futuro.

Aggiornamento: Vedo un posto in cui potresti aver sbagliato (o che almeno sarebbe sbagliato nella mia soluzione). Sembra che tu stia iniziando il tuo modulo 'Env' (che immagino calcoli i dati immediatamente) e solo dopo questo cambi $ posizione - che potrebbe essere già troppo tardi.

6

Il modo migliore è quello di utilizzare le spie IMHO: http://tobyho.com/2011/12/15/jasmine-spy-cheatsheet/

// inject $location 
spyOn($location, "host").andReturn("super.domain.com"); 
var host = $location.host(); 
alert(host) // is "super.domain.com" 
expect($location.host).toHaveBeenCalled(); 
+1

La sintassi per jasmine 2.0 e versioni successive è stata modificata, è necessario utilizzare .and.returnValue() – wholladay

0

Ecco un altro modo per prendere in giro $ posizione, con Sinon.js

locationMock = { 
     path: sinon.spy() 
    }; 

E un esempio asserzione:

locationMock.path.should.be.calledWith('/home'); 
2

C'è un buon esempio nel documento: https://docs.angularjs.org/guide/services

Si desidera assicurarsi di utilizzare $provide prima di creare un'istanza del servizio. Mi piace usare $injector nel testcase, per prima cosa decidere la posizione quindi creare un'istanza del servizio.

var mockLocation; 

    beforeEach(function() { 
    // control the $location.host() function 
    // which we fake in the testcases. 
    mockLocation = { 
     host: jasmine.createSpy() 
    }; 
    module(function($provide) { 
     $provide.value('$location', mockLocation); 
    }); 
    }); 

poi

describe('staging server:', function() { 
    beforeEach(function() { 
     // mockLocation.host.andReturn('http://staging-site.com'); // jasmine 1.x 
     mockLocation.host.and.returnValue('http://staging-site.com'); 
    }); 

    it('returns envrionment as staging', inject(function($injector) { 
     Env = $injector.get('Env'); 
     expect(Env.environment).toEqual('staging'); 
    })); 
    }); 

e

describe('production server:', function() { 
    beforeEach(function() { 
     // mockLocation.host.andReturn('prod.example.com'); // jasmine 1.x 
     mockLocation.host.and.returnValue('prod.example.com'); 
    }); 

    it('returns envrionment as production', inject(function($injector) { 
     Env = $injector.get('Env'); 
     expect(Env.environment).toEqual('production'); 
    })); 
    }); 
Problemi correlati