茉莉花对象“没有方法”和“返回” [英] Jasmine object “has no method 'andReturn'”

查看:152
本文介绍了茉莉花对象“没有方法”和“返回”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jasmine的初学者,与Jasmine Spies的第一次尝试。我以为我在模仿在这里显示 (搜索:andReturn)的格式,但我得到了我无法解决的错误:

Beginner with Jasmine, very first attempt with Jasmine Spies. I thought I was mimicking the format displayed here (search: "andReturn"), but I'm getting an error that I can't work out:

TypeError: Object function () {
        callTracker.track({
          object: this,
          args: Array.prototype.slice.apply(arguments)
        });
        return spyStrategy.exec.apply(this, arguments);
      } has no method 'andReturn'

不知道我做错了什么。这是我的规格:

No clue what I'm doing wrong. Here's my Spec:

describe('Die', function() {
    it('returns a value when you roll it', function() {
        var die = Object.create(Die);
        spyOn(Math, 'random').andReturn(1);
        expect(die.roll()).toEqual(6);
    });
});

相应的JS:

var Die = 
{   
    roll: function() {
        return Math.floor(Math.random() * 5 + 1);
    }
}

感谢您的帮助!!!

推荐答案

jasmine 2.0改变了一些间谍语法。 jasmine 2.0 docs

jasmine 2.0 changed some of the spy syntax. jasmine 2.0 docs

spyOn(Math, 'random').and.returnValue(1);

这篇关于茉莉花对象“没有方法”和“返回”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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