使用`return`和`yield`对单元测试生成器

时间:2017-02-17 21:49:43

标签: unit-testing ecmascript-6 generator babel istanbul

鉴于此功能

function* backflip(query) {
  return yield 123;
}

此测试

describe('backflip', () =>
  it('should do that ^', () =>
    let handlerInstance = handler();
    expect(handlerInstance.next().value).to.equal(123);
    expect(handlerInstance.next().done).to.equal(true);
  );
);

伊斯坦布尔,应该说所有分支都已覆盖,但实际上这涵盖了3个中的4个。删除return可以解决问题。

  • returnyield是否一起反模式?
  • 这是一个错误吗?

对于上下文,Babel将其编译为

"use strict";

var _marked = [backflip].map(regeneratorRuntime.mark);

function backflip(query) {
  return regeneratorRuntime.wrap(function backflip$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return 123;

        case 2:
          return _context.abrupt("return", _context.sent);

        case 3:
        case "end":
          return _context.stop();
      }
    }
  }, _marked[0], this);
}

2 个答案:

答案 0 :(得分:0)

这是一个正在解决的错误。该分支目前尚无法解决。

答案 1 :(得分:-1)

来自控制台的快速示例:

let animationRotateDegres: CGFloat = 0.5
let animationTranslateX: CGFloat = 1.0
let animationTranslateY: CGFloat = 1.0
let count: Int = 1

func wobble() {
    let leftOrRight: CGFloat = (count % 2 == 0 ? 1 : -1)
    let rightOrLeft: CGFloat = (count % 2 == 0 ? -1 : 1)
    let leftWobble: CGAffineTransform = CGAffineTransform(rotationAngle: degreesToRadians(x: animationRotateDegres * leftOrRight))
    let rightWobble: CGAffineTransform = CGAffineTransform(rotationAngle: degreesToRadians(x: animationRotateDegres * rightOrLeft))
    let moveTransform: CGAffineTransform = leftWobble.translatedBy(x: -animationTranslateX, y: -animationTranslateY)
    let conCatTransform: CGAffineTransform = leftWobble.concatenating(moveTransform)

    transform = rightWobble // starting point

    UIView.animate(withDuration: 0.1, delay: 0.08, options: [.allowUserInteraction, .autoreverse], animations: { () -> Void in
        self.transform = conCatTransform
    }, completion: nil)
}

func degreesToRadians(x: CGFloat) -> CGFloat {
    return CGFloat(M_PI) * x / 180.0
}

发电机还有1个状态。