Nash
Nash

Reputation: 95

spyOn: Expected a spy, but got Function

I'm creating some Javascript tests using Jasmine framework. I am trying to use the spyOn() method to ensure a specific function have been called. here is my code

    describe("Match a regular expression", function() {
    var text = "sometext"; //not important text; irrelevant value

    beforeEach(function () {
        spyOn(text, "match");
        IsNumber(text);
    });

    it("should verify that text.match have been called", function () {
        expect(text.match).toHaveBeenCalled();
    });
});

but I'm getting an

Expected a spy, but got Function

error. I tried to remove the spyOn(text, "match"); line, and it gave the same error, it seems that the fucntion spyOn() is not working is it should. Any thought?

Upvotes: 2

Views: 9792

Answers (2)

CSS
CSS

Reputation: 412

I have found that in order to test something like string.match or string.replace, you will not need a spy, but rather to declare text containing what you are matching or replacing and call the function in the beforeEach, then check that the response is equal to what you would expect it to. Here is a simple example:

describe('replacement', function(){
    var text;
    beforeEach(function(){
        text = 'Some message with a newline \n or carriage return \r';
        text.replace(/(?:\\[rn])+/g, ' ');
        text.replace(/\s\s+/g, ' ');
    });
    it('should replace instances of \n and \r with spaces', function(){
        expect(text).toEqual('Some message with a newline or carriage return ');
    });
});

This will be successful. I would also follow up with a replace to cut down multiple spacing to single spacing, given this scenario. Also, in this case, the beforeEach is not necessary as you can use the assignment and call to your function within the it statement and before your expectation. It should work similarly with a string.match operation if you flip it around to read more like expect(string.match(/someRegEx/).toBeGreaterThan(0);.

Hope this helps.

-C§

Edit: Alternatively, you can zip up your str.replace(/regex/); or str.match(/regex/); into a function that is called and use the spyOn there and use spyOn(class, 'function').and.callthrough(); in your beforeEach and use something like expect(class.function).toHaveBeenCalled(); and var result = class.function(someString); (rather than just calling the function) will allow you to test the return value with expect(class.function(someString)).toEqual(modifiedString); for a replace or expect(class.function(someString)).toBeGreaterThan(0); for the match.

Feel free to +1 if this has given greater insight.

Thanks,

Upvotes: 1

Pal Singh
Pal Singh

Reputation: 1974

You are getting that error because its failing on expect method. expect method is expecting a spy to be passed in but its not. To fix this problem do:

var text = new String("sometext");

Your test case will still fail because you are not calling the match method anywhere. If you want it to pass, then you would need to call text.match(/WHATEVER REGEX/) inside the it function.

Upvotes: 0

Related Questions