jasmine

jasmine: spyOn(obj, 'method').andCallFake or and.callFake?

☆樱花仙子☆ 提交于 2019-12-17 16:41:59
问题 I want to mock test data in my Jasmine tests. Here are two versions: // version 1: spyOn(mBankAccountResource, 'getBankAccountData').and.callFake(fakedFunction); // version 2: spyOn(mBankAccountResource, 'getBankAccountData').andCallFake(fakedFunction); When I execute my tests with a browser (Chrome, Firefox) then the first version works. However, when I run the same test with phantomjs, I have to use the second version. Otherwise, it complains that the function is not defined. Here are the

angularjs route unit testing

ε祈祈猫儿з 提交于 2019-12-17 15:23:32
问题 As we see here in http://docs.angularjs.org/tutorial/step_07, angular.module('phonecat', []). config(['$routeProvider', function($routeProvider) { $routeProvider. when('/phones', {templateUrl: 'partials/phone-list.html', controller: PhoneListCtrl}). when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: PhoneDetailCtrl}). otherwise({redirectTo: '/phones'}); }]); routing test is suggested to be done with e2e test, it('should redirect index.html to index.html#/phones',

Testing directives that require controllers

↘锁芯ラ 提交于 2019-12-17 15:23:00
问题 So I did see another question: How to mock required directive controller in directive UT which is basically my problem but it seems the answer to this thread was "change your design." I wanted to make sure there is no way to do this. I have a directive that declares a controller which is used by children directives. I am now trying to write jasmine tests for the children directive but I cant get them to compile in the tests because they are dependent on the controller. Here is what it looks

Jasmine - Spying on a method call within a constructor

只谈情不闲聊 提交于 2019-12-17 15:22:27
问题 I want to test whether the following method is called with in my Javascript object constructor. From what I have seen in the Jasmine documentation, I can spy on a constructor method and I can spy on methods after an object has been instantiated, but I can't seem to be able to spy on a method before the object is constructed. The object: Klass = function() { this.called_method(); }; Klass.prototype.called_method = function() { //method to be called in the constructor. } I want to do something

Unit testing AngularJS factories that have dependencies

僤鯓⒐⒋嵵緔 提交于 2019-12-17 15:15:16
问题 When unit testing an Angular factory (with Karma + Jasmine), how do I inject a stub dependency into the factory under test? Here's my factory: mod = angular.module('myFactoryMod', []); mod.factory('myFactory', [ '$log', 'oneOfMyOtherServices', function($log, svc) { return makeSomethingThatDoesSomethingWithTheseDependencies($log, svc); } ]); oneOfMyOtherServices is needed when instantiating my factory. Here's my test: it('can get an instance of my factory', function() { var

How to spyOn a value property (rather than a method) with Jasmine

我的未来我决定 提交于 2019-12-17 10:33:26
问题 Jasmine's spyOn is good to change a method's behavior, but is there any way to change a value property (rather than a method) for an object? the code could be like below: spyOn(myObj, 'valueA').andReturn(1); expect(myObj.valueA).toBe(1); 回答1: In February 2017, they merged a PR adding this feature, they released in April 2017. so to spy on getters/setters you use: const spy = spyOnProperty(myObj, 'myGetterName', 'get'); where myObj is your instance, 'myGetterName' is the name of that one

How to spyOn a value property (rather than a method) with Jasmine

一曲冷凌霜 提交于 2019-12-17 10:33:01
问题 Jasmine's spyOn is good to change a method's behavior, but is there any way to change a value property (rather than a method) for an object? the code could be like below: spyOn(myObj, 'valueA').andReturn(1); expect(myObj.valueA).toBe(1); 回答1: In February 2017, they merged a PR adding this feature, they released in April 2017. so to spy on getters/setters you use: const spy = spyOnProperty(myObj, 'myGetterName', 'get'); where myObj is your instance, 'myGetterName' is the name of that one

Protractor Angular 2 Failed: unknown error: angular is not defined

时光毁灭记忆、已成空白 提交于 2019-12-17 09:46:13
问题 I'm getting the following error: Failed: unknown error: angular is not defined This only happens when using angular specific selectors like "by.model". But selectors such as "by.css" work correctly . This is an Angular 2 app... Test it('should set focus', () => { //This works //var input = element(by.css('myclass')); //This fails var input = element(by.model('config.value')); input.clear(); input.sendKeys('test'); input.sendKeys(Key.TAB); input.click(); var highlightedText = browser

How to debug timed out waiting for asynchronous Angular tasks? Failure to find elements on angular page occurring

梦想的初衷 提交于 2019-12-17 07:29:40
问题 Edit: Note that I found the root of my problem after help from @ernst-zwingli, so if you have this same error one of his noted fixes might help you out. My problem is a known issue with Protractor itself, if you think this may be you, I've expanded on my steps to pinpoint the root of the problem after my original question. I'm trying to use Protractor in an Angular2 (just Angular) application built using angular-cli. My problem: Elements on an Angular app page are not being found when browser

How to write unit testing for Angular / TypeScript for private methods with Jasmine

。_饼干妹妹 提交于 2019-12-17 03:50:33
问题 How do you test a private function in angular 2 ? class FooBar { private _status: number; constructor( private foo : Bar ) { this.initFooBar(); } private initFooBar(){ this.foo.bar( "data" ); this._status = this.fooo.foo(); } public get status(){ return this._status; } } The solution I found Put the test code itself inside the closure or Add code inside the closure that stores references to the local variables on existing objects in the outer scope. Later strip out the test code using a tool.