声明:ITeye资讯文章的版权属于ITeye网站所有,严禁任何网站转载本文,否则必将追究法律责任!
猎头职位:
Johannes Link是一个热爱Agile的小伙子,他对现存的JavaScript单元测试框架不100%满意,他解释了原因,比如他给出了一个例子:
testDoubleSpeaker: function() { with(this) { var actualMsg = null; var mockSay = function(msg) { actualMsg = msg; }; Speaker.say = mockSay; DoubleSpeaker.say('oops'); assertEqual('oopsoops', actualMsg); }}
这个例子能够运行,但是下面的方式是不是更好?
testDoubleSpeaker: function() { with(this) { mock(Speaker).andDo(function() { DoubleSpeaker.say('oops'); verify(Speaker.say)('oopsoops'); }); }},
所以,他给出了一个新的JavaScript mocking框架: MockMe。主要功能是:
1。提供基本颗粒mocking的功能,如果可能,可以伪装一个简单测试功能,而不影响其他内容(比如对象,原型或者全局命名空间等)。
2。很多时候,spying(刺探)比mocking更好,因为前者更简单。spying指的不是在你的mock对象开始测试前,指定期望的特定反馈,而是当测试发生时或之后,你使用mock spy对象来刺探反馈结果。
下面是另外一个例子:
when(f)('in').thenReturn('out'); assertEqual('out', f('in')); when(f)({name: 'hello'}).thenReturn('yeah'); assertEqual('yeah', f({name: 'hello'})); when(f)(any()).thenReturn('yeah'); assertEqual('yeah', f(1)); verify(times(2), f)(1); //succeeds useMockerFor(function(mocker)) { mocker.within(MyObject).mock('f1'); when(MyObject.f1)().thenReturn(5); assertEqual(7, MyObject.f2()); } // Here everything you mocked will automatically be restored assertEqual(3, MyObject.f2());
详细内容可以访问 http://johanneslink.net/projects/mockme.html
- testDoubleSpeaker:function(){with(this){
- varactualMsg=null;
- varmockSay=function(msg){
- actualMsg=msg;
- };
- Speaker.say=mockSay;
- DoubleSpeaker.say('oops');
- assertEqual('oopsoops',actualMsg);
- }}
这个例子能够运行,但是下面的方式是不是更好?
- testDoubleSpeaker:function(){with(this){
- mock(Speaker).andDo(function(){
- DoubleSpeaker.say('oops');
- verify(Speaker.say)('oopsoops');
- });
- }},
所以,他给出了一个新的JavaScript mocking框架: MockMe。主要功能是:
1。提供基本颗粒mocking的功能,如果可能,可以伪装一个简单测试功能,而不影响其他内容(比如对象,原型或者全局命名空间等)。
2。很多时候,spying(刺探)比mocking更好,因为前者更简单。spying指的不是在你的mock对象开始测试前,指定期望的特定反馈,而是当测试发生时或之后,你使用mock spy对象来刺探反馈结果。
下面是另外一个例子:
- when(f)('in').thenReturn('out');
- assertEqual('out',f('in'));
- when(f)({name:'hello'}).thenReturn('yeah');
- assertEqual('yeah',f({name:'hello'}));
- when(f)(any()).thenReturn('yeah');
- assertEqual('yeah',f(1));
- verify(times(2),f)(1);//succeeds
- useMockerFor(function(mocker)){
- mocker.within(MyObject).mock('f1');
- when(MyObject.f1)().thenReturn(5);
- assertEqual(7,MyObject.f2());
- }//Hereeverythingyoumockedwillautomaticallyberestored
- assertEqual(3,MyObject.f2());
详细内容可以访问 http://johanneslink.net/projects/mockme.html