spy

Using @Spy and @Autowired together

久未见 提交于 2021-02-05 22:57:33
问题 I have a Service Class with 3 methods, Service class is also using some @Autowired annotations. Out of 3 methods, I want to mock two methods but use real method for 3rd one. Problem is: If I am using @Autowired with @Spy, all three real method implementation is being called. If I am using @Spy only, call to real method is return with Null pointer as there is no initialisation of Autowired objects. 回答1: I know about these two options: Use @SpyBean annotation from spring-boot-test as the only

jest typescript - Mock Date constructor

為{幸葍}努か 提交于 2020-05-15 06:20:26
问题 I'm trying to mock new Date() to return a specific date. The following code: const now = new Date() jest.spyOn(global, 'Date').mockImplementation(() => now) gives a compilation error: Argument of type '() => Date' is not assignable to parameter of type '() => string'. Type 'Date' is not assignable to type 'string' . I think the reason is that jest thinks I'm trying to mock Date() instead of new Date() . Indeed, Date() returns a string. How can I solve this issue? 回答1: A workaround is to use

stubbing process.exit with jest

隐身守侯 提交于 2020-03-18 03:20:53
问题 I have code that does something like function myFunc(condition){ if(condition){ process.exit(ERROR_CODE) } } How can I test this in Jest? Overwriting exit in process with jest.fn() and returning it back after the test doesn't work, since the process exits 回答1: The other suggestions in this thread would cause errors on my end, where any tests with process.exit would run indefinitely. The following option worked for me on TypeScript, but it should work on JavaScript as well: const mockExit =

sgu 321 The Spy Network (dfs+贪心)

人盡茶涼 提交于 2020-03-04 15:31:22
321. The Spy Network Time limit per test: 0.5 second(s) Memory limit: 65536 kilobytes input: standard output: standard The network of spies consists of N intelligence officers. They are numbered with the code numbers from 1 to N so that nobody could discover them. The number 1 belongs to the radiowoman Kat. There is exactly N - 1 communication channels between the spies. It is known that a message from any spy to Kat can reach her. All channels are unidirectional. A channel can have one of two types: protected and almost protected. It is known that a message will not be intercepted almost

Testing FileSaver in Angular 5

不想你离开。 提交于 2020-03-02 00:28:54
问题 I have imported file-saver in angular 5 application, when I am executing test cases it is giving following error: TypeError: FileSaver.saveAs is not a function Spec.ts: import FileSaver from 'file-saver'; .ts import FileSaver from 'file-saver'; FileSaver.saveAs(blob, filename); How to mock filesaver in test cases. 回答1: This works in Angular 7, so I'm guessing it will work in NG5. The first thing is to import FileSaver using the 'as' syntax in both the component and the spec: import * as

Robolectric buildActivity() with Mockito spy?

百般思念 提交于 2020-01-28 10:31:05
问题 It seems to me that building an Activity unit test with Robolectric's lifecycle utilities (starting with Robolectric.buildActivity() ) and spying on the same Activity with a Mockito spy are mutually exclusive. Because buildActivity() controls the construction of the Activity object, the only place to add a spy for the Activity is after calling buildActivity() . However, the spy doesn't function properly when it's added after the fact. This is especially true when spying for side effects of

How to spy a function with Sinon.js that is in the same js file as the function under test

血红的双手。 提交于 2020-01-07 04:13:04
问题 I have a problem with Sinon.js while trying to spy a function that is in the same javascript file as the function that I want to test. Furthermore I assert that the spied function is called once. Unfortunately the test fails. Interesting thing is, that if the spied function is in another javascript file than the function under test it works ! Here is my code: mock_test.js: var sinon = require('sinon') var one = require('./one.js') var two = require('./two.js') describe('Spy ', function () {

How to spy on window.scrollTo in Jest?

*爱你&永不变心* 提交于 2020-01-02 02:02:38
问题 I got a very simple react component with following functionallity: componentDidMount() { window.scrollTo(0, 0) } It seems that you cannot do something like window.scrollTo = jest.fn() to spy on scrollTo function. Therefor, I want to know what is the best way to spy on this function and assert that it has been used in my tests. Thanks 回答1: In jest the global name space must be accessed by global not window . global.scrollTo = jest.fn() 回答2: I had a similar problem and just assigning the spy

调试windows消息

这一生的挚爱 提交于 2019-12-27 00:08:07
spy++。可以查看窗口、消息、线程、进程。spy->message->windows->windows finder tool,拖动焦距图标到想监视的指定窗口,定好消息格式,重定向到日志文件,最小化到后台运行。一系列操作完成基本的监控功能。 需要注意的是,spy++的消息顺序可能被程序中的API函数PeekMessage的调用影响,这个函数可以以不同于消息到达时的顺序处理消息队列中的消息。但是也有例外,如Windows API文档中规定,WM_NCDESTROY是一个窗口被销毁之前发出的最后一条消息。 来源: https://www.cnblogs.com/bbsno1/p/3263244.html