2013-12-19 1 views
2

저는 Durandal.js와 Jasmine을 사용한 Javascript 테스트를 처음 접했고 약간의 도움이 필요합니다. 나는 Karma와 Jasmine으로 테스트 환경을 만들고 운영하고 있지만, i18next 종속성을 포함하는 뷰 모델을 테스트하는 방법을 고민하고 있습니다.i18next 구현을 포함하는 Durandal.js 뷰 모델을 테스트하는 방법은 무엇입니까?

여기 내 설치의 벗었 예제 :

main.js

requirejs.config({ 
    paths: { 
     'models': 'models', 
     'text': '../lib/require/text', 
     'durandal': '../lib/durandal/js', 
     'plugins': '../lib/durandal/js/plugins', 
     'transitions': '../lib/durandal/js/transitions', 
     'knockout': '../lib/knockout/knockout-2.3.0', 
     'bootstrap': '../lib/bootstrap/js/bootstrap.min', 
     'jquery': '../lib/jquery/jquery-1.9.1', 
     'i18next': '../lib/i18next/i18next.amd.withJQuery-1.7.1.min' 
    } 
}); 

define(['plugins/router', 'durandal/system', 'durandal/app', 'durandal/binder', 'durandal/viewLocator', 'knockout', 'jquery', 'i18next'], function (router, system, app, binder, viewLocator, ko, $, i18n) {  
    app.configurePlugins({ 
     router: true 
    }); 

    app.start().then(function() { 
     // Setup of i18n 
     i18n.init({ 
      detectFromHeaders: false, 
      fallbackLng: 'en', 
      preload: ['nb', 'en'], 
      supportedLngs: ['nb', 'en'], 
      resGetPath: 'locales/__lng__.json', 
      useCookie: true, 
     }, function() { 
      binder.binding = function (obj, view) { 
       $(view).i18n(); 
      }; 

      viewLocator.useConvention(); 
      app.setRoot('viewmodels/shell'); 
     }); 
    }); 
}); 

shell.js

(자신이 필요로
define(['plugins/router', 'durandal/app', 'i18next'], function (router, app, i18n) { 
    return { 
     router: router, 
     activate: function() { 
      return router.map([ 
       { route: '', title: i18n.t('pageTitle'), moduleId: 'viewmodels/ViewModel', nav: true } 
      ]).buildNavigationModel().mapUnknownRoutes('viewmodels/ViewModel', 'unknown').activate({ 
       pushState: false, 
       hashChange: true 
      }); 
     } 
    }; 
}); 

test.js. js config)

define(["i18next"], function (i18n) { 
    var pageTitle = i18n.t("pageTitle"); 

    return { 
     pageTitle: pageTitle 
    }; 
}); 

사양 (i18next)

define(['viewModels/ViewModel'], function (ViewModel) { 
    describe("Viewmodel test", function() { 
     it("should be testable", function() { 
      expect(true).toEqual(true); 
     }); 
     it("should have title", function() { 
      expect(ViewModel.pageTitle).not.toBeEmpty(); 
     }); 
    }); 
}); 
,536 : 691,363,210
var tests = []; 

for (var file in window.__karma__.files) { 
    if (window.__karma__.files.hasOwnProperty(file)) { 
     if (/.*\.spec\.js$/.test(file)) { 
      tests.push(file); 
     } 
    } 
} 

//Workaround for the timestamp issue 
for (var file in window.__karma__.files) { 
    window.__karma__.files[file.replace(/^\//, '')] = window.__karma__.files[file]; 
} 

require.config({ 
    baseUrl: 'base', 
    paths: { 
     'models': 'app/models', 
     'viewModels': 'app/viewmodels', 
     'specs': 'tests/specs/', 
     'text': 'lib/require/text', 
     'durandal': 'lib/durandal/js', 
     'plugins': 'lib/durandal/js/plugins', 
     'transitions': 'lib/durandal/js/transitions', 
     'knockout': 'lib/knockout/knockout-2.3.0', 
     'jquery': 'lib/jquery/jquery-1.9.1', 
     'i18next': 'lib/i18next/i18next.amd.withJQuery-1.7.1.min', 
    }, 

    // ask Require.js to load these files (all our tests) 
    deps: tests, 

    // start test run, once Require.js is done 
    callback: window.__karma__.start 
}); 

// Karma configuration 

module.exports = function (config) { 
    config.set({ 
     // base path, that will be used to resolve files and exclude 
     basePath: '', 


     // frameworks to use 
     frameworks: ['jasmine', 'requirejs'], 


     // list of files/patterns to load in the browser 
     files: [ 
      { pattern: 'lib/jquery/jquery-1.9.1.js', watched: false, included: true, served: true }, 
      { pattern: 'tests/jasmine/jasmine-jquery.js', watched: false, served: true, included: true }, 

      { pattern: 'tests/specs/**/*.js', included: false }, 
      { pattern: 'tests/sinon.js', included: false }, 
      { pattern: 'lib/**/*.js', included: false, served: true }, 
      { pattern: 'app/**/*.js', included: false, served: true }, 

      'tests/test.js', 

      //Serve the fixtures 
      { pattern: 'app/**/*.html', watched: true, served: true, included: false }, 
      { pattern: 'app/**/*.json', watched: true, served: true, included: false }, 
      { pattern: 'tests/**/*.json', watched: true, served: true, included: false } 
     ], 

     preprocessors: [ 
      { '**/*.html': '' } 
     ], 
     // list of files to exclude 
     exclude: [ 
      'foundation/*.js', 
      'require.js', 
      'init.js', 
      'jasmine/jasmine.js', 
      'jasmine/jasmine-html.js', 
      'node_modules/**/*.html' 
     ], 


     // test results reporter to use 
     // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' 
     reporters: ['progress'], 


     // web server port 
     port: 9876, 


     // enable/disable colors in the output (reporters and logs) 
     colors: true, 


     // level of logging 
     // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
     logLevel: config.LOG_INFO, 


     // enable/disable watching file and executing tests whenever any file changes 
     autoWatch: true, 


     // Start these browsers, currently available: 
     // - Chrome 
     // - ChromeCanary 
     // - Firefox 
     // - Opera 
     // - Safari (only Mac) 
     // - PhantomJS 
     // - IE (only Windows) 
     browsers: ['PhantomJS'], 
     //browsers: ['Chrome', 'Firefox', 'IE'], 

     // If browser does not capture in given timeout [ms], kill it 
     captureTimeout: 60000, 


     // Continuous Integration mode 
     // if true, it capture browsers, run tests and exit 
     singleRun: false 
    }); 
}; 

뷰 모델 (i18next)를 karma.conf.js 91,363,210

테스트 러너 출력

PhantomJS 1.9.2 (Mac OS X) ERROR ReferenceError: Can't find variable: initialized at /Users/xxx/dev/projectXYZ/lib/i18next/i18next.amd.withJQuery-1.7.1.min.js:5 PhantomJS 1.9.2 (Mac OS X): Executed 0 of 0 ERROR (0.312 secs/0 secs)

뷰 모델 (WO/i18next)

define([], function() { 
    var pageTitle = "This is the page title"; 

    return { 
     pageTitle: pageTitle 
    }; 
}); 

사양 (WO/i18next)

define(['viewModels/ViewModel'], function (ViewModel) { 
    describe("Viewmodel test", function() { 
     it("should be testable", function() { 
      expect(true).toEqual(true); 
     }); 
     it("should have title", function() { 
      expect(ViewModel.pageTitle).toEqual("This is the page title"); 
     }); 
    }); 
}); 

테스트 러너 출력 (wo/i18next)

PhantomJS 1.9.2 (Mac OS X): Executed 2 of 2 SUCCESS (0.312 secs/0.164 secs)

나는 어떻게 든 i18next를 초기화해야 할 것 같아요,하지만 난 잘 모릅니다.

도움을 주시면 감사하겠습니다.

감사합니다,

레미


UPDATE

이것은 내가하고 결국 무엇을 :

테스트.JS :

'i18next-original': 'lib/i18next/i18next.amd.withJQuery-1.7.1.min', 
'i18next': 'tests/i18n-test' 

국제화-test.js :

define(['i18next-original'], function (i18noriginal) { 
'use strict'; 

i18noriginal.init({ 
    lng: 'nb', 
    detectFromHeaders: false, 
    fallbackLng: 'en', 
    preload: ['nb', 'en'], 
    supportedLngs: ['nb', 'en'], 
    resGetPath: 'base/locales/__lng__.json', 
    useCookie: false, 
    debug: false, 
    getAsync: false 
}); 

return i18noriginal; 
}); 

karma.conf.js :

{ pattern: 'tests/i18n-test.js', included: false, served: true }, 
+0

https://github.com/medallia/testr.js 테스트를 실행할 때로드되지 국제화에 문제가있을 수 있습니다. 'define ([ 'viewModels/ViewModel', 'i18next'] ...'스펙에 추가하거나 karma.conf.js를 통해 브라우저에로드하십시오. – RainerAtSpirit

+0

답장을 보내 주셔서 감사합니다. 원본을 업데이트합니다 변경 사항이있는 게시물 –

답변

0

Hallais 레미! 이 같은 beforeEach에 i18next 초기화 할 수 있습니다 :

define(['viewModels/ViewModel', 'i18next'], function (ViewModel, i18n) { 
    describe("Viewmodel test", function() { 
     beforeEach(function() { 
      var initialized = false; 

      runs(function() { 
       i18n.init({ 
        detectFromHeaders: false, 
        fallbackLng: 'en', 
        preload: ['nb', 'en'], 
        supportedLngs: ['nb', 'en'], 
        resGetPath: 'locales/__lng__.json', 
        useCookie: true 
       }, function() { initialized = true; }); 
      }); 

      waitsFor(function() { 
       return initialized; 
      }, 'i18n to initialize', 100); 
     }); 

     it("should be testable", function() { 
      expect(true).toEqual(true); 
     }); 
     it("should have title", function() { 
      expect(ViewModel.pageTitle).not.toBeEmpty(); 
     }); 
    }); 
}); 

뷰 모델 자체의 내부 코드에 테스트를 중심으로 들어 당신은 당신의 ViewModel에 사용되는 국제화 개체를 조롱하는 대신 고려할 수 있습니다.

testr.js는 require.js 종속성을 조롱 도움이 될 수 있습니다 :

+0

감사합니다, Hallvar 예를 들어 알리지 못했지만 동료가 휴가를 보내고있는 동안 해결책을 찾았습니다. 원래 게시물을 업데이트했습니다. –

관련 문제