Automocking dependencies typed with abstract class #66
-
As I understand, it's not possible to use automock to mock class dependencies that expect any implementation of an interface. What about when the dependency expect an implementation of an abstract class? Is it possible? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @gabrielgasp, I apologize for any confusion. Let me clarify regarding your questions about mocking class dependencies that expect an implementation of an interface or an abstract class. Automock does support mocking such dependencies. The approach for mocking these dependencies may vary depending on the framework you are using. Let's consider the scenarios separately:
In order to mock an implementation of an interface, you typically need to provide a "token" that represents the interface. This token allows you to bind the interface to the desired mock implementation. One example is using NestJS, where you can use the interface Logger {
// Interface methods here
}
class Foo {
public constructor(@Inject('ActualLogger') private readonly logger: Logger) {
// Class logic...
}
} By specifying the To provide a more specific solution, it would be helpful to know which framework you are using. Please let me know, and I can guide you accordingly.
Similar to mocking interface implementations, you can also mock dependencies that expect an implementation of an abstract class. The process involves providing a mock implementation of the abstract class when configuring the dependency injection system. If you can share an example of a dependency that expects an abstract class implementation, I can provide a more tailored solution for your specific case. Please let me know the framework you are using and share any example code you have, and I'll be happy to suggest a suitable solution. |
Beta Was this translation helpful? Give feedback.
Hi @gabrielgasp,
I apologize for any confusion. Let me clarify regarding your questions about mocking class dependencies that expect an implementation of an interface or an abstract class.
Automock does support mocking such dependencies. The approach for mocking these dependencies may vary depending on the framework you are using. Let's consider the scenarios separately:
In order to mock an implementation of an interface, you typically need to provide a "token" that represents the interface. This token allows you to bind the interface to the desired mock implementation. One example is using NestJS, where you can u…