[英]Spying on something inside a closure - AngularJS Jasmine testing
I have a simple example that I have not been able to solve. 我有一个无法解决的简单示例。 While this example is trivial, I want to know how to spy on this $log.error using Jasmine unit testing. 虽然这个例子很简单,但我想知道如何使用Jasmine单元测试来监视$ log.error。 I believe closures are the issue even though I am mocking the $log and it would work it it was on not in a closure on the service. 我相信关闭是个问题,即使我嘲笑$ log,它也会起作用,因为它不在服务的关闭中。 Below is code for the Angular Service and Tests. 以下是Angular服务和测试的代码。 Anyone know how to spy on something inside the closure?: 有谁知道如何监视闭包内部的内容?:
Angular Code: 角度代码:
afloatApp.service("WebConfigSettingsService", ['$http', '$q', '$log', function ($http, $q, $log) {
var getConfigSettings = function () {
var deferred = $q.defer();
$http({
method: 'GET',
url: '/api/SOMEPATH'
}).success(function (data, status, headers, config) {
deferred.resolve(data);
}).error(function (data, status, headers, config) {
//THIS IS THE $LOG.ERROR test
$log.error("Error loading WebConfigSettings");
deferred.reject(data);
});
return deferred.promise;
};
var webConfigSettings = getConfigSettings().then(function(data) {
return {
classification: angular.lowercase(data.Classification),
environment: angular.lowercase(data.Environment)
};
});
return webConfigSettings;
}]);
Jasmine Test that Fails 茉莉花测试失败
describe('WebConfigSettingsService', function () {
var webConfigSettingsService, $httpBackend, mockLog, $q,
urlExpected = "/api/SOMEPATH",
serverData = {
Classification: 'unclassified',
Environment: 'development'
};
beforeEach(module("myApp"));
beforeEach(inject(function (_$httpBackend_, _$log_, _$q_, _WebConfigSettingsService_) {
webConfigSettingsService = _WebConfigSettingsService_;
$httpBackend = _$httpBackend_;
mockLog = _$log_;
spyOn(mockLog, 'error');
$q = _$q_;
$httpBackend.expectGET(urlExpected);
}));
afterEach(function () {
$httpBackend.flush();
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("should log error when http errors out", function () {
$httpBackend.whenGET("/api/SOMEPATH").respond(500, "Random Error");
//THIS FAILS. Note that mockLog.error spy is set in before each
expect(mockLog.error).toHaveBeenCalled();
});
});
You're not flushing the backend in your test. 您没有在测试中刷新后端。
it("should log error when http errors out", function () {
$httpBackend.whenGET(urlExpected).respond(500, "Random Error");
$httpBackend.flush();
expect(mockLog.error).toHaveBeenCalled();
});
When you add that, you will get an error from the flush in the afterEach because there shouldn't be isn't anything left to flush, take that out. 当您添加它时,您将在afterEach中从刷新中得到一个错误,因为不应该剩下任何要刷新的内容,请将其取出。 Besides, this would cause your verifyNoOutstandingRequest to always pass. 此外,这将导致您的verifyNoOutstandingRequest始终通过。
What is the expectGET in the beforeEach for? beforeEach中的期望值是什么? Seems like it's not needed. 似乎不需要。
BTW, $log is already mocked by angular-mocks, take a look here for more details: https://docs.angularjs.org/api/ngMock/service/ $log 顺便说一句,$ log已经被angular-mocks嘲笑了,在这里看看更多细节: https : //docs.angularjs.org/api/ngMock/service/ $ log
You could take out the spy and do this: 您可以删除间谍并执行以下操作:
it("should log error when http errors out", function () {
$httpBackend.whenGET(urlExpected).respond(500, "Random Error");
$httpBackend.flush();
expect(mockLog.error.logs).toContain(["Error loading WebConfigSettings"]);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.