2012-09-20 15 views
8

Ho appena cercato l'iniezione delle dipendenze nella mia strategia di test delle unità utilizzando RequireJS e Jasmine. Mi piace molto l'idea che sta dietro allo testr e ho provato ad installare testr seguendo gli esempi in github ma non riesco a capire cosa c'è che non va. Ricevo sempre l'erroreInjection dipendenza JavaScript utilizzando RequireJS, Jasmine e testr

Error: module has not been loaded: today

quando testr prova a caricare il modulo che sta per essere testato.

Ecco qualche contesto ..

index.html ..

<script data-main="config" src="../../assets/js/libs/require.js"></script> 
<script src="vendor/testr.js"></script> 

config.js ..

require.config({ 

    // Initialize specs. 
    deps:["main"], 
... 
... 
}); 

main.js ..

require([ 
    // Load the example spec, replace this and add your own spec 
    "spec/today" 
], function() { 
    var jasmineEnv = jasmine.getEnv(); 
    jasmineEnv.execute(); 
}); 

spec \ today.js ..

describe('Today print', function() { 
    var date = {}, today; 
    beforeEach(function() { 
    date.today = new Date(2012, 3, 30); 
    today = testr('today', {'util/date': date}); //Here is where the error is thrown 
    }); 

    it('is user-friendly', function() { 
    expect(today.getDateString()).toBe('Today is Monday, 30th April, 2012'); 
    }); 
}); 

today.js ..

define(['string', 'util/date'], function(string, date) { 
    return { 
    getDateString: function() { 
     return string.format('Today is %d', date.today); 
    } 
    } 
}); 

c'è qualcuno che sono stato con il stesso tipo di problemi? . Sto usando RequireJS 2.0.6

Grazie.

risposta

1

Il modulo "oggi" deve essere caricato da requirejs prima di utilizzarlo con testr. Provare qualcosa di simile:

require(['today'], function(){ 
    describe('Today print', function() { 
     var date = {}, today; 
     beforeEach(function() { 
     date.today = new Date(2012, 3, 30); 
     today = testr('today', {'util/date': date}); //Here is where the error is thrown 
     }); 

     it('is user-friendly', function() { 
     expect(today.getDateString()).toBe('Today is Monday, 30th April, 2012'); 
     }); 
    }); 
}); 

Leggi anche: http://cyberasylum.janithw.com/mocking-requirejs-dependencies-for-unit-testing/

+0

bene. Ho lavorato per far funzionare subito i test per alcuni giorni. Questo è il più vicino che sono venuto a vedere un progetto di esempio. Ho creato un repo github per questo esempio (con alcune modifiche minori) utilizzando il gelsomino per condividere con altri. Tuttavia, non funziona ancora. Qualcuno può aiutare qui? Grazie. https://github.com/loesak/jasmine-maven-require-testr – loesak

+0

Ho lo stesso problema. Infatti, lo carico manualmente come dep nel mio require.config, e Karma mostra che il file viene richiesto (prima). – FlavorScape

Problemi correlati