How do I mock $window injected manually in provider private function?

Issue I have the following provider: angular.module(‘MyApp’).provider(‘MyDevice’, function () { var ngInjector = angular.injector([‘ng’]), $window = ngInjector.get(‘$window’); function isMobileDevice () { return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/) .test($window.navigator.userAgent || $window.navigator.vendor || $window.opera); } this.$get = function () { return { isDesktop: function ()

Continue reading

How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

Issue In the below SampleController, how do I unit test that postAttributes function calls sampleService.updateMethod. I’m having trouble since the updateMethod returns promise. angular.module(‘sampleModule’) .controller(‘SampleController’, SampleController); SampleController.$inject =[‘sampleService’]; function SampleController(sampleService){ this.postAttributes = function() { sampleService.updateMethod(number,attributes) .then(function(response){ //do something on successful

Continue reading

How to test $scope in Jasmine test?

Issue I trying to write unit tests for Angularjs with Jasmine. Here is my controller: function HomeController($scope, fav, news, materials) { console.log(‘home controller’); $scope.testMe = true; } module.controller(‘HomeController’, HomeController); And tests describe(‘Home controller tests’, function() { var $rootScope, $scope, controller;

Continue reading

Unit test controller

Issue I make an ionic app and it finish but when i start to add tests to it I face a problem with $resources ,in this case I have this Controller : .controller(‘newAccountCtrl’, function($scope, $window, $rootScope, API, $ionicPopup, $state) {

Continue reading

Unit test $mdDialog angular material

Issue I called one $mdDialog inside a function. I want to unit-test $mdDialog ok and cancel cases. The below is my controller code (app.controller.js). (function () { ‘use strict’; app.controller(‘AppCtrl’, AppCtrl); AppCtrl.$inject = [‘$scope’, ‘$mdDialog’]; function AppCtrl($scope, $mdDialog) { $scope.saveEntry

Continue reading

I have a problem with testing adding user with password encoder

Issue I have a problem when testing a method with using passwordencoder: Cannot invoke "org.springframework.security.crypto.password.PasswordEncoder.encode(java.lang.CharSequence)" because the return value of "com.store.restAPI.user.UserConfig.passwordEncoder()" is null` Thats my test class method: @ExtendWith(MockitoExtension.class) class UserServiceTest { private UserService underTest; @Mock private UserRepository userRepository; @Mock

Continue reading

Re-direct requests to SideEffect Utility Classes

Issue for a spring boot application that needs to be tested below is my query. @CustomLog @RestController @RequestMapping("/my_path") public class MyController { @GetMapping(path = "**", produces = {MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<JsonNode> fetchData(HttpServletRequest request){ … some code…..which also calls external apis….. }

Continue reading

Why do Angular unit tests load $route?

Issue After updating to Angular 1.5.3 (from 1.4.9) all my unit tests have started failing, where they worked before. The error is as follows: Error: [$injector:unpr] Unknown provider: AuthenticationHttpInterceptorProvider <- AuthenticationHttpInterceptor <- $http <- $templateRequest <- $route It is expected

Continue reading

can i set a base url for expect()

Issue My restangular call has a baseUrl set in a config file to http://localhost:3000/. So a call like Restangular.all(“awards”).customPOST(award) Calls at baseUrl+”awards” Now when I write a test for this, i have to write: httpBackend.expectPOST(“http://localhost:3000/awards”) But later if this baseUrl

Continue reading

Angular httpBackend crashing browser

Issue When using Jasmine and Angular (1.4.7) with Restangular (1.4.0), httpBackend and angular-mocks (1.4.7), Chrome and PhantomJS both crash when encountering the following line: httpBackend.whenGET(‘/something’).respond(200); If I get rid of it entirely, as below, I get the following error: //httpBackend.whenGET(‘/something’).respond(200);

Continue reading

Mock Activated Route With Params in Angular

Issue Question How to mock ActivatedRoutes in my unit tests? Service: constructor( private route: ActivatedRoute, private knowledgeModelService: KnowledgeModelService ) { route.params.pipe(map(p => p.modelId)).subscribe((modelId) => { this.modelId = modelId; }); } My unit test: With mock class class ActivatedRouteMock { //

Continue reading

Failed: Errors in unit testing angular

Issue I set up testing environment for a component that I would like to test, but unfortunately getting some errors which I can not get my head around.. describe(‘HelloWorldComponent’, () => { let component: HelloWorldComponent; let fixture: ComponentFixture<HelloWorldComponent>; let cookieValue:

Continue reading

AWS MediaConvert job settings unit tests

Issue I am using CreateJobCommandInput, CreateJobCommand, MediaConvertClient from @aws-sdk/client-mediaconvert to submit MediaConvert job. CreateJobCommandInput contains a lot of different parameters. I would like to check if those parameters are valid using some sort of unit tests. Is this possible? Solution

Continue reading

Chai eventually erroneously passing

Issue Why does the test expressed with eventually pass? I’m obviously missing something in the syntax or use case. const chai = require(‘chai’) const chaiAsPromised = require("chai-as-promised"); chai.use(chaiAsPromised); const {expect} = chai function p() { return new Promise( (resolve) =>

Continue reading

Angular 13 How to Unit Test Interceptor catchError method

Issue i’m working on an existing Angular Project and i have the following intercepter with Error Handling. this is the code : public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const sendAccessToken = this.moduleConfig.resourceServer.sendAccessToken; if (sendAccessToken) { const token = this.authStorage.getItem(‘access_token’);

Continue reading