我正在使用react + expect + enzyme + sinon。並有一個關於它的問題。如何檢查間諜中是否調用方法?
我試圖模擬點擊按鈕,檢查方法action_createNewUserDB
被調用,但有一個錯誤:
TypeError: Attempted to wrap undefined property action_createNewUserDB as function
這裏是我的規格:
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)我怎麼能檢查是否調用了action_createNewUserDB?
如果我改變sinon.spy(UserProfileProfile.prototype, 'action_createNewUserDB');
到
var spy = sinon.spy(props, 'action_createNewUserDB');
我得到另一個錯誤:
Error: The "actual" argument in expect(actual).toHaveBeenCalled() must be a spy
我也試過這樣:
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()
}
此外,我還試圖用shallow
而不是mount
,但它並沒有幫助頁。
我意識到有一個問題與sinon間諜,但不明白在哪裏。
P.S. console.log(間諜)給出了這個:
{ [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' }