当前位置: 首页 > 面试题库 >

当我尝试运行测试时,获取“不匹配的匿名define()模块……”

滕英奕
2023-03-14
问题内容

我正在尝试使用requirejs配置我的业力茉莉花单元测试。但是每次我运行它时,都会出现以下错误:

Chrome 34.0.1847 (Mac OS X 10.9.2) ERROR
Uncaught Error: Mismatched anonymous define() module: function (angular){

 describe('Unit: Testing RequireJS', function(){
  var ctrl;
  var scope;
  var rootScope;

  beforeEach(angular.mock.module('wsaApp'));

  beforeEach(angular.mock...<omitted>...ch

以下是differenr文件: spec文件:

define(['angular'], function(angular){

describe('Unit: Testing RequireJS', function(){
   var ctrl;
   var scope;
   var rootScope;

beforeEach(angular.mock.module('wsaApp'));

beforeEach(angular.mock.inject(function($rootScope){
    scope = $rootScope.$new();
    rootScope = $rootScope;
}));

});
});

main.js

require.config({

paths: {
    /* ABC order */
    'angular': 'vendor/angular/1.2.0/angular.min'
},
shim: {
    'angular': { exports: 'angular' },

    'app/controllers': { deps: ['angular'] }
}
});

test-main.js

// This creates an array of all the files that Karma finds with a suffix of
// Test.js (eg utilsTest.js) to be added to the Require JS config below
var tests = [],
file;
for (file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
    if(/spec\.js$/.test(file)) {
        tests.push(file);
    }
}
}
requirejs.config({
baseUrl: '/base/public/javascripts/',  // Karma serves files from /base/<your-base-path>
    paths: {
        /* ABC order */
        'angular': 'vendor/angular/1.2.1/angular.min'

    },
    shim: {
        'angular': { exports: 'angular' },
        'app/controllers': { deps: ['angular'] },           
         },
deps: tests,  // add tests array to load our tests

callback: window.__karma__.start  // start tests once Require.js is done
});

karma.conf.js

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

// Fix for "JASMINE is not supported anymore" warning
frameworks: ["jasmine", "requirejs"],

// list of files / patterns to load in the browser
files: [
    'vendor/angular/1.2.1/angular.js',
    'jquery-1.7.1.min.js',
    'test/spec/**/*.js',
    'test/test-main.js'
],

preprocessors: {
    'app/**/*.js': 'coverage'
},

// list of files to exclude
exclude: ['app/main.js'],

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

coverageReporter : {
    type: 'html',
    dir: 'coverage/'
},


// web server port
port: 9876,

// cli runner port
runnerPort: 9100,

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

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

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

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

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

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

我尝试了其他线程中提到的不同选项,但似乎没有任何效果。


问题答案:

最终,我解决了所有问题,并能够通过requirejs配置成功运行茉莉花测试。我最先提到了业力配置中的所有依赖项,并将它们标记为included: false独占,以便它们通过我的test-config文件由requirejs加载。

files: [
    {pattern: 'vendor/angular/1.2.1/angular.js', included: false},
    {pattern: 'vendor/angular/1.2.1/angular-mocks.js', included: false},
    {pattern: 'vendor/angular/1.2.1/angular-*.js', included: false},
    {pattern: 'vendor/bootstrap/bootstrap-*.js', included: false},
    {pattern: 'jquery-1.7.1.min.js', included: false},
    {pattern: 'app/app.js', included: false},
    {pattern: 'app/**/*.js', included: false},
    {pattern: 'test/test-config.js', included: true}]

只有test-config通过业力加载,所有其他配置都包含在业力配置中,但标记为false。

另外,我必须在我的规范文件中加载app.js,以便加载模块和控制器:

define(['angular-mocks', 'jquery', 'app/app'], function(angularmocks, $, app){
describe.....
}


 类似资料:
  • 问题内容: 首次浏览Web应用程序时(通常在禁用缓存的浏览器中),我收到此错误。 错误:匿名define()模块不匹配:函数(要求){ HTML : JS : 谁能确切知道此错误的含义以及发生的原因? 问题答案: 就像AlienWebguy说,每文档,require.js可以炸毁如果 您在自己的脚本标签中有一个匿名的define(“ 调用define()的模块,没有字符串ID ”)(我假设它们实际

  • 当我尝试在Chrome中运行测试时,出现以下错误: 初始化方法AutomationUsingSelenium。蓝绿色。MyTestInitialize引发异常。OpenQA。硒。DriversServiceNotFoundException:OpenQA。硒。DriversServiceNotFoundException 原因是什么?

  • 问题内容: 将模块插入项目后,如何在该模块中运行测试用例? 即我做了: 然后尝试测试其中的内容: 我们是否应该单独检查模块并进行测试?我不知道该怎么办。看来,当我知道如何找到已获取的模块时,但是当我使用它时,它在路径中“找不到”。 问题答案: 需要包,而不是相对于的文件夹。 因此,省略开头和结尾的文件名,它将起作用: 如果当前目录是您要测试的软件包文件夹,您甚至可以忽略它,例如 作为参考,请参阅C

  • 当我运行codeception测试用例时,我的日志文件中出现了这个错误。如何解决这个问题? 代码: 命令: ./vendor/bin/codecept run tests/functional/AdminPhoneTestCest.php:testUpdatePhone 日志文件中的错误: [2015-06-06 05:34:02]本地的。错误:异常'照明\会话\令牌错配异常'在 /var/www

  • 问题内容: 当我尝试在代码中执行HQL时,发生以下异常。我在各个站点中进行了检查,发现antlr.2.7.6.jar shd位于类路径中。我在项目中检查了此内容,发现在我的Maven依赖项中存在该内容。因此,不应有任何此类问题。但是我仍然遇到这个问题。谁能在这方面帮助我。我在“ empList = getHibernateTemplate()。find(“来自Employee”);“行中收到此错误

  • 我试图使用Selenium/Java为我的自动化脚本运行maven项目。但是,当我执行mvn test命令时,测试并没有运行。请让我知道当我尝试运行maven项目时,cucumber测试没有被执行的问题是什么。 下面是我的pom.xml文件