2017-08-29 1 views
0

J'utilise react + expect + enzyme + sinon. Et posez une question à ce sujet.Comment vérifier si la méthode est appelée dans un espion?

Je suis en train de simuler cliquez sur le bouton et vérifier si la méthode action_createNewUserDB est appelée, mais ont une erreur:

TypeError: Attempted to wrap undefined property action_createNewUserDB as function

Voici ma spec:

import React from 'react'; 
import { shallow, mount } from 'enzyme'; 
import expect from 'expect'; 
import sinon from 'sinon'; 
import { UserProfileProfile } from '../UserProfile/UserProfileProfile.component.jsx'; 

describe('Testing new user addition form in <UserProfileProfile> component',() => { 

    var props = { 
    userToEdit : { 
     profile : { 
     name : "Long Lohn", 
     email : "[email protected]", 
     phone : "+1-000-545-11-22", 
     description : "" 
     } 
    }, 
    users: [], 
    action_createNewUserDB : function() {} 
    } 


    var UserTestForm = mount(<UserProfileProfile {...props} />); 

    it('should check if SAVE button exist',() => { 
    expect(UserTestForm.find("button.UserProfile__profile__form__button").length).toEqual(1); 
    }); 

    it('simulates click on SAVE button',() => { 
    sinon.spy(UserProfileProfile.prototype, 'action_createNewUserDB'); 
    var btn = UserTestForm.find("button.UserProfile__profile__form__button"); 
    btn.simulate('click'); 
    expect(spy).toHaveBeenCalled(); 
    }); 
}); 

1) Comment puis-je vérifier si action_createNewUserDB est appelée?

Si je change sinon.spy(UserProfileProfile.prototype, 'action_createNewUserDB'); à

var spy = sinon.spy(props, 'action_createNewUserDB'); 

Je suis une autre erreur:

Error: The "actual" argument in expect(actual).toHaveBeenCalled() must be a spy

J'ai aussi essayé ceci:

var spy = sinon.spy(); 
var props = { 
    userToEdit : { 
     profile : { 
     name : "Long Lohn", 
     email : "[email protected]", 
     phone : "+1-000-545-11-22", 
     description : "" 
     } 
    }, 
    users: [], 
    action_createNewUserDB :() => spy() 
    } 

En outre, j'ai essayé aussi d'utiliser shallow au lieu de mount, mais il n'a pas p.

Je me rends compte qu'il y a un problème avec espion sinon, mais ne comprends pas où exactement.

P.S. console.log (espion) donne ceci:

