vendredi 3 juillet 2015

How to reset service between tests in AngularJS

I have a service that is injected into beforeEach:

beforeEach(inject(function($rootScope, $state, $injector, $controller, MyService) {
    var state = $state;
    scope = $rootScope.$new();
    myService = MyService;
    ctrl = $controller('MainCtrl', {$state: state, MyService: myService});
}));

And I have some tests described with "it":

it('test1', function() { 
   var data = ["test"]; 
   ctrl.extract_data(data); 
   expect(myService.get_data()).toEquals("test");
});

Now my problem is that I have a shared variable in service (variable data) between the tests and if I run the test with other data (e.g. var data = ["a", "b"]), the variable that is shared in myService will contain a, b and test (that was retrieved from the previous test). Is there a way to "reset" the service content everytime a test is ran?

Aucun commentaire:

Enregistrer un commentaire