spy

Angular unit test spyOn not detecting my call

孤人 提交于 2019-12-24 19:24:55
问题 I've got a problem with a unit test I can't solve. Here is my test : fit('should call the alert service in case something went wrong getting the list of files', async(() => { spyOn(this.component.alert, 'error'); this.component.onInputChange('error'); this.fixture.whenStable().then((() => { expect(this.component.alert.error).toHaveBeenCalledWith('an error occured'); })); })); And here is the part of the component I'm testing : private onInputChange (search: string) { const that = this; this

sinon - spy on toString method

六眼飞鱼酱① 提交于 2019-12-23 05:20:02
问题 In my file, I have something like this: if(somevar.toString().length == 2) .... How can I spy on toString from my test file? I know how to spy on things like parseInt with: let spy = sinon.spy(global, 'parseInt') But that doesn't work with toString since it's called on the value, I tried spying on Object and Object.prototype , but that doesn't work either. 回答1: You can't call sinon.spy or sinon.stub on a method of primitive value like: sinon.spy(1, 'toString') . This is wrong. You should call

Trying to spy (Jasmine) on Array.prototype methods causes stack overflow

两盒软妹~` 提交于 2019-12-22 05:41:51
问题 This is pretty odd. Using the testem runner with jasmine2 and the following spec executes (though it correctly flags that there are no expectations): describe('Spying on array.prototype methods', function(){ it('should work this way', function(){ spyOn( Array.prototype, 'push' ).and.callThrough(); // expect(1).toBe(1); }); }); However, add an expect (any expect !) and it causes the stack to overflow with the following message in the testem console: RangeError: Maximum call stack size exceeded

How do you spy on AngularJS's $timeout with Jasmine?

浪尽此生 提交于 2019-12-22 05:41:11
问题 I am trying to spy on $timeout so that I can verify that it has not been called. Specifically, my production code (see below) calls $timeout as a function, not an object: $timeout(function() { ... }) and not $timeout.cancel() // for instance Jasmine, however, requires an object to be spied upon, like this: spyOn(someObject, '$timeout') I don't know what 'someObject' would be though. I am using Angular mocks, if that makes any difference. Edit: The relevant production code I'm trying to test

Mockito spy method not working

老子叫甜甜 提交于 2019-12-21 22:36:21
问题 I'm in trouble with mockito.spy method. I'm recently arrived on a "old" project and my first task is to add mockito in it, and to do real unit test :) the project has many conception problems but its not the point here ;) I explain my problem: I have a class public class Tutu{ public Tutu(){ } } public class Toto{ public Toto(){ } public int executeToto(Tutu tutu){ //do some stuff return 5; } } public class Titi{ private Toto toto; public Titi(){ this.toto = new Toto(); } public void

Spying on a constructor using Jasmine

浪子不回头ぞ 提交于 2019-12-17 16:43:06
问题 I am using Jasmine to test if certain objects are created and methods are called on them. I have a jQuery widget that creates flipcounter objects and calls the setValue method on them. The code for flipcounter is here: https://bitbucket.org/cnanney/apple-style-flip-counter/src/13fd00129a41/js/flipcounter.js The flipcounters are created using: var myFlipCounter = new flipCounter("counter", {inc: 23, pace: 500}); I want to test that the flipcounters are created and the setValue method is called

Jasmine - Spying on a method call within a constructor

只谈情不闲聊 提交于 2019-12-17 15:22:27
问题 I want to test whether the following method is called with in my Javascript object constructor. From what I have seen in the Jasmine documentation, I can spy on a constructor method and I can spy on methods after an object has been instantiated, but I can't seem to be able to spy on a method before the object is constructed. The object: Klass = function() { this.called_method(); }; Klass.prototype.called_method = function() { //method to be called in the constructor. } I want to do something

Tracking email with PHP and image

爱⌒轻易说出口 提交于 2019-12-17 15:14:55
问题 I have seen the service like spypig.com placing a small image in the email and tracking when it is opened and from where. They track city, country, IP address etc. How is this done? How do we know when the mail is opened? And how is the image generated? How is the IP address detected and how is it possible to know location from it? 回答1: Basically, in the HTML body of your email, there will be an <img> tag that would look like this : <img src="http://www.yoursite.com/tracker.php?id=123456" alt

Redux-Form : How to mock formValueSelector in Jest

心已入冬 提交于 2019-12-14 03:57:49
问题 I have a component Demo whose Label depends on the current value of a field in the redux-form state. I am using formValueSelector to get the current value of "param" field from the form state. It works fine. However, while running npm test, the selector function always returns undefined. How can I mock it? Please let me know if I am doing this in a wrong way. I have a component like class Sample extends React.Component { render() { const {param, state} = this.props; const selector =

How to spy on a class property arrow function using Jest

☆樱花仙子☆ 提交于 2019-12-12 08:24:50
问题 How can I spy on a class property arrow function using Jest? I have the following example test case which fails with the error Expected mock function to have been called. : import React, {Component} from "react"; import {shallow} from "enzyme"; class App extends Component { onButtonClick = () => { // Button click logic. }; render() { return <button onClick={this.onButtonClick} />; } } describe("when button is clicked", () => { it("should call onButtonClick", () => { const app = shallow(<App /