jasmine

How to load mock data from JSON file in Angular 2 karma jasmine test?

喜欢而已 提交于 2019-12-04 02:58:23
I writing karma jasmine test case for angular 2 , And we came across requirement to mock data in separate JSON file as data is huge (want to make sure code cleanness). For this I searched a lot but didn't find proper solution. We already mocking HTTP service using MockBackend , So we can't use HTTP service of angular to load JSON as it eventually request will go to MockBackend. So is there any another way without using any third party lib like jasmine-jquery or Karma-jasmine-preprocessor ? More kind of Angular JS 2 way . I had the same issue! Finally, I realized that just using the require()

Unknown provider CookieStore

我怕爱的太早我们不能终老 提交于 2019-12-04 02:49:32
I am having service where I want to use the $cookieStore module. It works fine, but when unit testing it breaks, and gives the error: "$cookieStoreProvider <- $cookieStore <- filtersService". The service looks like this: serviceModule.factory('filtersService', ['$rootScope', '$location', '$cookieStore', function($rootScope, $location, $cookieStore){ return { getFilters: function(){...} } And the unit test service looks like this: describe('filtersService tests', function(){ var filtersService; beforeEach(module('App.services')); beforeEach(inject(function(filtersService, urlService, $location)

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

孤街醉人 提交于 2019-12-04 02:44:47
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 ? pkozlowski.opensource It is a bit hard to provide precise guidance without seeing a live code example (this is why it is usually a good idea to provide a plunk

How to install and run npm jasmine locally

自作多情 提交于 2019-12-04 02:32:23
Installing some npm packages globally is evil sometimes. I don't want to install jasmine like that: npm install -g jasmine How can I install and use jasmine without -g attribute? 1) You need to init an npm project. On the 5-th step of the wizard (question test command: ) input jasmine npm init 1b) If you init npm project before, make sure you have these lines in your package.json "scripts": { "test": "jasmine" }, 2) Install jasmine as a local dependency npm i --save-dev jasmine 3) To init jasmine (alternative for global jasmine init ) npm test init 4) To create example tests (alternative for

Karma vs Chutzpah

↘锁芯ラ 提交于 2019-12-04 02:20:08
I have been trying to figure out what is the best way to write/run automated jasmine tests in visual studio. Currently, I am using jasmine with Resharper (using PhantomJS) and the test can be run from visual studio. Now I want to run the tests as part of continuous integration and very are using TFS. Searching online give me few options which made me rather confused. 1) Use Chutzpah as a test runner to execute jasmine tests. 2) Use Karma as a test runner (but it also requires Chutzpah test adaptor for visual studio). I get the feeling that using Karma is somewhat better than anything else but

Globally mock object in angularjs for jasmine/karma testing

时光总嘲笑我的痴心妄想 提交于 2019-12-04 01:54:33
I have an object that I am mocking up for unit testing. Essentially in my test file I mock it up as follows: var mockObject = { mockMethod1 : function() {return true}, mockMethod2 : function() {return true} }; beforeEach(module('myModule') , function ($provide) { $provide.value('realObject',mockObject); }); The way i understand it is that as I test functionality in my module etc... anywhere that references the "realObject" will use my "mockObject" My issue is that I have made multiple js files for testing and I do not want to define my "mockObject" in each one of them ... nor do i want to

How to mock environment files import in unit tests

二次信任 提交于 2019-12-04 01:38:35
In our angular app, we use environment files to load some config. environment.ts export const environment = { production: false, defaultLocale: 'en_US', }; We then use it in one of our service: import { environment } from '../../environments/environment'; import { TranslateService } from './translate.service'; @Injectable() export class LocaleService { constructor(private translateService: TranslateService){} useDefaultLocaleAsLang(): void { const defaultLocale = environment.defaultLocale; this.translateService.setUsedLang(defaultLocale); } } So I use the values in environment file in a

Testing a debounced function in AngularJS with Jasmine never calls the function

♀尐吖头ヾ 提交于 2019-12-04 01:32:26
I have a method in a service that uses underscore's debounce. Inside that method is a call to a method on a different service. I'm trying to test that the different service is called. In my attempts to test the debounced method, the different services' method is never called, and jasmine fails with: "Expected spy aMethod to have been called." I know for a fact that it IS called (it logs to console in chrome), it's just called AFTER the expectation already failed. So... (preferably) without adding Sinon or another dependency and with bonus points* given to a solution doesn't have to turn the _

How do I test a form submit in Jasmine?

核能气质少年 提交于 2019-12-04 01:06:10
I have a form that does some extensive Javascript stuff before finally POSTing to it's ACTION URL. I am writing some Jasmine unit tests and want to make sure the Javascript stuff happens when the form is submitted. However, I definitely don't want the page to go to the ACTION URL while I am unit testing. I saw what seemed like a good suggestion here: http://groups.google.com/group/jasmine-js/browse_thread/thread/a010eced8db17c1a?pli=1 ...but, as I am fairly new to Jasmine, I am unsure how to implement it and cannot find any pertinent examples on the web. Does anyone have some sample code I

How do I debug a Jasmine spec run with Testacular (Karma)?

我的梦境 提交于 2019-12-04 01:01:42
I have a small project with Jasmine specs and I am using Testacular as my test runner. I don't understand how I can debug, both the app code or the spec code. When I try to set a breakpoint in Chrome Dev Tools it isn't hit next time the specs run because it loads the files every time with a new query string. The best thing I found so far is to use console.log() but I would rather use Chrome Dev Tools breakpoints. (I am using Visual Studio 2012 for development.) Thanks Testacular is not the best tool to use for debugging. It's power lies in the fact that it will run your tests in multiple