Angularjs 单元测试角火业力误差

Angularjs 单元测试角火业力误差,angularjs,unit-testing,firebase,karma-runner,angularfire,Angularjs,Unit Testing,Firebase,Karma Runner,Angularfire,当我运行我的因果报应单位脚本时,我得到了这个错误,但我还不能找出原因 Error: [$injector:unpr] Unknown provider: FBURLProvider <- FBURL 这是我的规格代码 'use strict'; describe('Directive: userMenu', function () { // load the directive's module beforeEach(module('userMenu', 'firebase'

当我运行我的因果报应单位脚本时,我得到了这个错误,但我还不能找出原因

Error: [$injector:unpr] Unknown provider: FBURLProvider <- FBURL
这是我的规格代码

'use strict';

describe('Directive: userMenu', function () {

  // load the directive's module
  beforeEach(module('userMenu', 'firebase'));

  var element,
    elementScope,
    scope;


  beforeEach(inject(function ($rootScope, $compile, _FBURL_, _angularFire_) {
    scope = $rootScope.$new();
    element = angular.element('<div user-menu></div>');
    element = $compile(element)(scope);
    elementScope = element.scope();
  }));

  it('should get user data', inject(function ($compile) {
    console.log(scope);
  }));
});
“严格使用”;
描述('指令:用户菜单',函数(){
//加载指令的模块
每个模块(模块('userMenu','firebase'))之前;
var元素,
元素范围,
范围
beforeach(注入(函数($rootScope、$compile、\u FBURL、\u angularFire){
scope=$rootScope.$new();
元素=角度。元素(“”);
元素=$compile(元素)(范围);
elementScope=element.scope();
}));
它('should get user data',inject(函数($compile)){
console.log(范围);
}));
});

老实说,我对单元测试不太熟悉,所以我可能错过了一些非常明显的东西,但是如果有任何帮助,我将不胜感激。

Firbase团队已经为我指出了中一些测试代码的方向,这些代码随后被删除。下面是我的单元测试,其中包括Fireseed项目中的存根

'use strict';

describe('Directive: userMenu', function () {

  // load the directive's module
  beforeEach(module('userMenu', 'firebase', function($provide) {
    $provide.value('Firebase', firebaseStub());
    $provide.value('FBURL', 'FAKE_FB_URL');
    $provide.value('angularFireAuth', angularAuthStub());
  }));


  /**
   * This is from https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
   */
  function stub() {
    var out = {};
    angular.forEach(arguments, function(m) {
      out[m] = jasmine.createSpy();
    });
    return out;
  }
  /**
   * This is from https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
   */
  function stub() {
    var out = {};
    angular.forEach(arguments, function(m) {
      out[m] = jasmine.createSpy();
    });
    return out;
  }
  /**
   * This is from https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
   */
  function reject($q, error) {
    var def = $q.defer();
    def.reject(error);
    return def.promise;
  }
  /**
   * This is from https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
   */
  function resolve($q, val) {
    var def = $q.defer();
    def.resolve(val);
    return def.promise;
  }
  /**
   * This is from https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
   */
  function firebaseStub() {
    // firebase is invoked using new Firebase, but we need a static ref
    // to the functions before it is instantiated, so we cheat here by
    // attaching the functions as Firebase.fns, and ignore new (we don't use `this` or `prototype`)
    var fns = stub('set');
    customSpy(fns, 'child', function() { return fns; });

    var Firebase = function() {
      angular.extend(this, fns);
      return fns;
    };
    Firebase.fns = fns;

    return Firebase;
  }
  /**
   * This is from https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
   */
  function angularAuthStub() {
    var auth = stub('login', 'logout', 'createAccount', 'changePassword');
    auth._authClient = stub('changePassword', 'createUser');
    return auth;
  }
  /**
   * This is from https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
   */
  function customSpy(obj, m, fn) {
    obj[m] = fn;
    spyOn(obj, m).andCallThrough();
  }

  var element,
    elementScope,
    scope;


  beforeEach(inject(function ($rootScope, $compile) {
    scope = $rootScope.$new();
    element = angular.element('<div user-menu></div>');
    element = $compile(element)(scope);
    elementScope = element.scope();
  }));

  it('should default to a login message', inject(function ($compile) {
    scope.$digest();
    var text = element.text();
    expect(text).toBe('Please login');
  }));

  it('default message should contain a link to login page', inject(function ($compile) {
    scope.$digest();
    var href = element.find('a').attr('href');
    expect(href).toBe('#/login');
  }));
});
“严格使用”;
描述('指令:用户菜单',函数(){
//加载指令的模块
每个模块('userMenu','firebase',函数($provide)之前{
$provide.value('Firebase',firebaseStub());
$provide.value('FBURL','FAKE_FB_URL');
$provide.value('angularFireAuth',angularAuthStub());
}));
/**
*这是从https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
*/
函数存根(){
var out={};
forEach(参数,函数(m){
out[m]=jasmine.createSpy();
});
返回;
}
/**
*这是从https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
*/
函数存根(){
var out={};
forEach(参数,函数(m){
out[m]=jasmine.createSpy();
});
返回;
}
/**
*这是从https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
*/
函数拒绝($q,错误){
var def=$q.defer();
定义拒绝(错误);
回报承诺;
}
/**
*这是从https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
*/
函数解析($q,val){
var def=$q.defer();
定义解析(val);
回报承诺;
}
/**
*这是从https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
*/
函数firebaseStub(){
//firebase是使用新的firebase调用的,但我们需要一个静态引用
//在它被实例化之前对函数进行修改,所以我们在这里通过
//将函数附加为Firebase.fns,并忽略new(我们不使用'this'或'prototype')
var fns=存根(“集”);
customSpy(fns,'child',function(){return fns;});
var Firebase=函数(){
角度。延伸(此,fns);
返回fns;
};
Firebase.fns=fns;
返回火基;
}
/**
*这是从https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
*/
函数angularAuthStub(){
var auth=stub('login','logout','createAccount','changePassword');
auth._authClient=stub('changePassword','createUser');
返回auth;
}
/**
*这是从https://github.com/firebase/angularFire-seed/blob/master/test/unit/servicesSpec.js
*/
功能customSpy(obj、m、fn){
obj[m]=fn;
spyOn(obj,m.)和callthrough();
}
var元素,
元素范围,
范围
beforeach(注入(函数($rootScope,$compile){
scope=$rootScope.$new();
元素=角度。元素(“”);
元素=$compile(元素)(范围);
elementScope=element.scope();
}));
它('应该默认为登录消息'),注入(函数($compile){
范围。$digest();
var text=element.text();
expect(text).toBe('请登录');
}));
它('默认消息应包含登录页面的链接'),注入(函数($compile){
范围。$digest();
var href=element.find('a').attr('href');
expect(href).toBe('#/login');
}));
});

