1

I'm trying to make a mock object with a function for a test in jasmine, and it's telling me that when it gets to the lines where the function is called.

the test is: it("Parse.User should be called to institute new object", function () {

    var user = 
    $scope.user.username = "wombatter";
    $scope.user.password = "wombatter";
    $scope.user.email = "wombatter@wombatter.com";

    spyOn(Parse, 'User');

    var user = {set: function(key, val){ this[key] = val; } }
    spyOn(user, 'set');
    // expect(user).toBeDefined();

    $scope.registerNowClick();
    expect(Parse.User).toHaveBeenCalled();
    // expect(user.set).toHaveBeenCalled();
});

And here's the code:

    var user = new Parse.User();

    user.set("username", $scope.user.username);
    user.set("password", $scope.user.password);
    user.set("email", $scope.user.email);

    console.log(JSON.stringify(user));

So the problem is that with unit tests, they don't make any calls outside to another server, and therefore I have to mock the object user that new Parse.User would otherwise create.

I tried that with the line var user = {set: function(key, val){ this[key] = val; } }, and spied on it with Jasmine, but I'm still getting the error user.set is not a function.

Still doing something wrong?

EDIT:

I tried doing this var user = {set: function(key, val) { this[key] = val }}

    // var spy = sinon.spy(user, "set");
    spyOn(user, "set").and.CallFake(function(){
        console.log("Fake user.set called");
    });

And that didn't work, i got the error message TypeError: spyOn(...).and.CallFake is not a function.

Hmm.

Tried injecting a mock Parse object in the forEach block:

beforeEach(function () {

    module(function ($provide){

        injectedUser = { 
            set: function(key, val){ 
                this[key] = val;
            }
        };

        injectedParse = {
            User: function() { 
                return injectedUser;
            }
        };

        mockParse = function() { 
            return injectedParse;
        };

        $provide.constant('Parse', mockParse);
    });

    module("app");

And in the it block:

it("Parse.User should be called to institute new object", inject (function (Parse) {

    $scope.user.username = "wombatter";
    $scope.user.password = "wombatter";
    $scope.user.email = "wombatter@wombatter.com";

    spyOn(Parse, 'User')

But I'm still getting errors, this one is Error: User() method does not exist

I posted the same question on my other account so I can send some bounty out on it in a couple days.

realization
  • 189
  • 12

0 Answers0