mercredi 27 mai 2015

using jasmine / spyOn to mock a method only when a particular method is passed

is it possible to mock a method using Jasmine .spyOn(), based only if a certain parameter is a certain value? Basically, imagine I have a GetUser method that I want to mock it such that if it is passed username = 'test', it would send out a test user, else null. Some test fake code would be:

spyOn(userService, "getUser('test')").and.returnValue(testUser);

This would be equivalent to C# / Moq as:

mockUserService.Setup(x => x.getUser("test")).Returns(testuser);

I know this can be done by custom coding using and.callFake, but it seems quite cumbersome. Test code which actually works would be:

spyOn(userService, "getUser").and.callFake(function (username)
{
    if (username == "test")
        return testuser;
    return null
});

Aucun commentaire:

Enregistrer un commentaire