jasmine

Unit Testing socket.io in Angular

本秂侑毒 提交于 2020-01-02 00:47:09
问题 I am trying to unit test an angular service in my application, which creates socket.io client. My service looks something like this: export class SocketService { private name: string; private host: string = window.location.protocol + "//" + window.location.hostname + ":3000"; socket: io.Socket; constructor() { } subscribeEvent(name: string): Observable<IJobResp> { this.setup(name); return new Observable(observer => { this.socket.on('job', val => observer.next(val)) }) } private setup(name:

How do I write FileReader test in Jasmine?

点点圈 提交于 2020-01-02 00:13:49
问题 I'm trying to make this test work, but I couldn't get my head around how to write a test with FileReader. This is my code function Uploader(file) { this.file = file; } Uploader.prototype = (function() { function upload_file(file, file_contents) { var file_data = new FormData() file_data.append('filename', file.name) file_data.append('mimetype', file.type) file_data.append('data', file_contents) file_data.append('size', file.size) $.ajax({ url: "/upload/file", type: "POST", data: file_contents

How to test that an angular factory function using a promise is throwing an error in jasmine

自作多情 提交于 2020-01-01 19:11:17
问题 Here's how my function looks like. var myFunc = function(){ return functionReturningaPromise() .then(function(){ //success, doesn't matter what happens here }) .catch(function(err){ //handle error here and then throw to handle higher throw new Error('Error in my function'); }) } I need the function to be this way to handle an error inside this function and then throw an error to handle on higher level. But i don't know how to test it with jasmine. I know how to control the promises for

Use Jasmine to stub JS callbacks based on argument values

拟墨画扇 提交于 2020-01-01 18:59:08
问题 I've got a JS method in my node.js app that I want to unit test. It makes several calls to a service method, each time passing that service a callback; the callback accumulates the results. How can I use Jasmine to stub out the service method so that each time the stub is called, it calls the callback with a response determined by the arguments? This is (like) the method I'm testing: function methodUnderTest() { var result = []; var f = function(response) {result.push(response)}; service

Classes defined in CoffeeScript not found by Jasmine specs

我是研究僧i 提交于 2020-01-01 14:36:06
问题 I am building a backbone.js app on a Rails 3.1 back-end. I'm using CoffeeScript to write the backbone classes, and Jasmine (via jasmine-headless-webkit) for testing. Given the following (partial) tree: . ├── app │ ├── assets │ │ ├── javascripts │ │ │ └── views │ │ │ ├── avia_view.js.coffee ├── spec │ ├── javascripts │ │ └── views │ │ └── avia_view_spec.js.coffee ... I would expect avia_view_spec.js.coffee to know about Avia.AviaView , which is defined in avia_view.js.coffee . However, I get

Classes defined in CoffeeScript not found by Jasmine specs

不问归期 提交于 2020-01-01 14:34:28
问题 I am building a backbone.js app on a Rails 3.1 back-end. I'm using CoffeeScript to write the backbone classes, and Jasmine (via jasmine-headless-webkit) for testing. Given the following (partial) tree: . ├── app │ ├── assets │ │ ├── javascripts │ │ │ └── views │ │ │ ├── avia_view.js.coffee ├── spec │ ├── javascripts │ │ └── views │ │ └── avia_view_spec.js.coffee ... I would expect avia_view_spec.js.coffee to know about Avia.AviaView , which is defined in avia_view.js.coffee . However, I get

How to test file upload with Jasmine BDD

荒凉一梦 提交于 2020-01-01 10:29:08
问题 I'm using jQuery file upload plugin for managing image file uploads. What will be the right way to test image submission with Jasmine. 回答1: I would take a look at using the helper library Sinon.js. You can mock the XMLHttpRequest object to intercept the post request, and verify that your plugin code is submitting images correctly. In particular, take a look at this part of the Sinon.js documentation: http://sinonjs.org/docs/#server 来源: https://stackoverflow.com/questions/10673001/how-to-test

AngularJS Unit Testing - Various patterns for injecting dependencies

▼魔方 西西 提交于 2020-01-01 10:03:40
问题 I'm new to unit testing and am mainly learning from examples that I find. The problem is that I've seen so many different patterns that it's hard to understand what the differences are between them. And how to combine those patterns for various use cases. Below is one such pattern: var $rootScope, $window, $location; beforeEach(angular.mock.module('security.service', 'security/loginModal.tpl.html')); beforeEach(inject(function(_$rootScope_, _$location_) { $rootScope = _$rootScope_; $location

Angular 5 Jasmine Error: Expected one matching request for criteria found none

淺唱寂寞╮ 提交于 2020-01-01 09:48:53
问题 I have a very simple service call and a jasmine test for it. Service call: myServiceCall(testId: number) : void { const url = `${this.url}/paramX/${testId}`; this.http.put(url, {},{headers: this.headers}).subscribe(); } My Test Method: it('should call myServiceCall', inject([MyService], (service: MyService) => { let testId = undefined; service.myServiceCall(testId); let req = httpMock.expectOne(environment.baseUrl + "/paramX/123"); expect(req.request.url).toBe(environment.baseUrl + "/paramX

Angular 5 Jasmine Error: Expected one matching request for criteria found none

六月ゝ 毕业季﹏ 提交于 2020-01-01 09:48:28
问题 I have a very simple service call and a jasmine test for it. Service call: myServiceCall(testId: number) : void { const url = `${this.url}/paramX/${testId}`; this.http.put(url, {},{headers: this.headers}).subscribe(); } My Test Method: it('should call myServiceCall', inject([MyService], (service: MyService) => { let testId = undefined; service.myServiceCall(testId); let req = httpMock.expectOne(environment.baseUrl + "/paramX/123"); expect(req.request.url).toBe(environment.baseUrl + "/paramX