gpt4 book ai didi

angularjs - 模拟服务以测试 Controller

转载 作者:行者123 更新时间:2023-12-02 19:43:39 24 4
gpt4 key购买 nike

我有一个 ParseService,我想模拟它以测试所有使用它的 Controller ,我一直在阅读有关 jasmine spies 的内容,但我仍然不清楚。有人能给我一个如何模拟自定义服务并在 Controller 测试中使用它的示例吗?

现在我有一个 Controller ,它使用服务来插入一本书:

BookCrossingApp.controller('AddBookCtrl', function ($scope, DataService, $location) {

$scope.registerNewBook = function (book) {
DataService.registerBook(book, function (isResult, result) {

$scope.$apply(function () {
$scope.registerResult = isResult ? "Success" : result;
});
if (isResult) {
//$scope.registerResult = "Success";
$location.path('/main');
}
else {
$scope.registerResult = "Fail!";
//$location.path('/');
}

});
};
});

服务是这样的:

angular.module('DataServices', [])

/**
* Parse Service
* Use Parse.com as a back-end for the application.
*/
.factory('ParseService', function () {
var ParseService = {
name: "Parse",

registerBook: function registerBook(bookk, callback) {

var book = new Book();

book.set("title", bookk.title);
book.set("description", bookk.Description);
book.set("registrationId", bookk.RegistrationId);
var newAcl = new Parse.ACL(Parse.User.current());
newAcl.setPublicReadAccess(true);
book.setACL(newAcl);

book.save(null, {
success: function (book) {
// The object was saved successfully.
callback(true, null);
},
error: function (book, error) {
// The save failed.
// error is a Parse.Error with an error code and description.
callback(false, error);
}
});
}
};

return ParseService;
});

到目前为止我的测试如下:

describe('Controller: AddBookCtrl', function() {

// // load the controller's module
beforeEach(module('BookCrossingApp'));


var AddBookCtrl, scope, book;

// Initialize the controller and a mock scope
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope;
book = {title: "fooTitle13"};
AddBookCtrl = $controller('AddBookCtrl', {
$scope: scope
});
}));

it('should call Parse Service method', function () {

//We need to get the injector from angular
var $injector = angular.injector([ 'DataServices' ]);
//We get the service from the injector that we have called
var mockService = $injector.get( 'ParseService' );
mockService.registerBook = jasmine.createSpy("registerBook");
scope.registerNewBook(book);
//With this call we SPY the method registerBook of our mockservice
//we have to make sure that the register book have been called after the call of our Controller
expect(mockService.registerBook).toHaveBeenCalled();
});
it('Dummy test', function () {
expect(true).toBe(true);
});
});

现在测试失败:

   Expected spy registerBook to have been called.
Error: Expected spy registerBook to have been called.

我做错了什么?

最佳答案

我做错的不是在 beforeEach 中将模拟服务注入(inject)到 Controller 中:

describe('Controller: AddBookCtrl', function() {

var scope;
var ParseServiceMock;
var AddBookCtrl;

// load the controller's module
beforeEach(module('BookCrossingApp'));

// define the mock Parse service
beforeEach(function() {
ParseServiceMock = {
registerBook: function(book) {},
getBookRegistrationId: function() {}
};
});

// inject the required services and instantiate the controller
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
AddBookCtrl = $controller('AddBookCtrl', {
$scope: scope,
DataService: ParseServiceMock
});
}));

it('should call registerBook Parse Service method', function () {
var book = {title: "fooTitle"}

spyOn(ParseServiceMock, 'registerBook').andCallThrough();
//spyOn(ParseServiceMock, 'getBookRegistrationId').andCallThrough();
scope.registerNewBook(book);

expect(ParseServiceMock.registerBook).toHaveBeenCalled();
//expect(ParseServiceMock.getBookRegistrationId).toHaveBeenCalled();
});
});

关于angularjs - 模拟服务以测试 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15854043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com