1

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
});
Karl Cassar
  • 6,043
  • 10
  • 47
  • 84
  • Doesn't look *that* cumbersome to me... this is too special a use-case for it to have its own jasmine syntax imo. – doldt May 27 '15 at 09:29
  • too special? If you had to use C# / Moq, you would use that all the time and it is much more convenient. Imagine you have multiple parameters you want to mock for. The test code would get really ugly.I doubt this is possible given the nature of Javascript language, but wanted to ask if there is any possibility that this could be done. – Karl Cassar May 27 '15 at 11:52

0 Answers0