Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 Can';Don’别让柴去工作_Javascript_Unit Testing_Chai - Fatal编程技术网

Javascript Can';Don’别让柴去工作

Javascript Can';Don’别让柴去工作,javascript,unit-testing,chai,Javascript,Unit Testing,Chai,请不要建议使用Sinon。我想让柴特务特别是柴特务在你的帮助下工作。基本上,我有这个规范。在PatientController中的initialize方法中,我称之为.initializePatientEvents() 但是,测试失败,出现此错误 AssertionError: expected { Spy } to have been called at Context.<anonymous> AssertionError:预期已调用{Spy} 在上下文中。 我花了将近3个小时

请不要建议使用Sinon。我想让柴特务特别是柴特务在你的帮助下工作。基本上,我有这个规范。在PatientController中的initialize方法中,我称之为.initializePatientEvents()

但是,测试失败,出现此错误

AssertionError: expected { Spy } to have been called
at Context.<anonymous>
AssertionError:预期已调用{Spy}
在上下文中。

我花了将近3个小时都没有运气(

将我的评论移到此处:

查看您的代码,我只是不确定此引用所指的是什么。根据您的错误消息,它似乎与上下文有关。因此,我尝试以下方法:

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相关的内容

编辑: 这是我在本地设置的东西,我能够通过测试。主要区别在于,我没有使用主干,而是创建了自己的构造函数

"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;
    });
});

如果
PatientController
构造函数是调用
initializePatientEvents
的构造函数,则spy创建的时间会有点偏离。目前,spy函数关系的顺序是:

  • 调用函数
  • 监视功能
  • 希望spied on函数调用ben
  • 由于函数在调用时未被监视,因此间谍会完全错过调用。顺序应为:

  • 监视功能
  • 调用函数
  • 希望spied on函数调用ben

  • 但是,您正处于一种棘手的情况,即您监视的对象在调用构造函数之后才存在。一种解决方法是断言
    initializePatientEvents
    的效果已经发生,而不是断言调用了该函数。

    不确定这是否相关,但这与
    有什么关系s
    在beforeach内的匿名函数中引用?通常,当我进行单元测试时,我会在beforeach外定义变量,然后在beforeach内分配它们的值。尝试在beforeach外声明变量
    patientController
    ,然后分配新的patientController对象。你是说使用patientController而不是这个。patientController?1)
    Beforeach
    description
    、it是测试框架(mocha/jasmine/sinon)的功能,所以你的问题没有任何意义。2) 即使每个之前的
    都在运行,那也是在您注册spy之前,因此您的InitializePaientEvents可能是从其构造函数运行的(在spy注册之前)。你需要一种调用initializePatientEvents的方法,因为你在测试中没有做任何事情来触发它。是的,这就是我尝试过的,但仍然不起作用。我可能需要对控制器进行故障排除。谢谢!也许我对这个过程理解不正确。第二个参数应该指向上下文中定义的常规函数,在本例中是patientController,对吗?我希望有人能在github中提供一个工作的JSFIDLE,甚至是一个快速的代码,它有chai-spies。现在看看。谢谢
    
    "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;
        });
    });