Backbone Jasmine Sinon.Stub TypeError
-
29-10-2019 - |
Question
J'essaie d'écrire une spécification simple pour une collection Todos Backbone qui coupe le modèle TODO Backbone.
Voici mes spécifications:
describe "TodoApp.Collections.Todos", ->
beforeEach ->
@todoStub = sinon.stub window, 'TodoApp.Models.Todo'
afterEach ->
@todoStub.restore()
Cela me donne l'erreur suivante:
TypeError: Attempted to wrap undefined property TodoApp.Models.Todo as function
Le modèle TODO est cependant défini comme TODO = new tododapp.models.todo () ne donne pas d'erreur.
Est-ce un problème de portée? Quelqu'un pourrait-il me pointer dans la bonne direction?
La solution
J'ai aussi rencontré ce problème. Vous devriez l'appeler comme ça ...
beforeEach ->
@todoStub = sinon.stub window.TodoApp.Models, 'Todo'
au lieu de cela.
beforeEach ->
@todoStub = sinon.stub window, 'TodoApp.Models.Todo'
Cela a résolu le problème pour moi
@smek: Cela résout également votre problème à partir de http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html
Autres conseils
La syntaxe que vous utilisez sinon.stub window, 'TodoApp.Models.Todo'
serait pour envelopper window['TodoApp.Models.Todo']
en tant que fonction. http://sinonjs.org/docs/#stubs
Avec Sinon, vous allez probablement emballer une fonction particulière sur votre modèle TODO avec un talon: sinon.stub TodoApp.Models.Todo, 'Foo'
.
Sinon peut Stuche un objet entier Mais je pense qu'il est conçu pour être plus granulaire.