How can I write unit tests for a function that has a $state.go () and which is expected to redirect to that particular sate?
$scope.inviteMembers = (id)=> {
$state.go('invite', {deptId: id});
}
Unit tests of controller logic. Unit tests involve testing a part of an app in isolation from its infrastructure and dependencies. When unit testing controller logic, only the contents of a single action are tested, not the behavior of its dependencies or of the framework itself.
Writing a Unit Test for REST Controller First, we need to create Abstract class file used to create web application context by using MockMvc and define the mapToJson() and mapFromJson() methods to convert the Java object into JSON string and convert the JSON string into Java object.
Unit tests can be performed manually or automated. Those employing a manual method may have an instinctual document made detailing each step in the process; however, automated testing is the more common method to unit tests. Automated approaches commonly use a testing framework to develop test cases.
test/run.py is the master script that RUNS the unit tests.
Since it is a unit test, you just need to make sure that the function is being called, nothing more.
Something along this lines:
it('should move to the invite page after inviting someone', function() {
spyOn($state, 'go');
$scope.inviteMembers(1);
expect($state.go).toHaveBeenCalledWith('invite', {deptId: 1});
});
So the main idea here is to spy on the go
method of $state
and when we call the method, we just need to verify that the $state.go
method have been called and with what parameters.
Before you ask me back... Yes but I want to be sure that the state change as well. Well you don't need to do that.
This is a unit test, that means that your only concern is to test this controller and nothing else, and its responsibility ends which just calling that method. What happens after that is the responsibility of someone else, in this case ui-router and I bet that ui-router is well tested :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With