在Ajax方法间谍调用上测试对象的茉莉花状态
Jasmine test state of object on Ajax method spy call
我正在单元测试一个Angular控制器,它使用Rails资源工厂来处理从Rails应用程序获取和发送模型数据。发送是通过模型上的方法完成的,例如(与模型$scope.resource
):
$scope.resource.update().then(successHandler, failureHandler);
我在这个方法上有一个间谍,以存根Ajax调用,以便我可以对控制器进行单元测试:
resUpdateSpy = spyOn($scope.resource, 'update').and.callFake(function() {
return {then: function(success, failure){ success(resUpdateResponse); }};
});
在我的一个控制器方法中,我希望将资源与某些数据(特别是Stripe数据)一起post。在同一方法中的POST之后,数据将被覆盖,因此之后我无法测试模型的状态。理想情况下,我想这样做:
expect($scope.resource.update).toHaveBeenCalled().whileValueOf($scope.resource.stripeKey).isEqualTo('tok123');
显然,这种方法在香草茉莉中不存在。在Jasmine中是否有一种方法(无论是vanilla还是通过第三方项目)可以在调用给定的spy时测试值的状态?或者是否有另一种方法来测试这种情况-具体来说,模型在其数据发布之前的状态-我错过了?
我在Angular 1.3.14应用上运行Jasmine 2.2.0和茶匙1.0.2。
您可以添加自己的jasme匹配器,您需要的将是
jasmine.Matchers.prototype.toBeResolvedWith = function() {
var done, expectedArgs;
expectedArgs = jasmine.util.argsToArray(arguments);
if (!this.actual.done) {
throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');
}
done = jasmine.createSpy('done');
this.actual.done(done);
this.message = function() {
if (done.callCount === 0) {
return ["Expected spy " + done.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but it was never resolved.", "Expected spy " + done.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but it was."];
} else {
return ["Expected spy " + done.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(done.argsForCall), "Expected spy " + done.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(done.argsForCall)];
}
};
return this.env.contains_(done.argsForCall, expectedArgs);
};
更多信息请点击这里https://gist.github.com/gr2m/2191748
jasmine基本上支持自定义匹配器。一些内置匹配器是toBe, toEqual等。你可以添加自己的自定义匹配器来检查承诺。
var customMatchers = {
toHaveBeenResolved: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var result = {};
// do you comparison logic here
result.pass = true/false;
result.message = 'some message about test result';
return result;
}
}
。Actual是一个承诺,你可以像这样解决它
result = {};
promise.then(function(value){
result.value = value;
result.status = 'Resolved';
}, function(value){
result.value = value;
result.status = 'Rejected';
});
一旦你声明了你的自定义匹配器,在beforeEach回调的测试用例中使用它。
beforeEach(function() {
jasmine.addMatchers(customMatchers);
});
由于您已经让您的间谍将调用转发给一个假函数,因此让该假函数为您存储感兴趣的值:
var scopeResourceStripeKeyDuringSpyCall = '(spy not called, yet)';
resUpdateSpy = spyOn($scope.resource, 'update').and.callFake(function() {
scopeResourceStripeKeyDuringSpyCall = $scope.resource.stripeKey;
return {then: function(success, failure){ success(resUpdateResponse); }};
});
然后检查断言中的存储值:
expect(scopeResourceStripeKeyDuringSpyCall).toEqual('tok123');
(取决于间谍设置和断言是否在同一作用域中,用于存储值的变量可能必须不那么本地)
相关文章:
- 引用对象中的通用值
- jQuery匹配JSON对象的部分文本
- 节点导出返回一个空对象
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 全局变量和全局对象的属性之间有什么区别吗
- 比较从函数和生成的日期对象
- Javascript,访问一个主要对象模块模式中的每个对象
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 调整窗口大小时,可拖动的对象会出现在容器外部
- 如何使用javascript从主svg对象动态创建svg视图框
- 为什么茉莉花间谍不't通过引用解析函数对象
- 在茉莉花中传递“this”对象流星方法
- 如何根据茉莉花单元测试中的其他对象测试功能
- 如何使用茉莉花检查对象是否包含项目
- 茉莉花测试:对象没有't支持属性或方法
- 在Ajax方法间谍调用上测试对象的茉莉花状态
- 在 angularjs 中全局模拟对象,用于茉莉花/业力测试
- 用茉莉花测试嵌套对象