jasmine

How to test an Angular controller with a function that makes an AJAX call without mocking the AJAX call?

安稳与你 提交于 2019-12-05 19:33:21
I have a function in a controller that calls a service which in turn calls another service, which makes the AJAX call and returns the promise. To put it simply: MainController call = function(){ //Handle the promise here, update some configs and stuff someService.call().then(success, failure); } SomeService return { call: function(){ return SomeOtherService.call(); } } SomeOtherService return { call: function(){ return $http.get(someUrl); } } How do I test the main controller's call function without actually making the AJAX call? I will obviously be testing the services separately. Update: So

How to mock JQuery with Jasmine?

╄→гoц情女王★ 提交于 2019-12-05 18:16:20
How can I test that a certain JQuery selector has been executed with Jasmine? I'm trying to do the following: spyOn($.fn, 'init').andCallThrough(); // my code expect($.init).toHaveBeenCalled(); But after this call, $('div') returns Object { selector="div", context=document, NaN=div.spec, more...} , though it has to return (and $.fn.init('div') does return it): [div.jasmine_reporter, div.banner, div.logo, 4 more...] . This stuff naturally breaks the code since the JQuery object is no longer usable. Example: Say I want to test that a JQuery selector has been called, I write: it('tests', function

AngularJS Protractor - How Do You Test an AJAX Login Call?

江枫思渺然 提交于 2019-12-05 18:02:33
I have a button that once clicked sends out an AJAX call in Angular's $promise format. When the login is successful, a $scope variable is changed and an element that looks like: <section id="logged-in-section" ng-if="auth.user !== null"> // Section to display if logged in </section> is displayed. I am currently testing the above with the following: loginButton.click(); browser.sleep(2000); expect($("#logged-in-section").isDisplayed()).toBeTruthy(); browser.sleep(2000) idles the browser for two seconds before Protractor checks to see if logged-in-section has been displayed. If I take out

Globally mock object in angularjs for jasmine/karma testing

风流意气都作罢 提交于 2019-12-05 17:58:06
问题 I have an object that I am mocking up for unit testing. Essentially in my test file I mock it up as follows: var mockObject = { mockMethod1 : function() {return true}, mockMethod2 : function() {return true} }; beforeEach(module('myModule') , function ($provide) { $provide.value('realObject',mockObject); }); The way i understand it is that as I test functionality in my module etc... anywhere that references the "realObject" will use my "mockObject" My issue is that I have made multiple js

Unit testing watchers in angular js controller

北城以北 提交于 2019-12-05 16:55:39
How can this snippet code be unit tested using jasmine ? $scope.profileObject = ProfilesSharedObject; $scope.$watch("profileObject.startDate", function() { var startDate = $scope.profileObject.startDate._d; var endDate = $scope.profileObject.endDate._d; var newStartDate = moment(startDate).format("YYYY-MM-DD"); var newEndDate = moment(endDate).format("YYYY-MM-DD"); $scope.startDate = moment(startDate).format("MM/DD"); $scope.endDate = moment(endDate).format("MM/DD/YYYY"); $scope.getSleepData(newStartDate, newEndDate); }); where ProfileSharedObject is a angular js service Watch listeners are

How to mock environment files import in unit tests

一曲冷凌霜 提交于 2019-12-05 15:44:15
问题 In our angular app, we use environment files to load some config. environment.ts export const environment = { production: false, defaultLocale: 'en_US', }; We then use it in one of our service: import { environment } from '../../environments/environment'; import { TranslateService } from './translate.service'; @Injectable() export class LocaleService { constructor(private translateService: TranslateService){} useDefaultLocaleAsLang(): void { const defaultLocale = environment.defaultLocale;

jasmine前端测试框架教程

浪子不回头ぞ 提交于 2019-12-05 15:30:41
一、简介 jasmine 是一个用来编写Javascript测试的框架,它不依赖于任何其它的javascript框架。它有拥有灵巧而明确的语法可以让你轻松的编写测试代码。 二、下载地址 https://github.com/jasmine/jasmine/releases 三、使用方式 上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="jquery-1.11.3.js"></script> <link rel="stylesheet" type="text/css" href="jasmine-2.8.0/jasmine.css"> <script src="jasmine-2.8.0/jasmine.js"></script> <script src="jasmine-2.8.0/jasmine-html.js"></script> <script src="jasmine-2.8.0/boot.js"></script> </head> <body> <script> function get1() { $.getJSON("http://localhost:8080/test/get").then(function(result) { console

How do I spy on an Angular promise chain using Jasmine

◇◆丶佛笑我妖孽 提交于 2019-12-05 15:15:49
Using AngularJS, CoffeeScript and Jasmine (edited in WebStorm), I would like to unit test a chain of promises. Lets say I have the following example service: Angular Service class ExampleService stepData: [] constructor: (@$http) -> attachScopeMethod: (@scope) -> @scope.callSteps = => @step1().then -> @step2() step1: -> @$http.get('app/step/1').then (results) => @stepData[0] = results.data results step2: -> @$http.get('app/step/2').then (results) => @stepData[2] = results.data results This service allows me to attach a the method callSteps() to the scope. This method, when called, executes a

Is there a way to verify the order of spy executions with Jasmine?

雨燕双飞 提交于 2019-12-05 15:03:49
问题 I've got two objects that have been set up as spies with Jasmine: spyOn(obj, 'spy1'); spyOn(obj, 'spy2'); I need to verify that calls to spy1 come before calls to spy2 . I can check if both of them are called: expect(obj.spy1).toHaveBeenCalled(); expect(obj.spy2).toHaveBeenCalled(); but this will pass even if obj.spy2() was called first. Is there an easy way of verifying that one was called before the other? 回答1: Looks like the Jasmine folks saw this post or others like it, because this

Protractor: Correct use of waitReady.js file in my tests

时光总嘲笑我的痴心妄想 提交于 2019-12-05 14:53:38
We are using browser.wait extensively throughout the entire test suite. It became a headache when managing different timeouts for different scenarios. I came across a waitReady function( https://gist.github.com/elgalu/2939aad2b2e31418c1bb ), but I am unable to utilize it in my code. The directory looks like: (all files besides each other) . ├── conf.js ├── main.js ├── waitReady.js Main.js --->Contains all describe and it blocks require('./waitReady.js'); describe(...){ it{ code... expect(element.waitReady()).toBeTruthy(); }; }); For some reason the code does not find that function and my code