茉莉花spy的javascript getter造成业缘挂起 [英] Jasmine spyOn javascript getter causing karma to hang
问题描述
一个简单的例子可能是解释最简单的方法(使用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屋!