Allow spies to be easily-extended, w/o modifying Jasmine code, like Matchers do #279

Closed
shamansir opened this Issue Sep 10, 2012 · 2 comments

Comments

Projects
None yet
2 participants

It is a feature request.

I think it would be very nice if we'd have an ability to extend spies functionality without modifying basic Jasmine code, in the similar way we do with matchers using addMatcher.

I see users want a lot of rare or commonly used features from spies (like checking if one spy was called before another or even check the order of the spies calls (which for sure may be uncomfortly achieved with a sequences of call-next-then-reset)).

Currently, createSpy returns a function that is extended with some properties, this function (spyObj) is inside of a closure, so it is not accessible at all, and to substitute it we need to create somewhat like a proxy object to the actual spy.

As an example of confusion, I've wanted to add .performedAt timestamp to the calls (as one of the ways to check the order)

var originalCreateSpy = jasmine.createSpy,
    improvedCreateSpy = function(name) {
        var actualSpy = originalCreateSpy(name);
        var improvedSpyObj = function() {
            var result = actualSpy.apply(this, arguments);
            actualSpy.mostRecentCall.performedAt = new Date();
            return result;
        };
        // if I'll assign something to improvedSpyObj from here, it will not be passed to the actualSpy
        // jasmine.util.extend(improvedSpyObj, actualSpy) — will not help
        // creating new Spy with new jasmine.Spy() and returning it will also not help,
        // since calls from the function will be passed to actualSpy
        return improvedSpyObj;
    };

jasmine.createSpy = improvedCreateSpy;
Contributor

ragaskar commented Oct 6, 2012

Sounds like a good idea! Tracker story here: https://www.pivotaltracker.com/story/show/37288941

Thanks!

@ragaskar ragaskar closed this Oct 6, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment