jasmine

Checking object equality in Jasmine

三世轮回 提交于 2019-12-02 19:53:25
Jasmine has built-in matchers toBe and toEqual . If I have an object like this: function Money(amount, currency){ this.amount = amount; this.currency = currency; this.sum = function (money){ return new Money(200, "USD"); } } and try to compare new Money(200, "USD") and the result of sum, these built-in matchers will not work as expected. I have managed to implement a work-around based on a custom equals method and custom matcher, but it just seems to much work. What is the standard way to compare objects in Jasmine? lukas.pukenis I was looking for the same thing and found an existing way to do

How to use Jasmine spies on an object created inside another method?

早过忘川 提交于 2019-12-02 19:14:44
Given the following code snippet, how would you create a Jasmine spyOn test to confirm that doSomething gets called when you run MyFunction ? function MyFunction() { var foo = new MyCoolObject(); foo.doSomething(); }; Here's what my test looks like. Unfortunately, I get an error when the spyOn call is evaluated: describe("MyFunction", function () { it("calls doSomething", function () { spyOn(MyCoolObject, "doSomething"); MyFunction(); expect(MyCoolObject.doSomething).toHaveBeenCalled(); }); }); Jasmine doesn't appear to recognize the doSomething method at that point. Any suggestions? When you

How to get Angular 2 element through class name in Jasmine

冷暖自知 提交于 2019-12-02 18:57:26
I can get element with using the fixture.debugElement.query(By.css('h1')); But what I should to do when I want get element through class name. Something like this fixture.debugElement.query(By.css('class="name"')) Paul Samsotha You use By.css to pass a css selector. So any selector you can use with css, you can use with By.css . And a selector for a class is simply .classname (with period). By.css('.classname') // get by class name By.css('input[type=radio]') // get input by type radio By.css('.parent .child') // get child who has a parent These are just some example. If you know css, then you

Test AngularJS factory function with Jasmine

℡╲_俬逩灬. 提交于 2019-12-02 18:47:37
I am very new to this (angularjs, jasmine, testacular) and I have this code (I simplified it a bit, leaving only what matters): //my_module.js angular.module('my_module', ['my_data']) .config([...]); .controller('my_controller', ['$scope', 'my_data', function($scope, my_data) { $scope.my_function = function() { return my_data.my_factory.save().then(function () { console.log('saved'); }, function() { console.log('Error'); }); } } ) //my_data.js angular.module('my_data', []) .factory('my_factory', ['$q', '$rootScope', function($q, $rootScope) { var my_factory= function(my_data) { angular.extend

What does the “it” function do in this code?

女生的网名这么多〃 提交于 2019-12-02 17:54:31
I'm hoping somebody could explain to me what "it" does (is used for) in AngularJS or just plain JavaScript (I'm not sure if it's specific to Angular). This, turns out, is a difficult thing to Google for, being named "it" and all. I've seen it used throughout the AngularJS docs. I'll give you an example from the ngShow page (it's code to hide/show a div containing a thumbs up or thumbs down). var thumbsUp = element(by.css('span.glyphicon-thumbs-up')); var thumbsDown = element(by.css('span.glyphicon-thumbs-down')); it('should check ng-show / ng-hide', function() { expect(thumbsUp.isDisplayed())

Why can't nested describe() blocks see vars defined in outer blocks?

余生颓废 提交于 2019-12-02 17:24:49
I've run into this issue in real code, but I put together a trivial example to prove the point. The below code works fine. I've set up a variable in my root describe() block that is accessible within my sub- describe() s' it() blocks. describe('simple object', function () { var orchard; beforeEach(function () { orchard = { trees: { apple: 10, orange : 20 }, bushes: { boysenberry : 40, blueberry: 35 } }; }); describe('trees', function () { it ('should have apples and oranges', function() { var trees = orchard.trees; expect (trees.apple).toBeDefined(); expect (trees.orange).toBeDefined(); expect

Sinon JS “Attempted to wrap ajax which is already wrapped”

梦想与她 提交于 2019-12-02 17:23:04
I got the above error message when I ran my test. Below is my code (I'm using Backbone JS and Jasmine for testing). Does anyone know why this happens? $(function() { describe("Category", function() { beforeEach(function() { category = new Category; sinon.spy(jQuery, "ajax"); } it("should fetch notes", function() { category.set({code: 123}); category.fetchNotes(); expect(category.trigger).toHaveBeenCalled(); } }) } You have to remove the spy after every test. Take a look at the example from the sinon docs: { setUp: function () { sinon.spy(jQuery, "ajax"); }, tearDown: function () { jQuery.ajax

How to inject a service in a directive unit test in AngularJS

允我心安 提交于 2019-12-02 17:09:34
I need to test a directive that does some calls to some injected services. The following piece of code is an example directive, that listens for events, and redirects the browser if enter is pressed inside a specified element. Edit: I get the feeling I may be wading in E2E testing land? angular.module('fooApp') .directive('gotoOnEnter', ['$location', function ($location) { var _linkFn = function link(scope, element, attrs) { element.off('keypress').on('keypress', function(e) { if(e.keyCode === 13) { $location.path(scope.redirectUrl); } }); } return { restrict: 'A', link: _linkFn }; }]); The

Mocking Controller Instantiation In Angular Directive Unit Test

橙三吉。 提交于 2019-12-02 17:09:20
I am unit testing an Angular directive and would like to mock or stub in some way the instantiation of the named controller in the unit test. So first I suppose on to some code... 'use strict'; angular.module('App.Directives.BreadCrumbs', []) .directive('kxBreadcrumbs', function () { return { restrict: 'E', controller: 'BreadCrumbsController', template: '<!-- Breadcrumbs Directive HTML -->' + '<ol class="breadcrumb">' + ' <li ng-repeat="crumb in crumbPath">' + ' <a ng-class="{true: \'disable\', false: \'\'}[crumb.last]" href="{{crumb.href}}" ng-click="updateCrumb(crumb.name)">{{crumb.name}}</a

what is the real difference between ng test and ng e2e

天涯浪子 提交于 2019-12-02 17:06:45
I am afraid someone close my question but I couldn't find a satisfying question (maybe because I am very limited in Angular 2+ world and I understood something wrong). As far as I could understand after few Hello World done and few YouTube demo watched: ng test: you write your test using Jasmine language you test your test with many Browsers available using Karma you execute either unit or integrated testing all xxx.compnent.spec.ts run and a final report similar to JUnit is showed in browser ng e2e: you write your test using Jasmine language you test your test with many Browsers available