Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing controller which has a $state.go method in controller

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});
                }
like image 519
forgottofly Avatar asked Dec 31 '14 09:12

forgottofly


People also ask

What is unit testing in controller?

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.

How do you write a unit test for a controller?

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.

Which of the following is the most commonly used approach to unit testing?

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.

Which command is used for run unit test?

test/run.py is the master script that RUNS the unit tests.


1 Answers

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 :)

like image 107
Jesus Rodriguez Avatar answered Oct 04 '22 04:10

Jesus Rodriguez