hirezio / auto-spies

Create automatic spies from classes
MIT License
181 stars 30 forks source link

To Spy on Subjects #47

Closed hokrc01 closed 2 years ago

hokrc01 commented 3 years ago

When I go to spy on a subject such as in this case

https://stackblitz.com/edit/subjecttest?file=src%2Fapp%2Fapp.component.spec.ts

I get an error

Error: An error was thrown in When(): selectMember@https://subjecttest.stackblitz.io/~/src/app/app.component.ts:24:49 @https://subjecttest.stackblitz.io/~/src/app/app.component.spec.ts:29:23 promisify@https://subjecttest.stackblitz.io/turbo_modules/@hirez_io/jasmine-given@1.0.5/dist/jasmine-given.js:37:33 itCallbackInsideOfThen/</_temp<@https://subjecttest.stackblitz.io/turbo_modules/@hirez_io/jasmine-given@1.0.5/dist/jasmine-given.js:283:36 _catch@https://subjecttest.stackblitz.io/turbo_modules/@hirez_io/jasmine-given@1.0.5/dist/jasmine-given.js:4:20 itCallbackInsideOfThen/<@https://subjecttest.stackblitz.io/turbo_modules/@hirez_io/jasmine-given@1.0.5/dist/jasmine-given.js:282:29 _cycle2@https://subjecttest.stackblitz.io/turbo_modules/@hirez_io/jasmine-given@1.0.5/dist/jasmine-given.js:164:22 _forOf@https://subjecttest.stackblitz.io/turbo_modules/@hirez_io/jasmine-given@1.0.5/dist/jasmine-given.js:191:7 itCallbackInsideOfThen@https://subjecttest.stackblitz.io/turbo_modules/@hirez_io/jasmine-given@1.0.5/dist/jasmine-given.js:277:32 ZoneDelegate.prototype.invoke@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:400:30 ProxyZoneSpec.prototype.onInvoke@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/proxy.js:126:43 ZoneDelegate.prototype.invoke@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:399:56 Zone.prototype.run@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:160:47 runInTestZone@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/jasmine-patch.js:204:38 wrapTestInZone/<@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/jasmine-patch.js:219:24 attempt@https://subjecttest.stackblitz.io/turbo_modules/jasmine-core@3.6.0/lib/jasmine-core/jasmine.js:7063:44 QueueRunner.prototype.run@https://subjecttest.stackblitz.io/turbo_modules/jasmine-core@3.6.0/lib/jasmine-core/jasmine.js:7104:25 runNext@https://subjecttest.stackblitz.io/turbo_modules/jasmine-core@3.6.0/lib/jasmine-core/jasmine.js:7023:18 next@https://subjecttest.stackblitz.io/turbo_modules/jasmine-core@3.6.0/lib/jasmine-core/jasmine.js:7030:11 once/<@https://subjecttest.stackblitz.io/turbo_modules/jasmine-core@3.6.0/lib/jasmine-core/jasmine.js:6924:11 ZoneDelegate.prototype.invoke@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:400:30 Zone.prototype.run@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:160:47 scheduleResolveOrReject/<@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:1318:38 ZoneDelegate.prototype.invokeTask@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:434:35 Zone.prototype.runTask@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:205:51 drainMicroTaskQueue@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:620:39 ZoneTask.invokeTask@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:520:25 ZoneTask/this.invoke@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:505:52 scheduleTask/data.args[0]@https://subjecttest.stackblitz.io/turbo_modules/zone.js@0.11.4/dist/zone.js:3063:36 u/<@https://c.staticblitz.com/d/webcontainer.3a09fea6d6af8fa50aecb2140d2b6d2039950e89.js:15:173530 u@https://c.staticblitz.com/d/webcontainer.3a09fea6d6af8fa50aecb2140d2b6d2039950e89.js:15:173640 r@https://c.staticblitz.com/d/webcontainer.3a09fea6d6af8fa50aecb2140d2b6d2039950e89.js:15:172591 in https://c.staticblitz.com/d/webcontainer.3a09fea6d6af8fa50aecb2140d2b6d2039950e89.js line 15 > eval (line 326)

So what I am looking for is a SpyOnSuject Function. that would allow me to see if "next" has been called.

my current work around is to mock the service.

shairez commented 3 years ago

Thanks @hokrc01 !

So I think what you're looking for is subjectPropsToSpyOn in the configuration right?

hokrc01 commented 3 years ago

yes, that would be useful.

On Sat, Jun 12, 2021 at 6:23 PM Shai Reznik @.***> wrote:

Thanks @hokrc01 https://github.com/hokrc01 !

So I think what you're looking for is subjectPropsToSpyOn in the configuration right?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hirezio/auto-spies/issues/47#issuecomment-860120754, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAYSBFQWAAHSHGU6YQJMWV3TSPM6VANCNFSM4ZVDXOUA .

radzk commented 3 years ago

Thanks @hokrc01 !

So I think what you're looking for is subjectPropsToSpyOn in the configuration right?

But isn't support for this already provided? I mean the example clearly shows it should support subjects image

I have a problem, a simplest possible scenario I would say. I'm testing a function which calls this.service.mySubject.next();. this.service is mocked like: image

I get an error that says next is not a function, which is true because console.log says it has nextWith instead. Is there a different way to approach it? I just want to write expect(subject.next).toHaveBeenCalled(). Thanks

shairez commented 2 years ago

@hokrc01 @radzk Well, this functionality was meant to serve as a way to fake emits of observable / subject props before the actual "action" gets called.

Can you explain the need to spy on next() ? Why not using observer-spy to listen to its emits instead?

hokrc01 commented 2 years ago

Did not know it existed.

shairez commented 2 years ago

@hokrc01 cool, so I'm closing this issue for now