mocking

TypeScript doesn't recognize my jest mock module

南笙酒味 提交于 2021-02-07 13:32:34
问题 Suppose I have an index.ts that will import Database.ts and run some queries. To test this index.ts file, I want to mock the Database.ts because I don't want to connect to any real database. This is my index.ts : import { connect } from './Database' export async function runSomeQuery() { const connection = await connect() const result = await connection.query('SOME QUERY') return result } and here is the mock of database ( __mocks__/Database.ts ) const mockConnection = { query: jest.fn() }

How to get around “sys.exit()” in python nosetest?

北战南征 提交于 2021-02-07 11:22:49
问题 It seems that python nosetest will quit when encountered "sys.exit()", and mocking of this built-in doesn't work. Thanks for suggestions. 回答1: You can try catching the SystemExit exception. It is raised when someone calls sys.exit() . with self.assertRaises(SystemExit): myFunctionThatSometimesCallsSysExit() 回答2: import sys sys.exit = lambda *x: None Keep in mind that programs may reasonably expect not to continue after sys.exit() , so patching it out might not actually help... 回答3: If you're

How to get around “sys.exit()” in python nosetest?

牧云@^-^@ 提交于 2021-02-07 11:22:30
问题 It seems that python nosetest will quit when encountered "sys.exit()", and mocking of this built-in doesn't work. Thanks for suggestions. 回答1: You can try catching the SystemExit exception. It is raised when someone calls sys.exit() . with self.assertRaises(SystemExit): myFunctionThatSometimesCallsSysExit() 回答2: import sys sys.exit = lambda *x: None Keep in mind that programs may reasonably expect not to continue after sys.exit() , so patching it out might not actually help... 回答3: If you're

Mocking SparkSession for unit testing

本小妞迷上赌 提交于 2021-02-07 08:15:24
问题 I have a method in my spark application that loads the data from a MySQL database. the method looks something like this. trait DataManager { val session: SparkSession def loadFromDatabase(input: Input): DataFrame = { session.read.jdbc(input.jdbcUrl, s"(${input.selectQuery}) T0", input.columnName, 0L, input.maxId, input.parallelism, input.connectionProperties) } } The method does nothing else other than executing jdbc method and loads data from the database. How can I test this method? The

Mocking SparkSession for unit testing

余生颓废 提交于 2021-02-07 08:14:03
问题 I have a method in my spark application that loads the data from a MySQL database. the method looks something like this. trait DataManager { val session: SparkSession def loadFromDatabase(input: Input): DataFrame = { session.read.jdbc(input.jdbcUrl, s"(${input.selectQuery}) T0", input.columnName, 0L, input.maxId, input.parallelism, input.connectionProperties) } } The method does nothing else other than executing jdbc method and loads data from the database. How can I test this method? The

Mock ProceedingJoinPoint Signature

↘锁芯ラ 提交于 2021-02-07 06:46:15
问题 I am trying to mock a ProceedingJoinPoint class and I am having difficulty mocking a method. Here is the code that is calling the mock class: ... // ProceedingJoinPoint joinPoint Object targetObject = joinPoint.getTarget(); try { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); ... ... and here is my mocked class attempt so far... accountService = new AccountService(); ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class)

Using mock objects outside of testing, bad practice?

瘦欲@ 提交于 2021-02-07 03:40:28
问题 I'm working on a project where there is a lot of external service messaging. A good way to describe it in only a slightly "hyperbolas" way would be an application where the system has to send messages to the Flicker API, the Facebook API, and the Netflix API. To support disconnected scenarios, logging concerns, developer usability, configuration, etc... I've experimented using an approach which heavily uses generics and expression trees. The end result looks like this: Messenger<NetflixApi>

Using mock objects outside of testing, bad practice?

青春壹個敷衍的年華 提交于 2021-02-07 03:38:16
问题 I'm working on a project where there is a lot of external service messaging. A good way to describe it in only a slightly "hyperbolas" way would be an application where the system has to send messages to the Flicker API, the Facebook API, and the Netflix API. To support disconnected scenarios, logging concerns, developer usability, configuration, etc... I've experimented using an approach which heavily uses generics and expression trees. The end result looks like this: Messenger<NetflixApi>

Testing listeners with Queue::fake()

夙愿已清 提交于 2021-02-06 15:18:55
问题 My Laravel 5.5 application has a Product model. The Product model has a dispatchesEvents property that looks like this: /** * The event map for the model. * * @var array */ protected $dispatchesEvents = [ 'created' => ProductCreated::class, 'updated' => ProductUpdated::class, 'deleted' => ProductDeleted::class ]; I also have a listener that is called CreateProductInMagento which is mapped to the ProductCreated event in the EventServiceProvider . This listener implements the ShouldQueue

Testing listeners with Queue::fake()

江枫思渺然 提交于 2021-02-06 15:17:33
问题 My Laravel 5.5 application has a Product model. The Product model has a dispatchesEvents property that looks like this: /** * The event map for the model. * * @var array */ protected $dispatchesEvents = [ 'created' => ProductCreated::class, 'updated' => ProductUpdated::class, 'deleted' => ProductDeleted::class ]; I also have a listener that is called CreateProductInMagento which is mapped to the ProductCreated event in the EventServiceProvider . This listener implements the ShouldQueue