Accessing ENV from createSpy says I am not inside a spec or a before function

15 views
Skip to first unread message

Maayan Glikser

unread,
Aug 10, 2015, 11:43:07 AM8/10/15
to Jasmine Dev
Hi,

I am trying to write an implementation to allow extending spies as per this issue:

However I am having an issue reading my ENV config in the createSpy phase in base.js

I looked over the custom matchers implementation and decided to implement it in a similar way, that is storing the custom spy functions in ENV and in a spec specific manner.
However I am having a problem when I try to read the saved functions in the createSpy function as it says I am not inside a spec or in a before function, which I am when creating the spy in the test.
The issue is that currentRunnable() does not return any result.

The offending function being called from the createSpy function is this:

this.getSpyExtensions = function() {
 
if(!currentRunnable()) {
 
throw new Error('Spy extensions must be fetched in a before function or a spec');
 
}

 
return runnableResources[currentRunnable().id].spyExtensions;
};

I am trying to call this function from createSpy function like this:
j$.getEnv().getSpyExtensions();


And the self test for it:
it("allows adding a custom function to spies", function(done) {

 env
.it('spec defining a custom spy function', function() {

   env
.addSpyExtensions({
     returnMock
: function() {
       
return 'mock';
     
}
   
});

   
var returnMockSpy = j$.createSpy("returnMockSpy");

   env
.expect(returnMockSpy.and.returnMock).toBeDefined();
   env
.expect(returnMockSpy.and.returnMock()).toEqual('mock');
 
});

 
var specExpectations = function(result) {
   expect
(result.status).toEqual('passed');
 
};

 env
.addReporter({ specDone: specExpectations, jasmineDone: done });
 env
.execute();
});

What am I doing wrong?
Should I store the spy functions in a different place?
Am I testing this incorrectly?

I will appreciate if someone could explain why the create spy function thinks it is not called inside a spec, is there a lifecycle i am not aware of?

Thanks

Gregg Van Hove

unread,
Sep 22, 2015, 4:28:09 PM9/22/15
to jasmine...@googlegroups.com
`createSpy` probably shouldn't be groping out for the current `Env` with `j$.getEnv()`. In general we would prefer that any dependencies of objects or methods be passed explicitly to them.

The spy strategy extensions should probably be stored on the runnableResources for the spec as you had planned.

In order to preserve the current interface but allow proper injection, you may need to actually rewire how `createSpy` and `createSpyObj` are added to the `jasmine` global so they have access to the proper `env` in the same way that `expect` and `addMatchers` do.

-Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js-de...@googlegroups.com.
To post to this group, send email to jasmine...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js-dev.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages