本文介绍了无法让chai.spy.on上班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要建议使用Sinon.在您的帮助下,我想让chai-spies特别是chai.spy.on.基本上,我有这个规格.在PatientController的initialize方法内部,我将其称为this.initializePatientEvents();

Please don't suggest to use Sinon. I want to get chai-spies specifically chai.spy.on working with your help. Basically, I have this spec. Inside my initialize method in PatientController, I call this.initializePatientEvents();

beforeEach(function() {
  this.patientController = new PatientController({model: new Backbone.Model(PatientModel)});
});

it('executes this.initializePatientEvents', function () {
  let spy = chai.spy.on(this.patientController, 'initializePatientEvents');
  expect(spy).to.have.been.called();
});

但是,测试失败并显示此错误

However, the test is failing with this error

AssertionError: expected { Spy } to have been called
at Context.<anonymous>

我现在花了将近3个小时,没有运气!:(

I spent almost 3 hours now with no luck! :(

推荐答案

在上方将我的评论移至此处:

Moving my comment above to a response here:

看看您的代码,我不确定 this 引用指的是什么.根据您的错误消息,它似乎与上下文有关.因此,我会尝试这样的事情:

Looking at your code, I'm just not sure what the this reference refers to. And based on your error message, it seems like its related to something about the context. Therefore, I'd try something like this:

var patientController;

beforeEach(function() {
    patientController = new PatientController({model: new Backbone.Model(PatientModel)});
});

it('executes this.initializePatientEvents', function () {
    let spy = chai.spy.on(patientController, 'initializePatientEvents');
    expect(spy).to.have.been.called();
});

如果这不起作用,则它更特定于您对 PatientController initializePatientEvents 方法的实现,而不是与chai.spy相关.

If this doesn't work, then its more specific to your implementation of patientController and the initializePatientEvents method, and not something related to chai.spy.

这是我在本地设置的,并且能够通过测试.主要区别在于,我没有使用Backbone,而是创建了自己的构造函数.

Here's something I set up locally and I was able to get a passing test. The main difference is that instead of using Backbone, I just created my own constructor function.

"use strict";
var chai = require("chai");
var sinon = require("sinon");
var sinonChai = require("sinon-chai");
chai.use(sinonChai);
var expect = chai.expect;
var should = chai.should();

describe("PatientController Test", function() {
    var PatientController;
    var initializePatientEventsSpy;
    var patient;

    beforeEach(function() {
        PatientController = function(name, age) {
            this.name = name;
            this.age = age;
            this.initializePatientEvents();
        };

        PatientController.prototype.initializePatientEvents = function() {
            console.log("Do some initialization stuff here");
        };

        initializePatientEventsSpy = sinon.spy(PatientController.prototype, "initializePatientEvents");
    });

    it("should test initializePatientEvents was called", function() {
        patient = new PatientController("Willson", 30);
        initializePatientEventsSpy.should.have.been.called;
    });
});

这篇关于无法让chai.spy.on上班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:41