{ [Function: proxy] 
    isSinonProxy: true, 
    formatters: 
    { c: [Function: c], 
    n: [Function: n], 
    D: [Function: D], 
    C: [Function: C], 
    t: [Function: t], 
    '*': [Function: *] }, 
    reset: [Function: reset], 
    invoke: [Function: invoke], 
    named: [Function: named], 
    getCall: [Function: getCall], 
    getCalls: [Function: getCalls], 
    calledBefore: [Function: calledBefore], 
    calledAfter: [Function: calledAfter], 
    calledImmediatelyBefore: [Function: calledImmediatelyBefore], 
    calledImmediatelyAfter: [Function: calledImmediatelyAfter], 
    withArgs: [Function: withArgs], 
    matchingFakes: [Function: matchingFakes], 
    matches: [Function: matches], 
    printf: [Function: printf], 
    calledOn: [Function], 
    alwaysCalledOn: [Function], 
    calledWith: [Function], 
    calledWithMatch: [Function], 
    alwaysCalledWith: [Function], 
    alwaysCalledWithMatch: [Function], 
    calledWithExactly: [Function], 
    alwaysCalledWithExactly: [Function], 
    neverCalledWith: [Function], 
    neverCalledWithMatch: [Function], 
    threw: [Function], 
    alwaysThrew: [Function], 
    returned: [Function], 
    alwaysReturned: [Function], 
    calledWithNew: [Function], 
    alwaysCalledWithNew: [Function], 
    callArg: [Function], 
    callArgWith: [Function], 
    callArgOn: [Function], 
    callArgOnWith: [Function], 
    throwArg: [Function], 
    yield: [Function], 
    invokeCallback: [Function], 
    yieldOn: [Function], 
    yieldTo: [Function], 
    yieldToOn: [Function], 
    spyCall: { [Function: createSpyCall] toString: [Function: toString] }, 
    called: true, 
    notCalled: false, 
    calledOnce: true, 
    calledTwice: false, 
    calledThrice: false, 
    callCount: 1, 
    firstCall: 
    { proxy: [Circular], 
    thisValue: undefined, 
    args: [], 
    returnValue: undefined, 
    exception: undefined, 
    callId: 0, 
    errorWithCallStack: 
     Error 
      at Function.invoke (C:\Programming\mini-crm\node_modules\sinon\lib\sinon\spy.js:212:19) 
      at proxy (C:\Programming\mini-crm\node_modules\sinon\lib\sinon\spy.js:89:22) 
      at Object.action_createNewUserDB (C:/Programming/mini-crm/src/spec/newuser.spec.js:23:84) 
      at UserProfileProfile.saveUserData (C:/Programming/mini-crm/src/UserProfile/UserProfileProfile.component.jsx:103:15) 
      at onClick (C:/Programming/mini-crm/src/UserProfile/UserProfileProfile.component.jsx:135:96) 
      at Object.invokeGuardedCallback [as invokeGuardedCallbackWithCatch] (C:\Programming\mini-crm\node_modules\react-dom\lib\ReactErrorUtils.js:26:5) 
      at executeDispatch (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginUtils.js:83:21) 
      at Object.executeDispatchesInOrder (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginUtils.js:108:5) 
      at executeDispatchesAndRelease (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginHub.js:43:22) 
      at executeDispatchesAndReleaseSimulated (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginHub.js:51:10) 
      at forEachAccumulated (C:\Programming\mini-crm\node_modules\react-dom\lib\forEachAccumulated.js:26:8) 
      at Object.processEventQueue (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginHub.js:255:7) 
      at C:\Programming\mini-crm\node_modules\react-dom\lib\ReactTestUtils.js:350:22 
      at ReactDefaultBatchingStrategyTransaction.perform (C:\Programming\mini-crm\node_modules\react-dom\lib\Transaction.js:140:20) 
      at Object.batchedUpdates (C:\Programming\mini-crm\node_modules\react-dom\lib\ReactDefaultBatchingStrategy.js:62:26) 
      at Object.batchedUpdates (C:\Programming\mini-crm\node_modules\react-dom\lib\ReactUpdates.js:97:27) 
      at C:\Programming\mini-crm\node_modules\react-dom\lib\ReactTestUtils.js:348:18 
      at ReactWrapper.<anonymous> (C:\Programming\mini-crm\node_modules\enzyme\build\ReactWrapper.js:776:11) 
      at ReactWrapper.single (C:\Programming\mini-crm\node_modules\enzyme\build\ReactWrapper.js:1421:25) 
      at ReactWrapper.simulate (C:\Programming\mini-crm\node_modules\enzyme\build\ReactWrapper.js:769:14) 
      at Context.<anonymous> (C:/Programming/mini-crm/src/spec/newuser.spec.js:32:9) 
      at callFn (C:\Programming\mini-crm\node_modules\mocha\lib\runnable.js:348:21) 
      at Test.Runnable.run (C:\Programming\mini-crm\node_modules\mocha\lib\runnable.js:340:7) 
      at Runner.runTest (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:443:10) 
      at C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:549:12 
      at next (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:361:14) 
      at C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:371:7 
      at next (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:295:14) 
      at Immediate.<anonymous> (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:339:5) 
      at runCallback (timers.js:672:20) 
      at tryOnImmediate (timers.js:645:5) 
      at processImmediate [as _immediateCallback] (timers.js:617:5) }, 
    secondCall: null, 
    thirdCall: null, 
    lastCall: 
    { proxy: [Circular], 
    thisValue: undefined, 
    args: [], 
    returnValue: undefined, 
    exception: undefined, 
    callId: 0, 
    errorWithCallStack: 
     Error 
      at Function.invoke (C:\Programming\mini-crm\node_modules\sinon\lib\sinon\spy.js:212:19) 
      at proxy (C:\Programming\mini-crm\node_modules\sinon\lib\sinon\spy.js:89:22) 
      at Object.action_createNewUserDB (C:/Programming/mini-crm/src/spec/newuser.spec.js:23:84) 
      at UserProfileProfile.saveUserData (C:/Programming/mini-crm/src/UserProfile/UserProfileProfile.component.jsx:103:15) 
      at onClick (C:/Programming/mini-crm/src/UserProfile/UserProfileProfile.component.jsx:135:96) 
      at Object.invokeGuardedCallback [as invokeGuardedCallbackWithCatch] (C:\Programming\mini-crm\node_modules\react-dom\lib\ReactErrorUtils.js:26:5) 
      at executeDispatch (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginUtils.js:83:21) 
      at Object.executeDispatchesInOrder (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginUtils.js:108:5) 
      at executeDispatchesAndRelease (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginHub.js:43:22) 
      at executeDispatchesAndReleaseSimulated (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginHub.js:51:10) 
      at forEachAccumulated (C:\Programming\mini-crm\node_modules\react-dom\lib\forEachAccumulated.js:26:8) 
      at Object.processEventQueue (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginHub.js:255:7) 
      at C:\Programming\mini-crm\node_modules\react-dom\lib\ReactTestUtils.js:350:22 
      at ReactDefaultBatchingStrategyTransaction.perform (C:\Programming\mini-crm\node_modules\react-dom\lib\Transaction.js:140:20) 
      at Object.batchedUpdates (C:\Programming\mini-crm\node_modules\react-dom\lib\ReactDefaultBatchingStrategy.js:62:26) 
      at Object.batchedUpdates (C:\Programming\mini-crm\node_modules\react-dom\lib\ReactUpdates.js:97:27) 
      at C:\Programming\mini-crm\node_modules\react-dom\lib\ReactTestUtils.js:348:18 
      at ReactWrapper.<anonymous> (C:\Programming\mini-crm\node_modules\enzyme\build\ReactWrapper.js:776:11) 
      at ReactWrapper.single (C:\Programming\mini-crm\node_modules\enzyme\build\ReactWrapper.js:1421:25) 
      at ReactWrapper.simulate (C:\Programming\mini-crm\node_modules\enzyme\build\ReactWrapper.js:769:14) 
      at Context.<anonymous> (C:/Programming/mini-crm/src/spec/newuser.spec.js:32:9) 
      at callFn (C:\Programming\mini-crm\node_modules\mocha\lib\runnable.js:348:21) 
      at Test.Runnable.run (C:\Programming\mini-crm\node_modules\mocha\lib\runnable.js:340:7) 
      at Runner.runTest (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:443:10) 
      at C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:549:12 
      at next (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:361:14) 
      at C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:371:7 
      at next (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:295:14) 
      at Immediate.<anonymous> (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:339:5) 
      at runCallback (timers.js:672:20) 
      at tryOnImmediate (timers.js:645:5) 
      at processImmediate [as _immediateCallback] (timers.js:617:5) }, 
    args: [ [] ], 
    returnValues: [ undefined ], 
    thisValues: [ undefined ], 
    exceptions: [ undefined ], 
    callIds: [ 0 ], 
    errorsWithCallStack: 
    [ Error 
     at Function.invoke (C:\Programming\mini-crm\node_modules\sinon\lib\sinon\spy.js:212:19) 
     at proxy (C:\Programming\mini-crm\node_modules\sinon\lib\sinon\spy.js:89:22) 
     at Object.action_createNewUserDB (C:/Programming/mini-crm/src/spec/newuser.spec.js:23:84) 
     at UserProfileProfile.saveUserData (C:/Programming/mini-crm/src/UserProfile/UserProfileProfile.component.jsx:103:15) 
     at onClick (C:/Programming/mini-crm/src/UserProfile/UserProfileProfile.component.jsx:135:96) 
     at Object.invokeGuardedCallback [as invokeGuardedCallbackWithCatch] (C:\Programming\mini-crm\node_modules\react-dom\lib\ReactErrorUtils.js:26:5) 
     at executeDispatch (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginUtils.js:83:21) 
     at Object.executeDispatchesInOrder (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginUtils.js:108:5) 
     at executeDispatchesAndRelease (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginHub.js:43:22) 
     at executeDispatchesAndReleaseSimulated (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginHub.js:51:10) 
     at forEachAccumulated (C:\Programming\mini-crm\node_modules\react-dom\lib\forEachAccumulated.js:26:8) 
     at Object.processEventQueue (C:\Programming\mini-crm\node_modules\react-dom\lib\EventPluginHub.js:255:7) 
     at C:\Programming\mini-crm\node_modules\react-dom\lib\ReactTestUtils.js:350:22 
     at ReactDefaultBatchingStrategyTransaction.perform (C:\Programming\mini-crm\node_modules\react-dom\lib\Transaction.js:140:20) 
     at Object.batchedUpdates (C:\Programming\mini-crm\node_modules\react-dom\lib\ReactDefaultBatchingStrategy.js:62:26) 
     at Object.batchedUpdates (C:\Programming\mini-crm\node_modules\react-dom\lib\ReactUpdates.js:97:27) 
     at C:\Programming\mini-crm\node_modules\react-dom\lib\ReactTestUtils.js:348:18 
     at ReactWrapper.<anonymous> (C:\Programming\mini-crm\node_modules\enzyme\build\ReactWrapper.js:776:11) 
     at ReactWrapper.single (C:\Programming\mini-crm\node_modules\enzyme\build\ReactWrapper.js:1421:25) 
     at ReactWrapper.simulate (C:\Programming\mini-crm\node_modules\enzyme\build\ReactWrapper.js:769:14) 
     at Context.<anonymous> (C:/Programming/mini-crm/src/spec/newuser.spec.js:32:9) 
     at callFn (C:\Programming\mini-crm\node_modules\mocha\lib\runnable.js:348:21) 
     at Test.Runnable.run (C:\Programming\mini-crm\node_modules\mocha\lib\runnable.js:340:7) 
     at Runner.runTest (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:443:10) 
     at C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:549:12 
     at next (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:361:14) 
     at C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:371:7 
     at next (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:295:14) 
     at Immediate.<anonymous> (C:\Programming\mini-crm\node_modules\mocha\lib\runner.js:339:5) 
     at runCallback (timers.js:672:20) 
     at tryOnImmediate (timers.js:645:5) 
     at processImmediate [as _immediateCallback] (timers.js:617:5) ], 
    displayName: 'spy', 
    toString: [Function: toString], 
    instantiateFake: [Function: create], 
    id: 'spy#0' } 

Répondre

1

J'ai changé expect(spy).toHaveBeenCalled() en sinon.assert.called(spy); et maintenant cela fonctionne. toHaveBeenCalled est la fonction de jasmin, pas Sinon

P.S. Il est également possible d'utiliser expect(spy.called).toEqual(true);