[英]Jasmine Testing Ternary Conditionals
假设我们有以下JavaScript代码。
object = _.isUndefined(object) ? '' : aDifferentObject.property;
我们如何在Jasmine中针对这两种情况编写测试?
是否需要两个单独的描述? 还是我们能够在测试本身中具有三元条件?
谢谢! 杰里米
我将这样使用两个单独的描述
// System Under Test
function getObjectValue() {
return _.isUndefined(object) ? '' : aDifferentObject.property;
}
// Tests
describe('when object is undefined', function() {
it('should return empty string', function() {
expect(getObjectValue()).toBe('');
});
});
describe('when object is no undefined', function () {
it('should return property from different object', function () {
expect(getObjectValue()).toBe(property);
});
});
考虑以下情况(Angular JS / ES6 / Jasmine,控制器“ as”语法)
码:
Controller.toggleWidgetView = () => {
Controller.isFullScreenElement() ? Controller.goNormalScreen() : Controller.goFullScreen();
};
茉莉花中的测试用例:
describe('.toggleWidgetView()', function() {
it('should call goNormalScreen method', function() {
spyOn(Controller, 'isFullScreenElement').and.callFake(function(){
return true;
});
spyOn(Controller, 'goNormalScreen').and.callThrough();
Controller.toggleWidgetView();
expect(Controller.goNormalScreen).toHaveBeenCalled();
});
it('should call goFullScreen method', function() {
spyOn(Controller, 'isFullScreenElement').and.callFake(function(){
return false;
});
spyOn(Controller, 'goFullScreen').and.callThrough();
Controller.toggleWidgetView();
expect(Controller.goFullScreen).toHaveBeenCalled();
});
});
两个测试用例都通过了。 基本上,我们两次调用“ toggleWidgetView”方法,并且在每次调用中,条件都会像在现实世界中一样发生变化(真/假)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.