使用jasmine.spyOn进行mongoose模式方法

时间:2021-08-07 19:24:35

I'm having trouble getting jasmine spies to work for my mongoose documents. I have a method set up in my User schema like so:

我无法让茉莉花间谍为我的猫鼬文件工作。我在我的用户架构中设置了一个方法,如下所示:

User.methods.doSomething = function() {
   // implementation
}

User is a dependency of the model I'm currently testing and I want to ensure that doSomething is being called correctly. In my tests I have something like:

用户是我正在测试的模型的依赖项,我想确保正确调用doSomething。在我的测试中,我有类似的东西:

spyOn(User.schema.methods, 'doSomething')

If I log out User.schema.methods.doSomething I get the function I expect but when I run the code that calls that method the original implementation is invoked and not the spy. Also I can't do:

如果我注销User.schema.methods.doSomething我得到了我期望的函数但是当我运行调用该方法的代码时,调用原始实现而不是间谍。我也做不到:

spyOn(userInstance, 'doSomething')

In my tests as the userInstance isn't being exposed and I really want to avoid exposing it. Essentially I want to set up a spy on the User document (instance?) prototype. Is that possible?

在我的测试中,因为userInstance没有暴露,我真的想避免暴露它。基本上我想在User文档(实例?)原型上设置一个间谍。那可能吗?

2 个解决方案

#1


3  

Mongoose is copying the methods defined in the schema to the model prototype and only those methods are used. So even though

Mongoose正在将模式中定义的方法复制到模型原型中,并且只使用那些方法。即便如此

User.schema.methods.doSomething === User.prototype.doSomething

if you set:

如果你设置:

spyOn(User.schema.methods, 'doSomething')

it won't get called - User.prototype.doSomething will. Your guess was right, you should just use:

它不会被调用 - User.prototype.doSomething会。你的猜测是对的,你应该使用:

spyOn(User.prototype, 'doSometing');

Don't forget to use and.callThrough if you want to have the original method called after setting up the spy (I fell for that).

如果你想在设置间谍后调用原始方法,请不要忘记使用and.callThrough(我为之倾倒)。

#2


0  

Unfortunately, I couldn't get the spyOn to work on the model prototype. It might be because my 'model' is actually a mongoose subdocument.

不幸的是,我无法让spyOn处理模型原型。这可能是因为我的'模型'实际上是一个猫鼬子文档。

Instead I opted for a proxy function, which I spyOn instead. It's an extra function call, which is a bit annoying, but it works.

相反,我选择了一个代理功能,我反而代之以窥探。这是一个额外的函数调用,这有点烦人,但它的工作原理。

// index.js
Controller._performClone = function(doc) {
  return doc.clone();
};

Controller.clone = function(id) {
  var child = parent.children.id(req.params.id);
  return Controller._performClone(child);
};


// spec.js
describe('Cloning', function() {
  it('should clone a document', function(done) {
    spyOn(Controller, '_performClone').and.returnValue('cloned');
    var rtn = Controller.clone('1');
    expect(rtn).toBe('cloned');

    // `correctChild` is the one you expect with ID '1'
    expect(Controller._performClone).toHaveBeenCalledWith(correctChild);
  });
});

#1


3  

Mongoose is copying the methods defined in the schema to the model prototype and only those methods are used. So even though

Mongoose正在将模式中定义的方法复制到模型原型中,并且只使用那些方法。即便如此

User.schema.methods.doSomething === User.prototype.doSomething

if you set:

如果你设置:

spyOn(User.schema.methods, 'doSomething')

it won't get called - User.prototype.doSomething will. Your guess was right, you should just use:

它不会被调用 - User.prototype.doSomething会。你的猜测是对的,你应该使用:

spyOn(User.prototype, 'doSometing');

Don't forget to use and.callThrough if you want to have the original method called after setting up the spy (I fell for that).

如果你想在设置间谍后调用原始方法,请不要忘记使用and.callThrough(我为之倾倒)。

#2


0  

Unfortunately, I couldn't get the spyOn to work on the model prototype. It might be because my 'model' is actually a mongoose subdocument.

不幸的是,我无法让spyOn处理模型原型。这可能是因为我的'模型'实际上是一个猫鼬子文档。

Instead I opted for a proxy function, which I spyOn instead. It's an extra function call, which is a bit annoying, but it works.

相反,我选择了一个代理功能,我反而代之以窥探。这是一个额外的函数调用,这有点烦人,但它的工作原理。

// index.js
Controller._performClone = function(doc) {
  return doc.clone();
};

Controller.clone = function(id) {
  var child = parent.children.id(req.params.id);
  return Controller._performClone(child);
};


// spec.js
describe('Cloning', function() {
  it('should clone a document', function(done) {
    spyOn(Controller, '_performClone').and.returnValue('cloned');
    var rtn = Controller.clone('1');
    expect(rtn).toBe('cloned');

    // `correctChild` is the one you expect with ID '1'
    expect(Controller._performClone).toHaveBeenCalledWith(correctChild);
  });
});

相关文章