如何检查方法是否在间谍中被调用?

时间:2017-08-29 19:33:01

标签: javascript reactjs sinon enzyme

我正在使用react + expect + enzyme + sinon。并对此提出疑问。

我试图模拟点击按钮并检查方法action_createNewUserDB是否被调用,但是有错误:

  

TypeError:尝试将未定义属性action_createNewUserDB包装为函数

这是我的规格:

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 : "some@email.com",
        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)如何检查是否调用了action_createNewUserDB?

如果我改变sinon.spy(UserProfileProfile.prototype, 'action_createNewUserDB');

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

我收到了另一个错误:

  

错误:&#34;实际&#34; expect(actual)中的参数.toHaveBeenCalled()必须是间谍

我也试过这个:

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

此外,我还尝试使用shallow代替mount,但它没有帮助。

我意识到sinon间谍有问题,但不知道究竟在哪里。

P.S。 console.log(spy)给出了这个:

{ [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' }

1 个答案:

答案 0 :(得分:1)

知道了!

我将expect(spy).toHaveBeenCalled()更改为sinon.assert.called(spy);,现在可以正常工作了。 toHaveBeenCalled是Jasmine函数,而不是Sinon

P.S。也可以使用expect(spy.called).toEqual(true);