茉莉花spy的javascript getter造成业缘挂起 [英] Jasmine spyOn javascript getter causing karma to hang

查看:144
本文介绍了茉莉花spy的javascript getter造成业缘挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,使用Jasmine来编写一个监视Javascript吸尘器的测试。它使我的测试套件挂起(使用karma + phantomJS),然后最终浏览器断开连接,从未进一步比相关测试进一步。



一个简单的例子可能是解释最简单的方法(使用webpack + babel-loader打开的ES6):

  class ExampleClass {
get name(){
returnMy Name;
}
}

为了更改这个get方法为我的测试,我正在尝试以下:

  describe(example class getter),function(){
it (应该返回蓝色,function(){
let exampleClass = new ExampleClass();
spyOn(exampleClass,'name')。and.returnValue('blue');
expect (exampleClass.name).toBe('blue');
});
});

这将导致以下内容(相关测试是我的第7个测试):

  PhantomJS 1.9.8(Mac OS X 0.0.0):执行6个成功(0秒/ 0.02秒)
WARN [PhantomJS 1.9.8(Mac OS X 0.0.0)]:断开连接(1次),因为10000 ms内没有信息。
PhantomJS 1.9.8(Mac OS X 0.0.0):执行6个,共8个DISCONNECTED(10.003 secs / 0.02 secs)
DEBUG [karma]:运行完成,退出。

spyOn正在为其他使用get语法定义的方法工作,所以我有信心建造透水管道工作正常。



有没有人看过这个,或者有任何关于修复的想法?

解决方案

我已经一样了这是我解决这个问题的方法:

  class Foo {
get status(){
return 0;
}
}

所以嘲笑这个Foo进行测试:

  class FooMock extends Foo {
_fakeStatus(){
return 1;
}

get status(){
return this._fakeStatus();
}
}

然后你使用 FooMock 而不是 Foo !例如:

 它('检查状态',()=> {
spyOn(fooInstance,'_fakeStatus ').and.returnValue(3);
expect(fooInstance.status).toBe(3);
}

希望这适合你!:)


I'm having some issues using Jasmine to write tests which spy on a Javascript getter. It causes my test suite to hang (using karma + phantomJS) and then ultimately the browser disconnects having never progressed further than the test in question.

A simple example is probably the easiest way to explain (using ES6 transpiled with webpack + babel-loader):

class ExampleClass {
    get name() {
        return "My Name";
    }
}

In order to change what this get method returns for my test, I am trying the following:

describe("example class getter"), function() {
    it("should return blue", function() {
        let exampleClass = new ExampleClass();
        spyOn(exampleClass, 'name').and.returnValue('blue');
        expect(exampleClass.name).toBe('blue');
    });
});

This results in the following (where the test in question is my 7th test):

PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 6 of 8 SUCCESS (0 secs / 0.02 secs)
WARN [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Disconnected (1 times), because no message in 10000 ms.
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 6 of 8 DISCONNECTED (10.003 secs / 0.02 secs)
DEBUG [karma]: Run complete, exiting.

spyOn is working for other methods which aren't defined using the get syntax, so I am confident the build pipeline for transpilation is working fine.

Has anyone seen this before, or have any ideas about a fix?

解决方案

I've got the same. Here's the way I've solved this:

class Foo {
  get status() {
    return 0;
  }
}

So mock this Foo for test:

class FooMock extends Foo {
  _fakeStatus() {
    return 1;
  }

  get status() {
    return this._fakeStatus();
  }
}

And then you use the FooMock instead the Foo! e.g:

it('check the status', () => {
  spyOn(fooInstance, '_fakeStatus').and.returnValue(3);
  expect(fooInstance.status).toBe(3);
}

Hope this works for you! :)

这篇关于茉莉花spy的javascript getter造成业缘挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