javascript jasmin:类的模拟实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22654683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:31:34  来源:igfitidea点击:

jasmin: mock instance of a class

javascripttddjasmine

提问by Andrew

Is it possible to create a mock of an object objsuch that Jasmine test like

是否可以创建一个对象obj的模拟,这样 Jasmine 测试就像

expect(fakeB instanceof B).toBe(true);

passes?

通过?

In other words, I have a class Awith a method convertToBwhich argument must be an instance of a class B:

换句话说,我有一个带有方法convertToB的类A,该方法的参数必须是类B的实例:

function A(){
  this.convertToB = function(elem){
    if (!(elem instanceof B)){ throw an error}
    ...
    ... 
  }
}

I would like to cover with test that piece of code by creating a mock object that when being asked whether it is an instance of Bwould respond true.

我想通过创建一个模拟对象来测试那段代码,当被问及它是否是B的实例时,它会响应true

For the moment I am forced to write tests kind of

目前我被迫编写测试

  1. it('throws an error if the argument is a string')
  2. it('throws an error if the argument is an array')
  3. ...
  1. it('如果参数是字符串则抛出错误')
  2. it('如果参数是数组则抛出错误')
  3. ...

which is a bit annoying. I expect a command like

这有点烦人。我期待像这样的命令

 var fakeB = jasmine.createFake('B')

so that the first line of code in this question would pass.

这样问题中的第一行代码就会通过。

回答by Wojciech Szela

I have dozens of places in my code exactly as yours. spyOn method of Jasmine 2.0 will do the job. Same with Jasmine 1.0 but I don't remember if the method is called/used exactly the same way. To the example:

我的代码中有几十个地方和你的完全一样。Jasmine 2.0 的 spyOn 方法将完成这项工作。与 Jasmine 1.0 相同,但我不记得该方法的调用/使用方式是否完全相同。举个例子:

var realB = new B();

// change realB instance into a spy and mock its method 'foo' behaviour to always return 'bar'
// it will still respond "true" to "realB instanceof B"
spyOn(realB, 'foo').and.returnValue('bar')

var realC = new C();

// C.baz is expecting instance of B to be passed as first argument
var result = C.baz(realB)

// assuming C.baz return realB.foo() concatenated with '123'
expect(result).toEqual('bar123');

Jasmine documentation has extensive list of examples of spies: http://jasmine.github.io/2.0/introduction.html

Jasmine 文档包含大量间谍示例列表:http: //jasmine.github.io/2.0/introduction.html

回答by g13013

My implementation is as follows:

我的实现如下:

function proxyConstructor(obj) {
  obj = obj || {};
  for (var key in obj) {
    this[key] = obj[key];
  }

  this.prop = 'runtime prop';
  this.instanceMethod(1);
}

var TestClass = jasmine.createSpy(`TestClass.constructor`).and.callFake(proxyConstructor);
TestClass.prototype.instanceMethod = jasmine.createSpy(`TestClass#instanceMethod`);
TestClass.staticMethod = jasmine.createSpy(`TestClass.staticMethod`);

var ins = new TextClass();
expect(ins).toBe(jasmine.any(TestClass)) // success
expect(ins.prop).toBe('runtime prop'); // success
expect(ins.instanceMethod.toHaveBeenCalledWith(1)) // success