存根 $httpBackend 后,我似乎无法返回预期的结果,也不知道测试出了什么问题。你能看看这个失败的最小例子吗?
与演示链接
http://jsfiddle.net/paos/kTNF5/
茉莉花输出
query() Expected [ ] to equal [ { hello : 'world' } ].
代码
依赖资源<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
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();
谢谢!