jasmine

How to Jasmine test code within angular module run block

狂风中的少年 提交于 2019-11-30 18:28:44
I would like to Jasmine test that Welcome.go has been called. Welcome is an angular service. angular.module('welcome',[]) .run(function(Welcome) { Welcome.go(); }); This is my test so far: describe('module: welcome', function () { beforeEach(module('welcome')); var Welcome; beforeEach(inject(function(_Welcome_) { Welcome = _Welcome_; spyOn(Welcome, 'go'); })); it('should call Welcome.go', function() { expect(Welcome.go).toHaveBeenCalled(); }); }); Note: welcome (lowercase w) is the module Welcome (uppercase W) is the service Managed to figure it out. Here is what I came up with: 'use strict';

Angular Jasmine UI router inject resolve value into test

安稳与你 提交于 2019-11-30 17:56:46
In my Angular app, UI router resolves a promise into the controller. When trying to test this controller, Karma is complaining about an unknown provider. How do I inject a fake object into the test to represent this resolve object. My app's code looks something like: angular.module('myapp') .config(function($stateProvider, $urlRouterProvider) { $stateProvider .state('tab.name', { ... resolve: { allTemplates: function(Templates) { return Templates.all().then(function(templates) { return templates; }); } } }) }) .controller('QueriesCtrl', function(allTemplates, UserQuery) { var vm = this; vm

Run Karma using Gradle?

a 夏天 提交于 2019-11-30 17:51:42
I have Jasmine unit tests that I would like to run and get Istanbul code coverage reports for. I can run a command line script to run Karma in Jenkins, but it would be nice to have everything execute using my build.gradle. Is it possible to run Karma using Gradle? I've searched google, but I can't seem to find a solution. Thanks I solved this by creating and running an executable task in the build.gradle file: task karma(type:Exec) { commandLine 'karma', 'start', '--single-run true' } Following up here for anyone searching this in 2016 or later: There's now a Gradle Karma Plugin that is worth

How to make sharded protractor tests report after ALL tests finish (instead of per-file)?

无人久伴 提交于 2019-11-30 17:46:43
问题 When sharding tests (aka running tests in parallel; ie: shardTestFiles: true ), instead of reporting results when all tests are finished, Protractor reports results for each file when finished. Does anyone have a workaround for this? I've tried using an inline plugin with hooks teardown and postTest , but neither alter this behavior (still per test file reporting). I've also tried setting up the reporter outside of onPrepare , as suggested here, but also, no joy. I'm hoping for an easy

Can I dynamically create a test spec within a callback?

耗尽温柔 提交于 2019-11-30 17:45:51
问题 I want to retrieve a list of elements on a page, and for each one, create a test spec. My (pseudo) code is :- fetchElements().then(element_list) { foreach element { it("should have some property", function() { expect("foo") }) } } When I run this, I get "No specs found", which I guess makes sense since they are being defined off the main path. What's the best way to achieve dynamically created specs? 回答1: There are major problems preventing it to be easily achieved: the specs you are creating

Testing Angular2 components that use setInterval or setTimeout

拥有回忆 提交于 2019-11-30 17:31:54
I have a fairly typical, simple ng2 component that calls a service to get some data (carousel items). It also uses setInterval to auto-switch carousel slides in the UI every n seconds. It works just fine, but when running Jasmine tests I get the error: "Cannot use setInterval from within an async test zone". I tried wrapping the setInterval call in this.zone.runOutsideAngular(() => {...}), but the error remained. I would've thought changing the test to run in fakeAsync zone would solve the problem, but then I get an error saying XHR calls are not allowed from within fakeAsync test zone (which

How to get Jasmine's spyOnProperty to work?

谁说我不能喝 提交于 2019-11-30 17:24:43
I saw this post post and was excited to try it out, but I'm unable to get it working. Trying to keep this simple just to figure out what's wrong, but even this is failing. export class SomeService { ... private _myValue: Boolean = false; get myValue(): Boolean { return this._myValue; } set myValue(helper: Boolean) { this._myValue = helper; } And in my unit test, I have: it('should ', inject([SomeService], (someService: SomeService) => { let oldValue = someService.myValue; expect(oldValue).toBe(false); // passes, clearly we can use our getter someService.myValue = true; expect(someService

Custom Jasmine reporter in Protractor tests

久未见 提交于 2019-11-30 17:19:48
I can not find how to change reporter style in protractors runner using jasmine framework. What I have right now is: But I would like something more like: Is there a way to add custom reporter for jasmine that would show current test running instead of DOTS and Fs? I am building a jasmine reporter that does exactly what you want, jasmine-spec-reporter . fer Add the isVerbose flag to the protractor config, it's false by default: exports.config = { . . . // Options to be passed to Jasmine-node. jasmineNodeOpts: { showColors: true, defaultTimeoutInterval: 30000, isVerbose: true } }; Automation

How to provide mock files to change event of <input type='file'> for unit testing

大兔子大兔子 提交于 2019-11-30 17:12:17
I'm having difficulties with a unit test in which I want to verify the processing of a file, which would usually be selected in the view via <input type='file'> . In the controller part of my AngularJS app the file is processed inside the input's change event like so: //bind the change event of the file input and process the selected file inputElement.on("change", function (evt) { var fileList = evt.target.files; var selectedFile = fileList[0]; if (selectedFile.size > 500000) { alert('File too big!'); // ... I'd like evt.target.files to contain my mock data instead of the user's selected file

How to provide mock files to change event of <input type='file'> for unit testing

坚强是说给别人听的谎言 提交于 2019-11-30 16:29:14
问题 I'm having difficulties with a unit test in which I want to verify the processing of a file, which would usually be selected in the view via <input type='file'> . In the controller part of my AngularJS app the file is processed inside the input's change event like so: //bind the change event of the file input and process the selected file inputElement.on("change", function (evt) { var fileList = evt.target.files; var selectedFile = fileList[0]; if (selectedFile.size > 500000) { alert('File