jeudi 1 octobre 2015

Jasmine unit test with angular mock

I have created a mock service that would act just like my real service but return a defined set of values for my unit test. If I add that module to my apps. it intercepts the calls and returns the things I have in the mock just like its suppose to.

but when I add this module to my jasmine test I cant seem to get a value back

beforeEach(module('VideoConference'));//my app
beforeEach(module('lookupServiceMock')); //the mock service
beforeEach(inject(function(Mlb) { // Mlb is a factory that submits requests
    mlb = Mlb;
    spyOn(mlb, 'submitRequest').and.callThrough();
}));
...
it("Should Create a new conference", function () {
    childScope = $scope.$new();
    vm = $controller('TitleAndDateCtrl', { $scope: childScope });             

    childScope.requiredFieldsMet = function () { return true }
    vm.conference.Title = "Test";              
    vm.createConference();
    expect(mlb.submitRequest).toHaveBeenCalled();//this expectation comes back true
});
it("Should have returned a value", function() {
    var id = mlb.get("id");//at this point expect that the id has been set
    expect(id).toBe(123);
});

When I step through, the controller does go to where the $http.post(...) is made but it does not hit my mock or return a value. it does not go into the success of the post. any idea how to make it hit my mock, return the value

Aucun commentaire:

Enregistrer un commentaire