如果应用程序中的一切正常,但测试中出现错误,则需要将firebase添加到Karma的文件中。找到您的karma.conf.js(在yeoman中,将其添加到Grunfile.js的karma套件中),并使其类似于以下内容:

karma: {
  options: {
    ...
    files: [
      ...
      'https://cdn.firebase.com/v0/firebase.js',
      'app/bower_components/angular-fire/angularFire.js',
      ...
    ],
    ...
  }
  ...
}
然后在您的规范中包括firebase:

beforeEach(module('Simplift', 'firebase'));
每次您需要使用firebase服务时:

describe/it('some test desc ...', inject(function (..., $firebase) {
  // now we can use $firebase!!
  fireSync = $firebase(new Firebase('https://app_name.firebaseio.com'));
  ...
}));

我花了很长时间才弄明白这一点,并希望它能减轻某人的压力。这对我现在来说是可行的,但可能不是最干净的方法(请提供建议!),因为您实际上并没有删除firebase数据,但您可以向firebase DB添加一个“测试”url。

这个问题的实际解决方案是什么?我还没有发现这个答案有用,delisdeli下面的答案实际上帮助很大!
describe/it('some test desc ...', inject(function (..., $firebase) {
  // now we can use $firebase!!
  fireSync = $firebase(new Firebase('https://app_name.firebaseio.com'));
  ...
}));