为什么对角度的茉莉花测试不会调用$资源?

时间:2014-10-07 13:03:32

标签: angularjs angular-mock

看起来很简单,但无论我是否使用$httpBackend,它都永远不会返回!

    describe('R', function(){
      var Res, http, item, original = {name:"John"};
      beforeEach(function () {
        module('app');
        inject(function ($httpBackend, $resource) {
            http = $httpBackend;
            Res = $resource('/api/user/:user', {user:'@id'});
        });
      });
      beforeEach(function (done) {
        http.when('GET','/api/user/1').respond(original);
        console.log("I GET CALLED");
        Res.get({id:'1'},function (res) {
            console.log("I NEVER GET CALLED");
            item = res;
            done();
        });
      });
      it('should just be true', function(){
        expect(true).toBe(true);
      });
});

为什么第二个console.log从不被调用?

1 个答案:

答案 0 :(得分:1)

我明白了。花了很多时间来解决这个问题(好吧,其中很多都是时间问题,其中jsfiddle有时会给出一个“未声明的变量描述”错误,这个错误让我感到沮丧)。简而言之:

    http.when('GET','/api/user/1').respond(original);
    console.log("I GET CALLED");
    Res.get({id:'1'},function (res) {
        console.log("I NEVER GET CALLED");
        item = res;
        done();
    });
    http.flush();

唉...