jasmine

How do I stub `$window.localStorage` in my AngularJS unit tests?

北城余情 提交于 2019-12-10 11:39:44
问题 angular.module('MyApp') .controller('loginCtrl', function($scope, $rootScope, $location, $window, $auth) { $scope.login = function() { $auth.login($scope.user) .then(function(response) { if(response.data.users.status === 'success'){ $rootScope.currentUser = response.data.username; $window.localStorage.user = JSON.stringify(response.data.users); $location.path('/'); } }).catch(function(response){ if(response.data.users.status === 'error'){ $scope.error = response.data.users.error; } }) }; });

How to avoid long relative paths (../../../) with jasmine in different environments?

五迷三道 提交于 2019-12-10 11:30:07
问题 I am currently developing a project, which I want to test in different environments - including node.js and different browsers with karma/selenium - to avoid compatibility issues. (I think I will use browserify in browsers, but I am not familiar with it yet.) I have a nested testing directory, something like this: repo/ - project.js - project.my.module.js - spec/ -- helpers/ --- a.jasmine.helper.js -- support/ --- jasmine.json -- project.my.module/ --- ModuleClass.spec.js -- project.MyClass

angular-ui/bootstrap: Testing a controller that uses a dialog

筅森魡賤 提交于 2019-12-10 11:23:46
问题 I've a controller that uses a Dialog from angular-ui/bootstrap: function ClientFeatureController($dialog, $scope, ClientFeature, Country, FeatureService) { //Get list of client features for selected client (that is set in ClientController) $scope.clientFeatures = ClientFeature.query({clientId: $scope.selected.client.id}, function () { console.log('getting clientfeatures for clientid: ' + $scope.selected.client.id); console.log($scope.clientFeatures); }); //Selected ClientFeature $scope

Preventing AJAX call with Jasmine and Sinon using Backbone

北城余情 提交于 2019-12-10 11:17:08
问题 I'm just getting started testing my Backbone app with Sinon and Jasmine. I have a view that look something like (coffeescript): initialize: -> @collection.on 'reset', @render, this render: -> if @collection.fetched # do stuff else @$el.append "<h3>Loading...</h3>" @collection.fetch() this I want to test this with an unfetched collection, but I'm not sure how to fake an ajax call within my code (obviously can easily be done in the spec). I realize that I could just pass in a pre-fetched

not.custom_matcher error - Jasmine

旧时模样 提交于 2019-12-10 11:16:40
问题 I have created a custom matcher in jasmine to verify if an element has a class or not. This is the code that I have placed in the beforeEach() function to define the custom matcher: beforeEach(function() { jasmine.addMatchers({ toHaveClass: function() { return { compare: function(actual, expected) { return { pass: actual.getAttribute('class').then(function(classes) { return classes.split(' ').indexOf(expected) !== -1; }) } } } } }); this.driver = new webdriver.Builder().forBrowser('firefox')

Protractor - Jasmine - Download file to a relative path

跟風遠走 提交于 2019-12-10 11:10:18
问题 I've read and follow the below questions here and its comments/answers: Question 1 Question 2 Question 3 But could not find a way not to use absolutePath , since I need to have this tests run on another machine, so, I need to be relative to the project no the other folder outside of it. how can I accomplish that? 回答1: As explained in the questions you have quoted, You can control where the file downloads using the below option 'chromeOptions': { prefs: { download: { 'prompt_for_download':

How to mock socket.io with Angular and Jasmine

北城以北 提交于 2019-12-10 10:50:31
问题 I'm having trouble figuring out how to correctly mock Socket.io in an Angular application using Jasmine and Karma. Here are the files found in karma.conf.js : 'bower_components/angular/angular.js', 'bower_components/angular-mocks/angular-mocks.js', 'bower_components/angular-ui-router/release/angular-ui-router.js', 'bower_components/angular-socket-io/socket.js', 'bower_components/socket.io-client/socket.io.js', 'bower_components/angular-socket-io/mock/socket-io.js', 'public/javascripts/*.js',

Unit test 'success' and 'error' observable response in component for Angular 2

馋奶兔 提交于 2019-12-10 10:46:35
问题 I'm writing a unit test for a component that makes a call to a service OnInit. If the response is a 'success' one action taken and another for an 'error'. What is the best way to test both cases? I've created a simplified version of the component and unit test. Something that I could easily test against in both cases. I've attempted to implement the solution here but I must be off on the implementation. I've also attempted to throw an error as you will see in the spec and comments. Component

TypeError: Cannot read property 'then' of undefined in testing AngularJS controller with Karma

一个人想着一个人 提交于 2019-12-10 10:45:29
问题 I am trying to write unit tests for a controller that gets a promise from a service, but in Jasmine I am getting: TypeError: Cannot read property 'then' of undefined In controller I call .then() on returned promise from service. Can anybody help please? I am surely missing something obvious. Here is a plunk: http://plnkr.co/edit/vJOopys7pWTrTQ2vLgXS?p=preview 'use strict'; describe('Language controller', function() { var scope, translationService, createController; beforeEach(function() { var

Getting “Unexpected request” error when running Karma unit test in an AngularJS app

人走茶凉 提交于 2019-12-10 10:35:19
问题 I'm trying to write a unit test for a controller which fetches article details using $http service. Controller: .controller('ArticleDetailCtrl',function($scope, Article, $routeParams, API_URL, ARTICLE_URL, $http, $sce){ $scope.url = API_URL + ARTICLE_URL + '/' + $routeParams.articleId; $http.get($scope.url).then(function(response) { //console.log(response.data); $scope.heading = response.data.Headline; $scope.rawBody = response.data.Body; $scope.body = $sce.trustAsHtml($scope.rawBody); $scope