本文介绍了茉莉花和angularjs,不能存根$ httpBackend服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法存根$ httpBackend后返回预期的结果,我不知道什么是错的考验。能否请您看看这个失败的小例子?

I can't seem to return the expected results after stubbing $httpBackend, and I don't know what is wrong with the test. Can you please take a look at this failing minimal example?

query() Expected [ ] to equal [ { hello : 'world' } ].

code

家属资源

<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine.js"></script>
<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine-html.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular-resource.min.js"></script>
<script src="http://code.angularjs.org/1.0.1/angular-mocks-1.0.1.js"></script>

测试JavaScript

Test javascript

var mod = angular.module('mod', ['ngResource']);

mod.factory('Brief', function($resource) {
   var Brief = $resource('http://some_test/:id');
   return Brief;
});

beforeEach(function() {
    module('mod');

    inject(function($injector) {
        resource = $injector.get('$resource');
        $httpBackend = $injector.get('$httpBackend');
        Brief = $injector.get('Brief');
    });

});

afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
});

describe("Brief", function() {
    it("query()", function() {
        var getRequest = 'http://some_test';
        var fakeGetResponse = [{
            "hello": "world"}];

        $httpBackend.when('GET', getRequest).respond(fakeGetResponse);

        var briefs = Brief.query();
        expect(briefs).toEqual(fakeGetResponse);
        $httpBackend.flush();
    });
});


// KICK OFF JASMINE
var jasmineEnv = jasmine.getEnv();
var trivialReporter = new jasmine.TrivialReporter();

jasmineEnv.addReporter(trivialReporter);

jasmineEnv.specFilter = function(spec) {
    return trivialReporter.specFilter(spec);
};

jasmineEnv.execute();​

谢谢!

推荐答案

您已经差不多了,但仍然有在测试2个问题:

You've been almost there, but still there were 2 issues in your test:


  • 的期望是在 $ httpBackend 通红之前验证。这是 $ httpBackend.flush(); 模拟传入响应

  • 在你的期望,你是比较对象实例(而不是它们的值),但AngularJS $资源中的。你需要比较对象,它们的值不引用。这样做的一个方法是:预期(angular.equals(三角裤,fakeGetResponse))toBeTruthy();

  • expectation was verified before the $httpBackend was flushed. It is the $httpBackend.flush(); that simulates an incoming response
  • in your expectation you were comparing object instances (and not their values) but AngularJS $resource will create a new array as described in http://stackoverflow.com/a/11966512/1418796. You need to compare objects for their values not references. One way of doing so is: expect(angular.equals(briefs, fakeGetResponse)).toBeTruthy();

最后,这里是一个的jsfiddle与测试传:

Finally here is a jsFiddle with a test passing: http://jsfiddle.net/VFqFU/

这篇关于茉莉花和angularjs,不能存根$ httpBackend服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:47