[英]AngularJS $scope variable change in .then() unit testing
I'm trying to unit test a function within my controller but am unable to get a $scope
variable to be testable. 我正在尝试对控制器中的函数进行单元测试,但无法获取$scope
变量以进行测试。 I'm setting the variable in my controller's .then()
and want to unit test to make sure this is set appropriately when it hits the .then block. 我正在控制器的.then()
设置变量,并想进行单元测试以确保在命中.then块时将其正确设置。
My test controller code: 我的测试控制器代码:
function submit() {
myService.submit().then(function(responseData){
if(!responseData.errors) {
$scope.complete = true;
$scope.details = [
{
value: $scope.formattedCurrentDate
},
{
value: "$" + $scope.premium.toFixed(2)
},
];
} else {
$scope.submitError = true;
}
});
}
Where this service call goes is irrelevant. 该服务呼叫的去向无关紧要。 It will return JSON with action: 'submitted', 'response' : 'some response'
. 它将返回JSON并带有以下action: 'submitted', 'response' : 'some response'
。 The .then() checks if errors are present on responseData, and if not it should set some details. .then()检查responseData上是否存在错误,如果没有,则应设置一些详细信息。 These $scope.details are what I'm trying to test in my unit test below: 这些$ scope.details是我要在下面的单元测试中测试的内容:
it('should handle submit details', function () {
var result;
var premium = 123.45;
var formattedCurrentDate = "2016-01-04";
var promise = myService.submit();
mockResponse = {
action: 'submitted',
response: 'some response'
};
var mockDetails = [
{
value: formattedCurrentDate
},
{
value: "$"+ premium.toFixed(2)
}
];
//Resolve the promise and store results
promise.then(function(res) {
result = res;
});
//Apply scope changes
$scope.$apply();
expect(mockDetails).toEqual(submitController.details);
});
I'm receiving an error that $scope.details is undefined. 我收到未定义$ scope.details的错误。 I'm not sure how to make the test recognize this $scope data changing within the controller. 我不确定如何使测试识别该$ scope数据在控制器中的变化。
Before each and other functions in my unit test: 在我的单元测试中的每个其他功能之前:
function mockPromise() {
return {
then: function(callback) {
if (callback) {
callback(mockResponse);
}
}
}
}
beforeEach(function() {
mockResponse = {};
module('myApp');
module(function($provide) {
$provide.service('myService', function() {
this.submit = jasmine.createSpy('submit').and.callFake(mockPromise);
});
});
inject(function($injector) {
$q = $injector.get('$q');
$controller = $injector.get('$controller');
$scope = $injector.get('$rootScope');
myService = $injector.get('myService');
submitController = $controller('myController', { $scope: $scope, $q : $q, myService: myService});
});
});
How do I resolve the promise within my unit test so that I can $scope.$digest() and see the $scope variable change? 如何在单元测试中解析promise,以便可以查看$ scope。$ digest()并查看$ scope变量的变化?
You should look how to test promises with jasmine http://ng-learn.org/2014/08/Testing_Promises_with_Jasmine_Provide_Spy/ 您应该看看如何用茉莉花来测试诺言http://ng-learn.org/2014/08/Testing_Promises_with_Jasmine_Provide_Spy/
using a callFake
would do what you try to mock 使用callFake
将做您尝试模仿的事情
spyOn(myService, 'submit').and.callFake(function() {
return {
then: function(callback) { return callback(yourMock); }
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.