jasmine

AngularJS, prevent init method on controller from launching during jasmine tests

馋奶兔 提交于 2019-12-09 15:32:14
问题 I have a controller with a init() method launched on instantiation. It does a bunch of things which are useful for my app in a live environment, but that messes up with my unit-tests spies. Is there a way to prevent its call when instantiating the controller in the unit-test environment ? Or maybe a way to have it called automatically in the webapp context without making an explicit call to init() at the end of the controller code ? 回答1: It is a bit hard to provide precise guidance without

AngularJS + Karma: reuse a mock service when unit testing directives or controllers

匆匆过客 提交于 2019-12-09 10:51:39
问题 I'm working with AngularJS + Karma. configService manages the settings of my app (e.g. the background-color, wether it's on debug mode, general permissions...). It loads initial data with $http. I wrote the test successfully for the service but my directives and controllers use it. When I write the unit tests for directives, I have to mock the service. I know I can do: spyOn(configService, 'getBackgroundColor').andCallFake(function (params) { return "red"; }); but the service has 25+ methods

一张图掌握移动Web前端所有技术(大前端、工程化、预编译、自动化)

主宰稳场 提交于 2019-12-09 10:42:09
你要的移动web前端都在这里! 大前端方向:移动Web前端、Native客户端、Node.js、 大前端框架:React、Vue.js、Koa 跨终端技术:HTML 5、CSS 3、JavaScript 跨平台框架:React Native、Cordova 前端工程化:Grunt、Gulp、Webpack 前端预编译:Babel、Sass、Less 自动化测试:Jasmine、Mocha、Karma 一图在手,应有尽有! 更多信息参考: 来源: oschina 链接: https://my.oschina.net/u/59463/blog/1539543

How can I test a directive using templateUrl in Chutzpah's headless browser

拥有回忆 提交于 2019-12-09 08:03:59
问题 Does anyone know how to get a headless browser like Chutzpah's Visual Studio Test Adapter to allow a directive to access its .html template file? Chutzpah uses PhantomJS for a headless browser which appears to limit my options. I'm using Chutzpah Test Adapter 2.5 and AngularJS 1.2.0-r.2. I get the error: Unexpected request: GET myApp/directives/template.html Which is caused by Angular attempting to use the $http service to access my directive's template. I've found a few different workarounds

How do I declare a variable in a specific scope in coffeescript?

十年热恋 提交于 2019-12-09 07:31:46
问题 I'm trying to write a jasmine test in coffeescript that uses a beforeEach block. This runs into a problem with coffeescript's variable scoping. Here's what I'd like to write: describe 'PhoneDetailCtrl', () -> beforeEach () -> scope = angular.scope() $browser = scope.$service('$browser') it 'should fetch phone detail', () -> scope.params = {phoneId:'xyz'} $browser.xhr.expectGET('phones/xyz.json').respond({name:'phone xyz'}) ctrl = scope.$new(PhoneDetailCtrl) expect(ctrl.phone).toEqualData({})

Protractor, Jasmine, and stopping test on first fail

*爱你&永不变心* 提交于 2019-12-09 06:22:27
问题 While trying to figure out how to make certain jasmine expect statements dependent on a previous expect statement I discovered that previous to Jasmine 2.3.0, there was not a way. (see Stop jasmine test after first expect fails) However, Jasmine 2.3.0 added an option stopSpecOnExpectationFailure that when set to true will stop a test on the first failure. Excited by this prospect, I modified my conf.js to include the option: /* * conf.js */ exports.config = { framework: 'jasmine', specs: [

AngularJS Change Constant Value that gets passed to Config for Unit Test

£可爱£侵袭症+ 提交于 2019-12-09 04:39:22
问题 Is there anyway to change the constant value that gets sent to the config function of the module for a Unit Test? I have the following (fiddle here): //--- CODE -------------------------- var module = angular.module("myApp", []); module.constant("myConstant", "foo"); module.provider("awesomeStuff", function () { var value; this.setValue = function (val) { value = val; }; this.$get = function () { return { myValue: value }; }; }); module.config(function (myConstant, awesomeStuffProvider) { /

How to debug PhantomJS when running through Karma

自作多情 提交于 2019-12-09 04:37:39
问题 I test through Jasmine, Karma and a variety of browsers. I'm currently debugging a test that fails only in PhantomJS. I'd like to debug this call, so I've setup a custom PhantomJS launcher in karma that runs it with the debug port open. I'm able to access the remote debugger in Chrome through that port, however, it seems to know nothing about any of my test files. It reports an "about:blank" and a "localhost:9876" (the karma server that Phantom is hitting) but when I try to debug that

How to unit-test canActivate guard method of angular2 using Jasmine?

◇◆丶佛笑我妖孽 提交于 2019-12-09 04:37:01
问题 Sorry for asking this type of question. But I'm not able to find any blog or youtube tutorials on writing the canActivate guard file testing. Nor in the official documentation there is anything mentioned. any help will be much appreciated. 回答1: since no one answered my question, so I'm pasting my code snippet for the reference to help people who might get this situation. sampleLoggedIn.guard.ts import {Injectable} from '@angular/core'; import {Router, CanActivate} from '@angular/router';

How do I test a private method with Jasmine Unit tests

ⅰ亾dé卋堺 提交于 2019-12-09 03:26:31
问题 I want to call a private method in my component Private Method: private test(): void { return true; } Spec It: it('should call test method and return true', () => { const response = component.test(); expect(response).toBeTruthy(); }); Issue: Says: "Property 'test' is private and only accessible within class 'MyTestComponent'." 回答1: You could use component['test'](); // OR in your component, add callMethod() { this.test(); } But if I were you, I would remove the private attribute. In