jasmine

Making both parts of expect resolve promises

梦想的初衷 提交于 2019-12-13 16:17:34
问题 The Problem: In Protractor, expect() is patched to implicitly understand promises which enables a shorthand assertion style . E.g.: expect(elm.getText()).toEqual("expected text"); elm.getText() here does not need to be explicitly resolved with then() and would be implicitly resolved by Protractor before the expectation is checked. But, what if the "to equal" part is also a promise. For instance, a text from an another element. In this case we have to resolve the second part explicitly: elm2

In Jasmine, how does one test a function that uses document.write

我只是一个虾纸丫 提交于 2019-12-13 16:06:50
问题 I have a function: var foo = function() { document.write( bar() ); }; My Jasmine test is: describe('has a method, foo, that', function() { it('calls bar', function() { spyOn(window, 'bar').andReturn(''); foo(); expect(bar).toHaveBeenCalled(); }); }); My problem is that the test passes and foo document.writes to the page, completely overwriting the page. Is there a good way to test this function? A related issue 回答1: You can spy on document.write var foo = function () { document.write('bar');

How to test the done and fail Deferred Object by using jasmine

倖福魔咒の 提交于 2019-12-13 15:47:50
问题 Here is the code about the javascript submit request (1). Here is the test about mocking the ajax request by using jasmine (2). I would like to mock the server behaviour. Any ideas? See the comment in (1) and (2) for more details. P.S.: Actually in both case the done and the fail Deferred Object of fakeFunction are called. (1) submitForm: function () { // the server execute fail only if message.val() is empty // and I would like to mock this behaviour in (2) backendController.submitForm

CallThrough injected spy

我们两清 提交于 2019-12-13 12:50:43
问题 I'm doing some unitTests and my scenario is the following. I have like 50 tests whose call to a service function must be the same, but for one single test It will be so helpfull if I can call the original method. I tried with the and.callThrough but It's not working correctly. I'm trying to override the spy too but I can't. What I'm doing wrong? beforeEach(inject(function($controller, _myService_){ spyOn(_myService_, 'getSomeData').and.callFake(function(data, params){ return dummyData; });

How to remove module not found error in angular?

£可爱£侵袭症+ 提交于 2019-12-13 12:42:32
问题 I make a simple example of controller here . http://plnkr.co/edit/dplJ6sf4kgiwJ5pXu4GE?p=preview and I want to test the controller .I am able to test my controller online Here is my code to test my controller online http://plnkr.co/edit/xzvhXHPoUdulOM9clOkQ?p=preview But when I try to run that same work on my computer my test case are fail I am getting error this Module 'app.home' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that

Why should I mock HTTP requests inside unit tests?

自作多情 提交于 2019-12-13 12:28:24
问题 I'm working on a project and we've begun to write Jasmine unit tests. This application, like any good JS app does a lot of asynchronous fetching of data. I see that angular provides $httpBackend to mock up an HTTP request. I've also read and heard that it's a bad idea to test AJAX requests in your controller, and thus the raison d'etre for $httpBackend. Why is it not a good idea to test AJAX calls? How do big JS applications get around this fact? When does the actual testing of hitting the

How to mock JQuery with Jasmine?

拟墨画扇 提交于 2019-12-13 12:27:42
问题 How can I test that a certain JQuery selector has been executed with Jasmine? I'm trying to do the following: spyOn($.fn, 'init').andCallThrough(); // my code expect($.init).toHaveBeenCalled(); But after this call, $('div') returns Object { selector="div", context=document, NaN=div.spec, more...} , though it has to return (and $.fn.init('div') does return it): [div.jasmine_reporter, div.banner, div.logo, 4 more...] . This stuff naturally breaks the code since the JQuery object is no longer

Variable scope in protractor

ぃ、小莉子 提交于 2019-12-13 09:22:26
问题 I am running protractor and jasmine to run unit tests. I need to know the build version of my web app in order to execute different tests. I have declared a variable to store this version value. var version =''; I am getting the version number by using the following code. menuObject.modaltext.getText().then(function(text) { version = text.slice(79,86); console.log(version); browser.driver.sleep(7000); }); The version number is acquired correctly and is consoled properly. But when i use this

Locate element by a presence of a custom attribute

巧了我就是萌 提交于 2019-12-13 08:29:52
问题 How do you locate all those elements that has particular custom attribute in Protractor? I found similar questions on Stackoverflow and on net, but they uses xpath which puts restriction on tag. element(by.xpath('//div[@custom-attribute]')) As oppose to above example, I don't want to put restriction on tag since we have different tags with the same custom attribute. I'd like locate all elements with the attribute regardless of tag. Is that possible? 回答1: You can use a CSS selector locator:

Element is not currently visible and so may not be interacted with when clicking a button

我们两清 提交于 2019-12-13 07:06:23
问题 My Protrator code is element(by.dataHook("delete-button")).click(); Getting: Element is not currently visible and so may not be interacted with HTML source: <button class="md-icon-but" type="button" ng-transclude="" ng-click="g" translate="loc" aria-label="Delete" title="Delete"> <md-icon md-svg-icon="ass" data-hook="delete-button" class="ng-scope" aria-hidden="true"><svg xmlns="ht" width="100%" height="100%" viewBox="0 0 24 24" fit="" preserveAspectRatio="xMidYMid meet" focusable="false">