Error: Unexpected request: GET /api/users/me meanstack jasmine testing - angularjs

Please help my http request keeps failing and i have tried different methods from Internet
test.js
{
describe('EcdreportController', function(){
beforeEach(function() {
module('mean');
module('mean.ecdreport');
});
// Initialize the controller and a mock scope
var EcdreportController,
$scope,
$httpBackend,
$stateParams,
$location;
beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_,$http) {
$scope = $rootScope.$new();
EcdreportController = $controller('EcdreportController', {
$scope: $scope,
$http : $http
});
$stateParams = _$stateParams_;
$httpBackend = _$httpBackend_;
$location = _$location_;
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('$scope.getdatamodel',function($http){
// test expected GET request
$httpBackend.expectGET('/api/v1/getdatamodel').respond(200);
//expect($httpBackend.flush).not.toThrow();
$httpBackend.flush();
$scope.getDataModel()
expect($scope.datamodel).toEqual(data)
});
})
}
controllers.js
$scope.getDataModel = function() {
$http({url:'/api/v1/getdatamodel', method:"GET"})
.success(function(data) {
console.log('Datamodel successful');
$scope.datamodel = data[0];
console.log('datamodel', data);
})
.error(function(error) {
$scope.datamodel =[];
});
}
// console.log("Trying to get datamodel");
$scope.getDataModel();`
terminal results
PhantomJS 2.1.1 (Linux 0.0.0) EcdreportController $scope.getdatamodel FAILED
Error: Unexpected request: GET /api/users/me
Expected GET /api/v1/getdatamodel in bower_components/angular-mocks/angular-mocks.js (line 1412)
$httpBackend#bower_components/angular-mocks/angular-mocks.js:1412:90
n#bower_components/angular/angular.min.js:98:461
bower_components/angular/angular.min.js:95:490
bower_components/angular/angular.min.js:130:511
$eval#bower_components/angular/angular.min.js:145:107
$digest#bower_components/angular/angular.min.js:142:178
flush#bower_components/angular-mocks/angular-mocks.js:1791:45
packages/custom/ecdreport/public/tests/ecdreport.tests.js:71:21
Error: [$rootScope:inprog] http://errors.angularjs.org/1.5.7/$rootScope/inprog?p0=%24digest in bower_components/angular/angular.min.js (line 137)
n#bower_components/angular/angular.min.js:137:402
$digest#bower_components/angular/angular.min.js:142:31
verifyNoOutstandingExpectation#bower_components/angular-mocks/angular-mocks.js:1823:45
packages/custom/ecdreport/public/tests/ecdreport.tests.js:63:46
PhantomJS 2.1.1 (Linux 0.0.0): Executed 3 of 3 (1 FAILED) (0.056 secs / 0.074 secs)

Related

Error in Karma/Jasmine Unit test

This is my AngularJS code:
angular.module('myapp', [])
.controller('MainCtrl', function($scope, $http, $rootScope, $routeParams) {
$scope.name = "Girish";
$scope.sayHello = function() {
$scope.greeting = "Hello " + $scope.name;
};
$scope.commonUrl = "/";
$rootScope.$watch('eng', function() {
$scope.currentLang = $rootScope.CURRENT_LANGUAGE;
});
$scope.homePageFirstSlider = function() {
$scope.anun = "Nar";
$http({
method: 'GET',
url: "/" + "eng" + '/api/getslideritems/main'
}).then(function successCallback(response) {
$scope.Data = response.data;
$scope.loadCss('mainCarousel');
},
function errorCallback(response) {});
};
});
This is a test file:
'use strict';
describe('myapp', function() {
beforeEach(module('myapp'));
var $controller;
beforeEach(inject(function(_$controller_) {
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
}));
describe('$scope.sayHello', function() {
it('same tests', function() {
var $scope = {};
var controller = $controller('MainCtrl', { $scope: $scope });
$scope.name = 'Girish';
$scope.sayHello();
expect($scope.greeting).toEqual('Hello Girish');
});
});
});
After I run karma.conf.js file I have this error:
PhantomJS 2.1.1 (Linux 0.0.0) myapp $scope.sayHello same tests FAILED
***Error: [$injector:unpr] Unknown provider: $routeParamsProvider <- $routeParams <- MainCtrl***
http://errors.angularjs.org/1.5.0/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams%20%3C-%20MainCtrl in /var/www/html/famboxv2/public/bower_components/angular/angular.js (line 4397)
loaded#http://localhost:9882/context.js:151:17
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.025 secs / 0.004 secs)
Chromium 45.0.2454 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.008 secs / 0.005 secs)
How can I solve this error?
Unknown provider: $routeParamsProvider <- $routeParams <- MainCtrl
$routeParams service is a part of ngRoute module. So you'll have to include it as a dependency on your module.
Here's how:
angular.module('myapp', ['ngRoute'])
Also you should be mocking the controller in the first beforeEach block instead of mocking it inside a specific test, so that it could be reusable in all your tests.
And your tests will still be failing because you've inject an empty object for $scope instead of creating a new scope using the $rootScope's $new function. So you should be doing the following changes to make them pass:
describe('myapp', function() {
beforeEach(module('myapp'));
//Replace this
beforeEach(inject(function($controller, _$rootScope_){
//With this
// beforeEach(inject(function($controller, _$rootScope_, _$routeParams_, _$http_){
//Uncomment the following comments if needed for writing tests for the $watch function.
// $rootScope = _$rootScope_;
$scope = _$rootScope_.$new();
// $http = _$http_;
// $routeParams = _$routeParams_;
controller = $controller('MainCtrl', {
$scope: $scope,
// $http: $http,
// $rootScope: $rootScope,
// $routeParams: $routeParams
});
}));
describe('$scope.sayHello', function() {
it('same tests', function() {
$scope.name = 'Girish';
$scope.sayHello();
expect($scope.greeting).toEqual('Hello Girish');
});
});
});

How to correctly inject a factory using Jasmine / karma and test it?

This is my factory / base.js:
angular.module("BaseApp", [])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
}])
.config(['$locationProvider', function($locationProvider){
$locationProvider.html5Mode(true);
}])
.factory("BaseService", ["$http", "$window", function($http, $window) {
And this is my test_base.js:
describe('Factory: BaseService', function() {
var fctry, mockBackend;
beforeEach(function() {
module('BaseApp');
inject(function($factory, $httpBackend) {
mockBackend = $httpBackend;
fctry = $factory('BaseService', {});
});
});
it('logout() should POST to /logout', function() {
// I want to test if the logout(cb) function posts to /logout and, on success, redirect to '/'
// but on failure, calls accessErrors and the cb.
});
});
This is my karma config file:
files: [
'../angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'../base.js',
'tests/test_base.js',
],
When I do karma start and run the tests, I get this error / fail:
Chromium 48.0.2564 (Ubuntu 0.0.0) Factory: BaseService logout() should POST to /logout FAILED
Error: [$injector:unpr] Unknown provider: $factoryProvider <- $factory
http://errors.angularjs.org/1.3.15/$injector/unpr?p0=%24factoryProvider%20%3C-%20%24factory
...
Chromium 48.0.2564 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.053 secChromium 48.0.2564 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.114 secs / 0.053 secs)
How come it is failing already and how come it is saying that $factoryProvider is an unknown provider?
It thinks you're trying inject factory, which is a function, not a provider, so that won't work.
You're also missing out on the underscore trick
Try changing this:
var fctry, mockBackend;
beforeEach(function() {
module('BaseApp');
inject(function($factory, $httpBackend) {
mockBackend = $httpBackend;
fctry = $factory('BaseService', {});
});
});
To this:
var BaseService, $httpBackend;
beforeEach(function() {
module('BaseApp');
inject(function(_BaseService_, _$httpBackend_) {
$httpBackend = _$httpBackend_;
BaseService = _BaseService_;
});
});

Jasmine Testing with vm in Angular controller

Trying to test a simple test in Jasmine & Karma and AngularJs frameWork,
controller:
(function() {
'use strict';
angular
.module('bsp.account')
.controller('Account', Account);
/* #ngInject */
function Account(userService, accountService) {
var vm = this;
vm.title = 'Accounts';
vm.username = userService.getUsername();
vm.showPasswordModal = accountService.showPasswordModal;
vm.showLogoutModal = accountService.showLogoutModal;
activate();
////////////////
function activate() {
}
}
})();
Can anyOne tell me how do i write a simple test case using Jasmin with this code ? and also maybe a possible methode and its test,
My test code below:
describe('Account', function() {
var scope,
controller;
beforeEach(module('bsp.account'));
//sample testCase--
it('testCheck', function() {
expect('helloWorld').toBe("helloWorld");
});
describe('Account', function() {
//injecting the scope&controller--
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('Account', {
$scope: scope
});
scope.vm = controller;
}));
});
it("checkTitle",function(){
var vm = controller("Account",{$scope:scope});
expect(vm.title).toEqual("Accounts");
});
});
karma ,response:
Running "karma:unit:run" (karma) task
[2015-10-23 15:11:03.542] [DEBUG] config - Loading config /vagrant/frontend/build/karma-unit.js
✗ checkTitle
TypeError: 'undefined' is not a function (evaluating 'controller("Account",{$scope:scope})')
at /vagrant/frontend/src/app/account/account.controller.spec.js:33
LOG LOG: undefined
PhantomJS 1.9.8 (Linux 0.0.0) LOG: undefined
LOG LOG: '** if *********'
PhantomJS 1.9.8 (Linux 0.0.0) LOG: '** if *********'
PhantomJS 1.9.8 (Linux 0.0.0): Executed 37 of 37 (1 FAILED) (0.134 secs / 0.128 secs)
Warning: Task "karma:unit:run" failed. Use --force to continue.
Aborted due to warnings.
Completed in 10.849s at Fri Oct 23 2015 15:11:04 GMT-0700 (PDT) - Waiting...
Any suggestions is appreciated,Am new to Jasmin testing.Thank You
You are actually instantiating a BoardPerformance controller in your beforeEach block but the code you are showing is for Account, so I understand that's a mistake.
So for your simple test case to run try this:
describe('Account', function() {
var scope, controller, userServiceMock;
beforeEach(module('bsp'));
beforeEach(function() {
userServiceMock = {
getUsername: function(){}
}
});
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('Account', {
'userService': userServiceMock
});
}));
it('checkTitle', function(){
expect(controller.title).toEqual('Accounts');
});
});
So this is the new test case in Jasmin,
describe('Account', function() {
var scope, controller, userServiceMock,accountServiceMock;
beforeEach(module('bsp'));
beforeEach(function() {
userServiceMock = {
getUsername: function(){} //this is a method inside userService
};
accountServiceMock = {
showPasswordModal :function(){}//this is a method inside accountService
};
});
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('Account', {
'userService': userServiceMock,
'accountService':accountServiceMock
});
}));
describe('testing Title',function(){
it('checkTitle', function(){
expect(controller.title).toEqual('Accounts');
});
});
});
It is very basic but has necessary information for controllers with dependency and also the vm..thank to Pablo

AngularJs controller test: unexpected request : GET

I am trying to write my first test for controller by mocking the service layer using karma-jasmine and httpBackend. But I am getting this error.
Error: Unexpected request: GET./webresources/campaign/getCampaignDetails/undefined
Expected GET ./webresources/campaign/getCampaignDetails/CMP1000004568
Thanks in advance for any help :)
Service:
(function() {
'use strict';
angular.module('tbApp').factory('Campaign', CampaignFactory);
CampaignFactory.inject = ['$http'];
function CampaignFactory($http) {
//base url for all rest service calls
var baseUrl = WS_PATH + '/webresources/campaign';
var Campaign = {
getCampaignDetails: getCampaignDetails,
};
return Campaign;
function getCampaignDetails(campaignId) {
return $http.get(baseUrl + '/getCampaignDetails/' + campaignId).then(getCampaignDetailsComplete);
function getCampaignDetailsComplete(response) {
return response.data;
}
}
}
})();
Controller:
(function () {
'use strict';
angular.module('tbApp').controller('campaignEditCtrl', campaignEditCtrl);
campaignEditCtrl.inject = ['$routeParams', 'Campaign', 'User', '$location', 'Asset', '$window', '$scope', '$compile', 'ImageLibrary', 'Lists'];
function campaignEditCtrl($routeParams, Campaign, User, $location, Asset, $window, $scope, $compile, ImageLibrary, Lists) {
var vm = this;
vm.campaignData = [];
vm.getCampaignDetails = getCampaignDetails;
initialise();
/**
* Initialises the campaign properties page
*/
function initialise() {
// Registes a click listener to hide action menu
$window.addEventListener('click', function (event) {
if (event.which !== 3) {
$scope.$apply(function () {
vm.selectedActionItem = '';
});
}
});
fromPage=true;
fromPage1=true;
vm.user = User.info;
vm.campaignId = $routeParams.campId;
//set default value to prevent filter error
vm.campaignData.imageName = '///////';
getCampaignDetails();
getAllImageData();
}
/**
* Load the campaign details
*/
function getCampaignDetails() {
vm.loadingCampaignDetails = true;
Campaign.getCampaignDetails(vm.campaignId).then(getCampaignDetailsComplete);
function getCampaignDetailsComplete(data) {
vm.campaignData = data;
}
}
}})();
controllerSpec:
describe('campaignEditCtrl as vm', function() {
var scope, httpBackend, controller, vm, createController, campaignId;
var baseUrl = './webresources/campaign';
beforeEach(module("tbApp"));
beforeEach(inject(function($rootScope, $httpBackend, $controller) {
httpBackend = $httpBackend;
scope = $rootScope.$new();
controller = $controller;
campaignId= "CMP1000004568";
//service = Campaign;
createController = function() {
return $controller('campaignEditCtrl', {
'$scope': scope
});
};
httpBackend.when("GET", baseUrl+"/getCampaignDetails/"+campaignId).respond([{}, {}, {}]);
}));
/*afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});*/
it('should run the Test to get the campaign data from the backend', function() {
console.log("here"+campaignId);
vm=createController();
httpBackend.expectGET(baseUrl+"/getCampaignDetails/"+campaignId);
scope.$apply(function() {
scope.runTest();
});
//expect(vm.campaignData.imageName).toEqual('///////');
httpBackend.flush();
expect(vm.campaignData.length).toBe(3);
});
});
Output:
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Windows 7)]: Connected on socket jAWt7tfOMUkrmCjM0d_n with id 71764124
INFO [Chrome 41.0.2272 (Windows 7)]: Connected on socket Fu0mS3U91St4gSQ10d_o with id 78282639
WARN [web-server]: 404: /webresources/user/checkSession
PhantomJS 1.9.8 (Windows 7) LOG: 'WARNING: Tried to load angular more than once.'
LOG: 'hereCMP1000004568'
PhantomJS 1.9.8 (Windows 7) campaignEditCtrl as vm should run the Test to get the campaign data from the backend FAILED
Error: Unexpected request: GET ./webresources/campaign/getCampaignDetails/undefined
Expected GET ./webresources/campaign/getCampaignDetails/CMP1000004568
at $httpBackend (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/angular-mocks.js:1181)
at sendReq (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:8404)
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:8123
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:11659
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:11659
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:11745
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:12788
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:12600
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:12892
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/test/js/campaignEditSpec.js:42
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/node_modules/karma-jasmine/lib/boot.js:117
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:9876/karma.js:185
at http://localhost:9876/context.html:157
PhantomJS 1.9.8 (Windows 7): Executed 2 of 2 (1 FAILED) (0.008 secs / 0.024 secs)
LOG: 'WARNING: Tried to load angular more than once.'
Chrome 41.0.2272 (Windows 7) LOG: 'hereCMP1000004568'
Chrome 41.0.2272 (Windows 7) campaignEditCtrl as vm should run the Test to get the campaign data from the backend FAILED
Error: Unexpected request: GET ./webresources/campaign/getCampaignDetails/undefined
Expected GET ./webresources/campaign/getCampaignDetails/CMP1000004568
at $httpBackend (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/angular-mocks.js:1180:9)
at sendReq (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:8403:9)
at serverRequest (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:8123:16)
at wrappedCallback (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:11659:8
at wrappedCallback (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:11659:8
at D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:11745:26
at Scope.$eval (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:12788:28)
at Scope.$digest (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:12600:31)
at Scope.$apply (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/main/webapp/js/libraries/uncompressed/angular.js:12892:24)
at Object.<anonymous> (D:/Tb_test/Campaign_Builder_Maven/campaign-builder-webapp/src/test/js/campaignEditSpec.js:40:9)
PhantomJS 1.9.8 (Windows 7): Executed 2 of 2 (1 FAILED) (0.008 secs / 0.024 secs)
Chrome 41.0.2272 (Windows 7): Executed 2 of 2 (1 FAILED) (0.051 secs / 0.047 secs)
TOTAL: 2 FAILED, 2 SUCCESS
We can see in the karma output that the test is failing because the URL is wrong. Instead of a campaign id, you're getting undefined.
Chrome 41.0.2272 (Windows 7) campaignEditCtrl as vm should run the Test to get the campaign data from the backend FAILED
Error: Unexpected request: GET ./webresources/campaign/getCampaignDetails/undefined
Expected GET ./webresources/campaign/getCampaignDetails/CMP1000004568
It looks like you're trying to test your getCampaignDetails() function, which passes in vm.campaignId as an argument to your service call. However, in your test you don't set vm.campaignId to anything before calling your function.
In your test setup, I would try changing your code to this:
scope = $rootScope.$new();
controller = $controller;
campaignId= "CMP1000004568";
//service = Campaign;
createController = function() {
var controller = $controller('campaignEditCtrl', {
'$scope': scope
});
scope.vm.campaignId = campaignId;
return controller;
};
...and see if that fixes it.
What's going on here is that your getCampaignDetails() function's behavior is dependent on the state of your controller. Your controller has an initialize() function that brings your controller to a state where the getCampaignDetails() can succeed. In your test, however, you don't perform similar initialization.

AngularJS mocking promise TypeError: 'undefined' is not an object (evaluating 'queryDeferred.resolve')

I'm trying to put some tests on a controller using ressources/promises. I had followed the article Mocking $resource and promises in AngularJS unit tests.
However I can't get a simple case to work as the queryDeferred object is undefined.
Error
PhantomJS 1.9.8 (Linux) OrdersModule API.Orders.query should query the API.Orders FAILED
TypeError: 'undefined' is not an object (evaluating 'queryDeferred.resolve')
at /mnt/data/projects/tcs_economat/frontend/static_src/test/unit/orderSpec.js:52
PhantomJS 1.9.8 (Linux): Executed 25 of 25 (1 FAILED) (0.235 secs / 0.223 secs)
Controller
"use strict";
angular.module('OrdersModule', ['services'])
.controller('OrdersControllers', ['$scope', 'API', function ($scope, API) {
$scope.orders = [];
API.Order.query().$promise.then(function (data) {
console.log(data);
});
}]
);
Test
"use strict";
describe('OrdersModule', function () {
var $q,
$rootScope,
$scope,
mockOrdersApiService,
queryDeferred,
mockOrdersResponse = [
{status: 'DRAFT'},
{status: 'DRAFT'}
];
beforeEach(module('tcsStoreApp'));
beforeEach(inject(function (_$q_, _$rootScope_) {
$q = _$q_;
$rootScope = _$rootScope_;
}));
beforeEach(inject(function ($controller) {
$scope = $rootScope.$new();
mockOrdersApiService = {
query: function () {
queryDeferred = $q.defer();
return {$promise: queryDeferred.promise};
}
};
spyOn(mockOrdersApiService, 'query').andCallThrough();
$controller('OrdersControllers', {
'$scope': $scope,
'API.Orders': mockOrdersApiService
});
}));
describe('API.Orders.query', function () {
beforeEach(inject(function (_$httpBackend_, $rootScope, $controller) {
var $httpBackend = _$httpBackend_;
queryDeferred.resolve(mockOrdersResponse);
$rootScope.$apply();
$httpBackend.expectGET('/static/js/tcs.fr.json').respond({});
}));
it('should query the API.Orders', function () {
expect(mockOrdersApiService.query).toHaveBeenCalled();
});
});
});
Question
Is the mockOrdersApiService correctly declared?
Why is the queryDeferred.resolve undefined?

Resources