Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在eventSpy上测试期望值_Javascript_Backbone.js_Jasmine_Sinon - Fatal编程技术网

Javascript 如何在eventSpy上测试期望值

Javascript 如何在eventSpy上测试期望值,javascript,backbone.js,jasmine,sinon,Javascript,Backbone.js,Jasmine,Sinon,我正在尝试在保存时测试backbone.model。 这是我的一段代码。 从注释中可以看出,tohavebeencalledance方法存在问题 附言: 我正在使用jasmine 1.2.0和Sinon.JS 1.3.4 describe('when saving', function () { beforeEach(function () { this.server = sinon.fakeServer.create();

我正在尝试在保存时测试backbone.model。
这是我的一段代码。
从注释中可以看出,tohavebeencalledance方法存在问题

附言:
我正在使用jasmine 1.2.0和Sinon.JS 1.3.4

    describe('when saving', function ()
    {
        beforeEach(function () {
            this.server = sinon.fakeServer.create();
            this.responseBody = '{"id":3,"title":"Hello","tags":["garden","weekend"]}';
            this.server.respondWith(
                'POST',
                Routing.generate(this.apiName),
                [
                    200, {'Content-Type': 'application/json'}, this.responseBody
                ]
            );
            this.eventSpy = sinon.spy();
        });

        afterEach(function() {
            this.server.restore();
        });

        it('should not save when title is empty', function() {
            this.model.bind('error', this.eventSpy);
            this.model.save({'title': ''});

            expect(this.eventSpy).toHaveBeenCalledOnce(); // TypeError: Object [object Object] has no method 'toHaveBeenCalledOnce'
            expect(this.eventSpy).toHaveBeenCalledWith(this.model, 'cannot have an empty title');
        });
    });

log(expect(this.eventSpy))


茉莉花没有任何功能
来进行催眠
。你需要自己检查一下计数

expect(this.eventSpy).toHaveBeenCalled();
expect(this.eventSpy.callCount).toBe(1);
所以我想在你的情况下,你会想要这个:

expect(this.eventSpy.callCount).toBe(1);
expect(this.eventSpy).toHaveBeenCalledWith(this.model, 'cannot have an empty title');
更新 你现在得到的错误,“预期是间谍,但得到了函数”正是因为这个。您正在使用Sinon库间谍,并将其传递给一个Jasmine函数,该函数需要一个Jasmine间谍

您应该执行以下任一操作:

this.eventSpy = jasmine.createSpy();

你为什么要把西农和茉莉花一起使用?我推荐第一种解决方案,因为测试失败时Jasmine会有更多信息显示。

有一个名为的库,它向Jasmine添加了sinon特定的匹配器

它允许你做像这样的事情

expect(mySpy).toHaveBeenCalledOnce();
expect(mySpy).toHaveBeenCalledBefore(myOtherSpy);
expect(mySpy).toHaveBeenCalledWith('arg1', 'arg2', 'arg3');
尝试以下方法:


这与您之前的问题重复:-只有测试的上下文已更改,而您得到的错误保持不变。请不要发布重复的问题。@DerickBailey谢谢您的评论。事实上,我将删除前面的问题,因为我认为这更清楚。谢谢。。。正在寻找一种方法来删除我的投票关闭,但没有看到。我在下面发布了我的答案,但在一个相关的注释上,您是否有理由将所有内容都附加到此?看起来你这样做只是把事情弄得一团糟。谢谢,
expect(this.eventSpy.callCount)工作正常。关于
expect(this.eventSpy).toHaveBeenCalledWith
我得到了以下错误:
预期是一个间谍,但得到了函数。
我如何修复它?你能
console.log(this.eventSpy)
并确认那里确实有一个间谍吗?错误指的是我传递给HavebeenCalledWith的参数,因为
expect(this.eventSpy)。toHaveBeenCalledWith
是定义的。在我的例子中,这个模型是Backbone.model的一个实例。错误在于
this.eventSpy
,它只在你调用HaveBeenCalledWith时检查类型。我更新了我的答案。你在哪里设置
this.model
this.apiName
expect(mySpy).toHaveBeenCalledOnce();
expect(mySpy).toHaveBeenCalledBefore(myOtherSpy);
expect(mySpy).toHaveBeenCalledWith('arg1', 'arg2', 'arg3');
expect(this.eventSpy).toHaveBeenCalledTimes(1);