简体   繁体   English

如何模拟嵌套方法?

[英]How to mock a nested method?

I'm learning more about Jasmine Unit Testing and I've ran into something that I can't figure out. 我正在学习有关Jasmine单元测试的更多信息,并且遇到了一些我不知道的问题。 I'm new to both JavaScript and Unit Testing. 我是JavaScript和单元测试的新手。 I've tried to look for examples about nested methods and mocking them, but I'm still unable to have a successful test. 我试图寻找有关嵌套方法并对其进行模拟的示例,但仍然无法成功进行测试。 I'm making a game with PhaserJS (HTML5 Game Library) and I've written successful tests so far. 我正在使用PhaserJS(HTML5游戏库)制作游戏,到目前为止,我已经编写了成功的测试。 This is an example of my successful test. 这是我成功测试的一个例子。

 function createGameScreenBorder(gameState) {
      var border = gameState.game.add.graphics();
    }

This is my test block. 这是我的测试块。

it("create gamescreen background border", function() {
    gameState.game = {
    add: jasmine.createSpyObj('add', ['graphics'])
    };
      createGameScreenBorder(gameState);
      expect(gameState.game.add.graphics).toHaveBeenCalled();
    });

Now the above code works, it doesn't do much. 现在,上面的代码可以正常工作了,但它并没有太大作用。 What I want is to draw a rectangle which is a method part of the graphics method. 我想要绘制一个矩形,它是graphics方法的方法部分。

function createGameScreenBorder(gameState) {
      var border = gameState.game.add.graphics();
          // drawRect: x, y width, length
          border.drawRect(0, 0, 0, 0);
      }

This is my test block. 这是我的测试块。

    it("create gamescreen background border", function() {
          gameState.game = {
            add: {
              graphics: jasmine.createSpyObj('graphics', ['drawRect'])
            }
          }
          createGameScreenBorder(gameState);
          expect(gameState.game.add.graphics).toHaveBeenCalled();
          expect(gameState.game.add.graphics().lineStyle).toHaveBeenCalledWith(0,0,0,0);
        });

I want to be able to make sure that drawRect() is called with my parameters, but I am confused as to how to do it. 我希望能够确保使用我的参数调用了drawRect(),但是我对如何执行此操作感到困惑。

Thank you! 谢谢!

The gameState.game.add.graphics() returns an object that has a drawRect() method on it. gameState.game.add.graphics()返回一个具有drawRect()方法的对象。

First you want to check if the gameState.game.add.graphics() was called - this is already done. 首先,您要检查是否调用了gameState.game.add.graphics() -已经完成。 Then check if the drawRect() was called on the object returned from this method. 然后检查是否从此方法返回的对象上调用了drawRect() To do that, set up your spy to return an object that also has a spy on it. 为此,将您的间谍程序设置为返回还带有间谍程序的对象。

it("create gamescreen background border", function() {
  let resultObject = {
    drawRect: jasmine.createSpy()
  };

  gameState.game.add = {
    graphics: jasmine.createSpy().and.callFake(() => {
      return resultObject;
    })
  };

  createGameScreenBorder(gameState);

  expect(gameState.game.add.graphics).toHaveBeenCalled();
  expect(resultObject.drawRect).toHaveBeenCalledWith(0, 0, 0, 0);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM