Creating unit test with jasmine and getting an object as my second call. How do I call the function...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am very new to coding so please bear with me.


I am trying to write a test for this function:



redirect() {
if (!this.userInfo || !this.userInfo.userId) {
const originalPath = this.$location.path();
if (originalPath === '/') {
this.$location.path('/login');
} else {
this.$location.path('/login').search('redirect', originalPath);
}
return;
}


This is my mock:



beforeEach(() => {
mocks = {path: function () {return '/login';},
search: function() {return 'redirect', 'fakepath'}};
ctrl = new BxIndex(mocks);
});


This is my test:



spyOn(ctrl.$location, 'path').and.returnValue(mocks);
spyOn(ctrl.$location, 'search').and.callThrough();
ctrl.redirect();
expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');


It fails because the second call is an object:



Expected spy search to have been called with [ 'redirect', 'fakepath' ] but actual calls were [ 'redirect', Object({ path: spy on path, search: spy on search }) ].









share|improve this question

























  • I don't think there's enough info for me to get too far into it, but... spyOn(ctrl.$location, 'path').and.returnValue(mocks); is saying "when ctrl.$location.path is called, return mocks" (which in this case is an object with path and search functions). I'm guessing that is the part that's tripping you up. What is ctrl.$location.path('abc'); supposed to return?

    – Mike B.
    Nov 27 '18 at 0:29













  • Updated question with some more original code that I am trying to test. I see that originalPath needs to be part of my test but don't know where/how to change that. I am getting that object rather than originalPath

    – Kaitlyn
    Nov 27 '18 at 19:46


















1















I am very new to coding so please bear with me.


I am trying to write a test for this function:



redirect() {
if (!this.userInfo || !this.userInfo.userId) {
const originalPath = this.$location.path();
if (originalPath === '/') {
this.$location.path('/login');
} else {
this.$location.path('/login').search('redirect', originalPath);
}
return;
}


This is my mock:



beforeEach(() => {
mocks = {path: function () {return '/login';},
search: function() {return 'redirect', 'fakepath'}};
ctrl = new BxIndex(mocks);
});


This is my test:



spyOn(ctrl.$location, 'path').and.returnValue(mocks);
spyOn(ctrl.$location, 'search').and.callThrough();
ctrl.redirect();
expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');


It fails because the second call is an object:



Expected spy search to have been called with [ 'redirect', 'fakepath' ] but actual calls were [ 'redirect', Object({ path: spy on path, search: spy on search }) ].









share|improve this question

























  • I don't think there's enough info for me to get too far into it, but... spyOn(ctrl.$location, 'path').and.returnValue(mocks); is saying "when ctrl.$location.path is called, return mocks" (which in this case is an object with path and search functions). I'm guessing that is the part that's tripping you up. What is ctrl.$location.path('abc'); supposed to return?

    – Mike B.
    Nov 27 '18 at 0:29













  • Updated question with some more original code that I am trying to test. I see that originalPath needs to be part of my test but don't know where/how to change that. I am getting that object rather than originalPath

    – Kaitlyn
    Nov 27 '18 at 19:46














1












1








1








I am very new to coding so please bear with me.


I am trying to write a test for this function:



redirect() {
if (!this.userInfo || !this.userInfo.userId) {
const originalPath = this.$location.path();
if (originalPath === '/') {
this.$location.path('/login');
} else {
this.$location.path('/login').search('redirect', originalPath);
}
return;
}


This is my mock:



beforeEach(() => {
mocks = {path: function () {return '/login';},
search: function() {return 'redirect', 'fakepath'}};
ctrl = new BxIndex(mocks);
});


This is my test:



spyOn(ctrl.$location, 'path').and.returnValue(mocks);
spyOn(ctrl.$location, 'search').and.callThrough();
ctrl.redirect();
expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');


It fails because the second call is an object:



Expected spy search to have been called with [ 'redirect', 'fakepath' ] but actual calls were [ 'redirect', Object({ path: spy on path, search: spy on search }) ].









share|improve this question
















I am very new to coding so please bear with me.


I am trying to write a test for this function:



redirect() {
if (!this.userInfo || !this.userInfo.userId) {
const originalPath = this.$location.path();
if (originalPath === '/') {
this.$location.path('/login');
} else {
this.$location.path('/login').search('redirect', originalPath);
}
return;
}


This is my mock:



beforeEach(() => {
mocks = {path: function () {return '/login';},
search: function() {return 'redirect', 'fakepath'}};
ctrl = new BxIndex(mocks);
});


This is my test:



spyOn(ctrl.$location, 'path').and.returnValue(mocks);
spyOn(ctrl.$location, 'search').and.callThrough();
ctrl.redirect();
expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');


It fails because the second call is an object:



Expected spy search to have been called with [ 'redirect', 'fakepath' ] but actual calls were [ 'redirect', Object({ path: spy on path, search: spy on search }) ].






javascript jasmine






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 19:44







Kaitlyn

















asked Nov 26 '18 at 23:55









KaitlynKaitlyn

83




83













  • I don't think there's enough info for me to get too far into it, but... spyOn(ctrl.$location, 'path').and.returnValue(mocks); is saying "when ctrl.$location.path is called, return mocks" (which in this case is an object with path and search functions). I'm guessing that is the part that's tripping you up. What is ctrl.$location.path('abc'); supposed to return?

    – Mike B.
    Nov 27 '18 at 0:29













  • Updated question with some more original code that I am trying to test. I see that originalPath needs to be part of my test but don't know where/how to change that. I am getting that object rather than originalPath

    – Kaitlyn
    Nov 27 '18 at 19:46



















  • I don't think there's enough info for me to get too far into it, but... spyOn(ctrl.$location, 'path').and.returnValue(mocks); is saying "when ctrl.$location.path is called, return mocks" (which in this case is an object with path and search functions). I'm guessing that is the part that's tripping you up. What is ctrl.$location.path('abc'); supposed to return?

    – Mike B.
    Nov 27 '18 at 0:29













  • Updated question with some more original code that I am trying to test. I see that originalPath needs to be part of my test but don't know where/how to change that. I am getting that object rather than originalPath

    – Kaitlyn
    Nov 27 '18 at 19:46

















I don't think there's enough info for me to get too far into it, but... spyOn(ctrl.$location, 'path').and.returnValue(mocks); is saying "when ctrl.$location.path is called, return mocks" (which in this case is an object with path and search functions). I'm guessing that is the part that's tripping you up. What is ctrl.$location.path('abc'); supposed to return?

– Mike B.
Nov 27 '18 at 0:29







I don't think there's enough info for me to get too far into it, but... spyOn(ctrl.$location, 'path').and.returnValue(mocks); is saying "when ctrl.$location.path is called, return mocks" (which in this case is an object with path and search functions). I'm guessing that is the part that's tripping you up. What is ctrl.$location.path('abc'); supposed to return?

– Mike B.
Nov 27 '18 at 0:29















Updated question with some more original code that I am trying to test. I see that originalPath needs to be part of my test but don't know where/how to change that. I am getting that object rather than originalPath

– Kaitlyn
Nov 27 '18 at 19:46





Updated question with some more original code that I am trying to test. I see that originalPath needs to be part of my test but don't know where/how to change that. I am getting that object rather than originalPath

– Kaitlyn
Nov 27 '18 at 19:46












1 Answer
1






active

oldest

votes


















1














The simple answer to get around the problem with this particular test is to change this line:



//spyOn(ctrl.$location, 'path').and.returnValue(mocks);
spyOn(ctrl.$location, 'path').and.returnValue('fakepath');


Edit: Looking at the code being tested, it looks like ctrl.$location.path returns different results depending on how it's called. If it's called with no parameters, then it appears to return a string. If it's called with a string as the parameter, then it appears to return the ctrl.$location object itself. Assuming I'm correct on that, there are two options...



The first option is to just set up your ctrl object for the test so that ctrl.$location.path doesn't need to be mocked (spied) at all, if possible.



The second option is to implement the dual-return logic in your spy using callFake:



spyOn(ctrl.$location, 'path').and.callFake(function(arg) {
if (typeof arg == "string") {
return ctrl.$location;
} else {
return "fakepath";
}
});


End Edit



But I think you're missing some details on how the spyOn functions work, so I will elaborate a little!



When you spy on a function, you're basically replacing that function with a spy - so if you subsequently call that function, it does nothing by default.
But you can add on to your spy too - for instance, you can have it return a specific value, or you can have it go ahead and call through to the original function that it is standing in for. That's what's happening when you use spyOn(...).and.returnValue() and spyOn(...).and.callThrough().



For example, let's say I have this simple object, foo. It has a function called sayHello, which calls another function called send, which sends a message over the network to Bob. If the message is sent successfully, the messagesToBob counter goes up by one, otherwise it remains the same. (send returns true if it worked, or false if it didn't).



// Begin contrived example!
var foo = {
messagesToBob: 0,
sayHello: function() {
if (this.send()) {
this.messagesToBob += 1;
}
},
send: function() {
network.send("Bob", "Hello");
}
};


Now I want to test this, but obviously I don't want to be sending Bob a bunch of messages during my tests. So that's where the spy comes in.



spyOn(foo, "send");


This essentially takes the foo.send function and replaces it with an empty function (I believe it will just returned undefined).



If I want to test the foo increments the counter on a successful run, I can make the "send" spy return true:



spyOn(foo, "send").and.returnValue(true);
foo.sayHello();

expect(foo.messagesToBob).to.equal(1);


If I want to test that it does not increment on a failed send, I can make the "send" spy return false.



In any of those cases, I could also check the status of the spy function itself using the expect(...).toHaveBeenCalled() or .toHaveNotBeenCalled() (or whatever functions are available to your particular testing framework/environment).



Anyway, I hope that helps! Your "mock" object's functions don't need to return a value in this example (assuming you need that object at all), because those functions are being replaced with spies anyway.






share|improve this answer


























  • Thank you very much for this extra information. It's making a little more sense now. How would I test that search was actually being called?

    – Kaitlyn
    Nov 28 '18 at 20:33











  • Happy to help! The same way you were doing it originally (the last line in your original code example) should be fine. It was just that the ‘path’ spy was returning an object instead of the string you were expecting (first code sample in this answer). :)

    – Mike B.
    Nov 28 '18 at 22:59











  • I get an error saying: Expected a spy, but got a function

    – Kaitlyn
    Nov 30 '18 at 17:55













  • Can you post the updated test code?

    – Mike B.
    Nov 30 '18 at 18:56











  • spyOn(ctrl.$location, 'path').and.returnValue('fakepath'); ctrl.redirect(); expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');

    – Kaitlyn
    Nov 30 '18 at 20:41














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490837%2fcreating-unit-test-with-jasmine-and-getting-an-object-as-my-second-call-how-do%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The simple answer to get around the problem with this particular test is to change this line:



//spyOn(ctrl.$location, 'path').and.returnValue(mocks);
spyOn(ctrl.$location, 'path').and.returnValue('fakepath');


Edit: Looking at the code being tested, it looks like ctrl.$location.path returns different results depending on how it's called. If it's called with no parameters, then it appears to return a string. If it's called with a string as the parameter, then it appears to return the ctrl.$location object itself. Assuming I'm correct on that, there are two options...



The first option is to just set up your ctrl object for the test so that ctrl.$location.path doesn't need to be mocked (spied) at all, if possible.



The second option is to implement the dual-return logic in your spy using callFake:



spyOn(ctrl.$location, 'path').and.callFake(function(arg) {
if (typeof arg == "string") {
return ctrl.$location;
} else {
return "fakepath";
}
});


End Edit



But I think you're missing some details on how the spyOn functions work, so I will elaborate a little!



When you spy on a function, you're basically replacing that function with a spy - so if you subsequently call that function, it does nothing by default.
But you can add on to your spy too - for instance, you can have it return a specific value, or you can have it go ahead and call through to the original function that it is standing in for. That's what's happening when you use spyOn(...).and.returnValue() and spyOn(...).and.callThrough().



For example, let's say I have this simple object, foo. It has a function called sayHello, which calls another function called send, which sends a message over the network to Bob. If the message is sent successfully, the messagesToBob counter goes up by one, otherwise it remains the same. (send returns true if it worked, or false if it didn't).



// Begin contrived example!
var foo = {
messagesToBob: 0,
sayHello: function() {
if (this.send()) {
this.messagesToBob += 1;
}
},
send: function() {
network.send("Bob", "Hello");
}
};


Now I want to test this, but obviously I don't want to be sending Bob a bunch of messages during my tests. So that's where the spy comes in.



spyOn(foo, "send");


This essentially takes the foo.send function and replaces it with an empty function (I believe it will just returned undefined).



If I want to test the foo increments the counter on a successful run, I can make the "send" spy return true:



spyOn(foo, "send").and.returnValue(true);
foo.sayHello();

expect(foo.messagesToBob).to.equal(1);


If I want to test that it does not increment on a failed send, I can make the "send" spy return false.



In any of those cases, I could also check the status of the spy function itself using the expect(...).toHaveBeenCalled() or .toHaveNotBeenCalled() (or whatever functions are available to your particular testing framework/environment).



Anyway, I hope that helps! Your "mock" object's functions don't need to return a value in this example (assuming you need that object at all), because those functions are being replaced with spies anyway.






share|improve this answer


























  • Thank you very much for this extra information. It's making a little more sense now. How would I test that search was actually being called?

    – Kaitlyn
    Nov 28 '18 at 20:33











  • Happy to help! The same way you were doing it originally (the last line in your original code example) should be fine. It was just that the ‘path’ spy was returning an object instead of the string you were expecting (first code sample in this answer). :)

    – Mike B.
    Nov 28 '18 at 22:59











  • I get an error saying: Expected a spy, but got a function

    – Kaitlyn
    Nov 30 '18 at 17:55













  • Can you post the updated test code?

    – Mike B.
    Nov 30 '18 at 18:56











  • spyOn(ctrl.$location, 'path').and.returnValue('fakepath'); ctrl.redirect(); expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');

    – Kaitlyn
    Nov 30 '18 at 20:41


















1














The simple answer to get around the problem with this particular test is to change this line:



//spyOn(ctrl.$location, 'path').and.returnValue(mocks);
spyOn(ctrl.$location, 'path').and.returnValue('fakepath');


Edit: Looking at the code being tested, it looks like ctrl.$location.path returns different results depending on how it's called. If it's called with no parameters, then it appears to return a string. If it's called with a string as the parameter, then it appears to return the ctrl.$location object itself. Assuming I'm correct on that, there are two options...



The first option is to just set up your ctrl object for the test so that ctrl.$location.path doesn't need to be mocked (spied) at all, if possible.



The second option is to implement the dual-return logic in your spy using callFake:



spyOn(ctrl.$location, 'path').and.callFake(function(arg) {
if (typeof arg == "string") {
return ctrl.$location;
} else {
return "fakepath";
}
});


End Edit



But I think you're missing some details on how the spyOn functions work, so I will elaborate a little!



When you spy on a function, you're basically replacing that function with a spy - so if you subsequently call that function, it does nothing by default.
But you can add on to your spy too - for instance, you can have it return a specific value, or you can have it go ahead and call through to the original function that it is standing in for. That's what's happening when you use spyOn(...).and.returnValue() and spyOn(...).and.callThrough().



For example, let's say I have this simple object, foo. It has a function called sayHello, which calls another function called send, which sends a message over the network to Bob. If the message is sent successfully, the messagesToBob counter goes up by one, otherwise it remains the same. (send returns true if it worked, or false if it didn't).



// Begin contrived example!
var foo = {
messagesToBob: 0,
sayHello: function() {
if (this.send()) {
this.messagesToBob += 1;
}
},
send: function() {
network.send("Bob", "Hello");
}
};


Now I want to test this, but obviously I don't want to be sending Bob a bunch of messages during my tests. So that's where the spy comes in.



spyOn(foo, "send");


This essentially takes the foo.send function and replaces it with an empty function (I believe it will just returned undefined).



If I want to test the foo increments the counter on a successful run, I can make the "send" spy return true:



spyOn(foo, "send").and.returnValue(true);
foo.sayHello();

expect(foo.messagesToBob).to.equal(1);


If I want to test that it does not increment on a failed send, I can make the "send" spy return false.



In any of those cases, I could also check the status of the spy function itself using the expect(...).toHaveBeenCalled() or .toHaveNotBeenCalled() (or whatever functions are available to your particular testing framework/environment).



Anyway, I hope that helps! Your "mock" object's functions don't need to return a value in this example (assuming you need that object at all), because those functions are being replaced with spies anyway.






share|improve this answer


























  • Thank you very much for this extra information. It's making a little more sense now. How would I test that search was actually being called?

    – Kaitlyn
    Nov 28 '18 at 20:33











  • Happy to help! The same way you were doing it originally (the last line in your original code example) should be fine. It was just that the ‘path’ spy was returning an object instead of the string you were expecting (first code sample in this answer). :)

    – Mike B.
    Nov 28 '18 at 22:59











  • I get an error saying: Expected a spy, but got a function

    – Kaitlyn
    Nov 30 '18 at 17:55













  • Can you post the updated test code?

    – Mike B.
    Nov 30 '18 at 18:56











  • spyOn(ctrl.$location, 'path').and.returnValue('fakepath'); ctrl.redirect(); expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');

    – Kaitlyn
    Nov 30 '18 at 20:41
















1












1








1







The simple answer to get around the problem with this particular test is to change this line:



//spyOn(ctrl.$location, 'path').and.returnValue(mocks);
spyOn(ctrl.$location, 'path').and.returnValue('fakepath');


Edit: Looking at the code being tested, it looks like ctrl.$location.path returns different results depending on how it's called. If it's called with no parameters, then it appears to return a string. If it's called with a string as the parameter, then it appears to return the ctrl.$location object itself. Assuming I'm correct on that, there are two options...



The first option is to just set up your ctrl object for the test so that ctrl.$location.path doesn't need to be mocked (spied) at all, if possible.



The second option is to implement the dual-return logic in your spy using callFake:



spyOn(ctrl.$location, 'path').and.callFake(function(arg) {
if (typeof arg == "string") {
return ctrl.$location;
} else {
return "fakepath";
}
});


End Edit



But I think you're missing some details on how the spyOn functions work, so I will elaborate a little!



When you spy on a function, you're basically replacing that function with a spy - so if you subsequently call that function, it does nothing by default.
But you can add on to your spy too - for instance, you can have it return a specific value, or you can have it go ahead and call through to the original function that it is standing in for. That's what's happening when you use spyOn(...).and.returnValue() and spyOn(...).and.callThrough().



For example, let's say I have this simple object, foo. It has a function called sayHello, which calls another function called send, which sends a message over the network to Bob. If the message is sent successfully, the messagesToBob counter goes up by one, otherwise it remains the same. (send returns true if it worked, or false if it didn't).



// Begin contrived example!
var foo = {
messagesToBob: 0,
sayHello: function() {
if (this.send()) {
this.messagesToBob += 1;
}
},
send: function() {
network.send("Bob", "Hello");
}
};


Now I want to test this, but obviously I don't want to be sending Bob a bunch of messages during my tests. So that's where the spy comes in.



spyOn(foo, "send");


This essentially takes the foo.send function and replaces it with an empty function (I believe it will just returned undefined).



If I want to test the foo increments the counter on a successful run, I can make the "send" spy return true:



spyOn(foo, "send").and.returnValue(true);
foo.sayHello();

expect(foo.messagesToBob).to.equal(1);


If I want to test that it does not increment on a failed send, I can make the "send" spy return false.



In any of those cases, I could also check the status of the spy function itself using the expect(...).toHaveBeenCalled() or .toHaveNotBeenCalled() (or whatever functions are available to your particular testing framework/environment).



Anyway, I hope that helps! Your "mock" object's functions don't need to return a value in this example (assuming you need that object at all), because those functions are being replaced with spies anyway.






share|improve this answer















The simple answer to get around the problem with this particular test is to change this line:



//spyOn(ctrl.$location, 'path').and.returnValue(mocks);
spyOn(ctrl.$location, 'path').and.returnValue('fakepath');


Edit: Looking at the code being tested, it looks like ctrl.$location.path returns different results depending on how it's called. If it's called with no parameters, then it appears to return a string. If it's called with a string as the parameter, then it appears to return the ctrl.$location object itself. Assuming I'm correct on that, there are two options...



The first option is to just set up your ctrl object for the test so that ctrl.$location.path doesn't need to be mocked (spied) at all, if possible.



The second option is to implement the dual-return logic in your spy using callFake:



spyOn(ctrl.$location, 'path').and.callFake(function(arg) {
if (typeof arg == "string") {
return ctrl.$location;
} else {
return "fakepath";
}
});


End Edit



But I think you're missing some details on how the spyOn functions work, so I will elaborate a little!



When you spy on a function, you're basically replacing that function with a spy - so if you subsequently call that function, it does nothing by default.
But you can add on to your spy too - for instance, you can have it return a specific value, or you can have it go ahead and call through to the original function that it is standing in for. That's what's happening when you use spyOn(...).and.returnValue() and spyOn(...).and.callThrough().



For example, let's say I have this simple object, foo. It has a function called sayHello, which calls another function called send, which sends a message over the network to Bob. If the message is sent successfully, the messagesToBob counter goes up by one, otherwise it remains the same. (send returns true if it worked, or false if it didn't).



// Begin contrived example!
var foo = {
messagesToBob: 0,
sayHello: function() {
if (this.send()) {
this.messagesToBob += 1;
}
},
send: function() {
network.send("Bob", "Hello");
}
};


Now I want to test this, but obviously I don't want to be sending Bob a bunch of messages during my tests. So that's where the spy comes in.



spyOn(foo, "send");


This essentially takes the foo.send function and replaces it with an empty function (I believe it will just returned undefined).



If I want to test the foo increments the counter on a successful run, I can make the "send" spy return true:



spyOn(foo, "send").and.returnValue(true);
foo.sayHello();

expect(foo.messagesToBob).to.equal(1);


If I want to test that it does not increment on a failed send, I can make the "send" spy return false.



In any of those cases, I could also check the status of the spy function itself using the expect(...).toHaveBeenCalled() or .toHaveNotBeenCalled() (or whatever functions are available to your particular testing framework/environment).



Anyway, I hope that helps! Your "mock" object's functions don't need to return a value in this example (assuming you need that object at all), because those functions are being replaced with spies anyway.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 3 '18 at 16:11

























answered Nov 27 '18 at 22:13









Mike B.Mike B.

1,06288




1,06288













  • Thank you very much for this extra information. It's making a little more sense now. How would I test that search was actually being called?

    – Kaitlyn
    Nov 28 '18 at 20:33











  • Happy to help! The same way you were doing it originally (the last line in your original code example) should be fine. It was just that the ‘path’ spy was returning an object instead of the string you were expecting (first code sample in this answer). :)

    – Mike B.
    Nov 28 '18 at 22:59











  • I get an error saying: Expected a spy, but got a function

    – Kaitlyn
    Nov 30 '18 at 17:55













  • Can you post the updated test code?

    – Mike B.
    Nov 30 '18 at 18:56











  • spyOn(ctrl.$location, 'path').and.returnValue('fakepath'); ctrl.redirect(); expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');

    – Kaitlyn
    Nov 30 '18 at 20:41





















  • Thank you very much for this extra information. It's making a little more sense now. How would I test that search was actually being called?

    – Kaitlyn
    Nov 28 '18 at 20:33











  • Happy to help! The same way you were doing it originally (the last line in your original code example) should be fine. It was just that the ‘path’ spy was returning an object instead of the string you were expecting (first code sample in this answer). :)

    – Mike B.
    Nov 28 '18 at 22:59











  • I get an error saying: Expected a spy, but got a function

    – Kaitlyn
    Nov 30 '18 at 17:55













  • Can you post the updated test code?

    – Mike B.
    Nov 30 '18 at 18:56











  • spyOn(ctrl.$location, 'path').and.returnValue('fakepath'); ctrl.redirect(); expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');

    – Kaitlyn
    Nov 30 '18 at 20:41



















Thank you very much for this extra information. It's making a little more sense now. How would I test that search was actually being called?

– Kaitlyn
Nov 28 '18 at 20:33





Thank you very much for this extra information. It's making a little more sense now. How would I test that search was actually being called?

– Kaitlyn
Nov 28 '18 at 20:33













Happy to help! The same way you were doing it originally (the last line in your original code example) should be fine. It was just that the ‘path’ spy was returning an object instead of the string you were expecting (first code sample in this answer). :)

– Mike B.
Nov 28 '18 at 22:59





Happy to help! The same way you were doing it originally (the last line in your original code example) should be fine. It was just that the ‘path’ spy was returning an object instead of the string you were expecting (first code sample in this answer). :)

– Mike B.
Nov 28 '18 at 22:59













I get an error saying: Expected a spy, but got a function

– Kaitlyn
Nov 30 '18 at 17:55







I get an error saying: Expected a spy, but got a function

– Kaitlyn
Nov 30 '18 at 17:55















Can you post the updated test code?

– Mike B.
Nov 30 '18 at 18:56





Can you post the updated test code?

– Mike B.
Nov 30 '18 at 18:56













spyOn(ctrl.$location, 'path').and.returnValue('fakepath'); ctrl.redirect(); expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');

– Kaitlyn
Nov 30 '18 at 20:41







spyOn(ctrl.$location, 'path').and.returnValue('fakepath'); ctrl.redirect(); expect(ctrl.$location.search).toHaveBeenCalledWith('redirect', 'fakepath');

– Kaitlyn
Nov 30 '18 at 20:41






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490837%2fcreating-unit-test-with-jasmine-and-getting-an-object-as-my-second-call-how-do%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen